You want to know how many emails are in your inbox right now — not ten minutes ago, right now. What shape do you think the Gmail API hands back when you ask?
I guess it returns a list? And then I call len() on that list?
Almost. It returns a dictionary, and one key inside ("messages") holds the list of emails. The safe way to reach into a dict without crashing is .get(key, []) — an empty list if the key is missing:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
count = len(result.get("messages", []))So every API call goes through toolset.execute_action? I pass the action enum and a dict of parameters, and I get a dict back?
That is the entire Composio interface for this track. One function, one enum, one params dict. Wrap it in a named function so the rest of your code can call it cleanly:
def count_emails(max_results: int) -> int:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
return len(result.get("messages", []))What if my inbox is empty? Does Gmail drop the "messages" key entirely, or send back an empty list?
Either shape is possible, and .get("messages", []) handles both. Missing key falls back to []; key present with an empty list is still length zero. Your function returns 0 cleanly in either case.
So the number the test prints is actually my live inbox — if I add an email and re-run, the count goes up?
Live data, your inbox, every run. When the test prints 47, that is the real count sitting in your mailbox at that moment.
TL;DR: Call toolset.execute_action with an Action enum plus a params dict, then reach into the response with .get().
Action.GMAIL_FETCH_EMAILS is typed, so typos fail at import, not runtime{"max_results": N} caps how many messages come back{"messages": [...]}; the key can be absent on empty inboxes.get() beats []| Access | Empty inbox |
|---|---|
result["messages"] | KeyError |
result.get("messages", []) | [] → len() is 0 |
Create a free account to get started. Paid plans unlock all tracks.
You want to know how many emails are in your inbox right now — not ten minutes ago, right now. What shape do you think the Gmail API hands back when you ask?
I guess it returns a list? And then I call len() on that list?
Almost. It returns a dictionary, and one key inside ("messages") holds the list of emails. The safe way to reach into a dict without crashing is .get(key, []) — an empty list if the key is missing:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
count = len(result.get("messages", []))So every API call goes through toolset.execute_action? I pass the action enum and a dict of parameters, and I get a dict back?
That is the entire Composio interface for this track. One function, one enum, one params dict. Wrap it in a named function so the rest of your code can call it cleanly:
def count_emails(max_results: int) -> int:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_results})
return len(result.get("messages", []))What if my inbox is empty? Does Gmail drop the "messages" key entirely, or send back an empty list?
Either shape is possible, and .get("messages", []) handles both. Missing key falls back to []; key present with an empty list is still length zero. Your function returns 0 cleanly in either case.
So the number the test prints is actually my live inbox — if I add an email and re-run, the count goes up?
Live data, your inbox, every run. When the test prints 47, that is the real count sitting in your mailbox at that moment.
TL;DR: Call toolset.execute_action with an Action enum plus a params dict, then reach into the response with .get().
Action.GMAIL_FETCH_EMAILS is typed, so typos fail at import, not runtime{"max_results": N} caps how many messages come back{"messages": [...]}; the key can be absent on empty inboxes.get() beats []| Access | Empty inbox |
|---|---|
result["messages"] | KeyError |
result.get("messages", []) | [] → len() is 0 |