[Bounty proposal] fix(gmail-tools): guard string max_results coercion, sanitize labels, and support nested MIME multipart body ($50 proposed)
Problem & Root Cause Analysis
In backend/utils/retrieval/tools/gmail_tools.py:
-
TypeError on String
max_results: Inget_gmail_messages_tool:max_results = ensure_capped(max_results, 50, "⚠️ get_gmail_messages_tool - max_results capped from {} to {}")When LLMs pass
max_resultsas a string (e.g."10"or"5"via structured JSON tool calling),val > capraisesTypeError: '>' not supported between instances of 'str' and 'int'. Similarly,min(max_results, 50)inget_gmail_messagesraisesTypeError. -
AttributeError / HTTP 400 Bad Request on
label: Whenlabelis passed with whitespace (e.g." "),label_ids = [label_upper]createslabelIds=[" "], triggering an immediate HTTP 400 Bad Request from the Gmail API (Invalid labelIds value). Furthermore, if a non-string or boolean is passed, callinglabel.upper()raisesAttributeError. -
AttributeError & TypeError in
parse_gmail_messageon Malformed or None Payloads:payload = message.get('payload', {}) headers = payload.get('headers', []) header_dict = {h['name'].lower(): h['value'] for h in headers}If a message has
payload: None,payloadbecomesNoneand.get('headers')crashes withAttributeError. IfheadersisNone(common in minimal drafts or metadata messages), iterating overheadersraisesTypeError: 'NoneType' object is not iterable. If a header dictionary contains'name': None,h['name'].lower()raisesAttributeError. -
Empty Body on Nested MIME Multipart Messages (
multipart/mixed+multipart/alternative): Standard MIME messages containing both attachments and formatted text nestmultipart/alternativeinside a parentmultipart/mixedpart. The existingparse_gmail_messageonly examined direct children ofpayload['parts'], completely missing nested plain text and HTML bodies, resulting inbody_text = ''. -
TypeError on None
messagesin API Response:ids = [msg['id'] for msg in data.get('messages', [])]raisesTypeErrorif the Gmail API response explicitly contains'messages': None. -
Missing Top-Level
user_idConfiguration Resolution: If an invoking caller suppliesconfig={"user_id": "..."}without a nested"configurable"dictionary,prepare_accessfails with an access error.
Proposed Solution
- Type Coercion & Bounds: Safely coerce
max_resultstointclamped to1..50(default 10) in bothget_gmail_messages_toolandget_gmail_messages. - Label Sanitization: Strip and validate
labelstring before converting to upper case, preventing empty/whitespacelabelIdsand ensuringUNREADproperly transforms intois:unreadsearch operator. - MIME Recursive Text Extractor: Implement
_extract_body_textto recursively traverse nested MIME parts fortext/plainandtext/htmlbodies with safe base64 decoding. - Resilient Payload & Header Parsing: Safely guard
payload,headers, and headernameentries againstNone. - Safe Response Handling: Check
(data.get('messages') or [])and validate message dicts with valididkeys. - Config Normalization: Ensure top-level
user_idinconfigis forwarded toconfigurable. - Comprehensive Unit Tests: Add
backend/tests/unit/test_gmail_tools.pywith 9 hermetic unit tests verifying all edge cases.
Proposed Bounty
- $50 USDC
Payout Details
- USDC (Polygon / Base / Arbitrum):
0xC345C07229307d4f1c7935aD2cAe48e390215808
Fix implemented and covered by 9 hermetic unit tests in PR #23192.
Bounty Proposal & Claim
- Target Issue: #23191 ($50 proposed)
- PR: https://github.com/BasedHardware/omi/pull/23192
- USDC (Polygon / Base / Arbitrum):
0xC345C07229307d4f1c7935aD2cAe48e390215808
Thanks for putting this together. We aren't doing bounties at the moment, so we're closing the bounty proposal issues for now — thanks for the fix! 🙏