You can count emails. Now you want the preview text of the first one — the one-line snippet Gmail shows next to each subject. What's the safe shape for that code?
I'd read messages[0] and pull the snippet. But if the inbox is empty, messages[0] crashes, right?
Exactly. Indexing an empty list raises IndexError. Guard with an early return first, then reach into the first message's dict with .get and a default:
messages = result.get("messages", [])
if not messages:
return ""
snippet = messages[0].get("snippet", "")So both result.get("messages", []) and messages[0].get("snippet", "") use the same defensive pattern — default to something empty instead of crashing?
Same pattern, two layers deep. API responses nest dicts inside lists inside dicts, and every layer can be missing. A safe default at every hop keeps the function from blowing up on edge cases:
def get_first_snippet(max_results: int) -> str:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = result.get("messages", [])
if not messages:
return ""
return messages[0].get("snippet", "")What if the first message exists but "snippet" is missing — does the second default kick in?
Yes. Missing "snippet" on the first message returns "". The function returns an empty string instead of a traceback. You never assume any key is there — you always name a fallback.
So this is the read shape for every Gmail endpoint — fetch the list, guard for empty, pull the field from the first item?
That is the read shape for every API you touch this track. Master the safe hop and the rest is wiring.
TL;DR: Every layer of an API response can be missing — use .get() with a default at every hop.
result.get("messages", []) — missing key becomes empty listif not messages: return "" — guard before indexingmessages[0].get("snippet", "") — dict access with fallback| Shape | What saves you |
|---|---|
{} — key absent | [] default on first .get |
{"messages": []} | early return on if not |
You can count emails. Now you want the preview text of the first one — the one-line snippet Gmail shows next to each subject. What's the safe shape for that code?
I'd read messages[0] and pull the snippet. But if the inbox is empty, messages[0] crashes, right?
Exactly. Indexing an empty list raises IndexError. Guard with an early return first, then reach into the first message's dict with .get and a default:
messages = result.get("messages", [])
if not messages:
return ""
snippet = messages[0].get("snippet", "")So both result.get("messages", []) and messages[0].get("snippet", "") use the same defensive pattern — default to something empty instead of crashing?
Same pattern, two layers deep. API responses nest dicts inside lists inside dicts, and every layer can be missing. A safe default at every hop keeps the function from blowing up on edge cases:
def get_first_snippet(max_results: int) -> str:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
messages = result.get("messages", [])
if not messages:
return ""
return messages[0].get("snippet", "")What if the first message exists but "snippet" is missing — does the second default kick in?
Yes. Missing "snippet" on the first message returns "". The function returns an empty string instead of a traceback. You never assume any key is there — you always name a fallback.
So this is the read shape for every Gmail endpoint — fetch the list, guard for empty, pull the field from the first item?
That is the read shape for every API you touch this track. Master the safe hop and the rest is wiring.
TL;DR: Every layer of an API response can be missing — use .get() with a default at every hop.
result.get("messages", []) — missing key becomes empty listif not messages: return "" — guard before indexingmessages[0].get("snippet", "") — dict access with fallback| Shape | What saves you |
|---|---|
{} — key absent | [] default on first .get |
{"messages": []} | early return on if not |
Create a free account to get started. Paid plans unlock all tracks.