fix: add button title validation and base64 decode error handling#6773
fix: add button title validation and base64 decode error handling#6773Himanshu040604 wants to merge 1 commit intoupdate/whatsapp-interface-v2from
Conversation
- Add max_length=20 to ReplyButton.title Field (Bug 1) - Add try/except around base64.b64decode in _extract_media_bytes (Bug 2) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes two bugs identified during review of the WhatsApp interface overhaul (#6466):
- Missing Pydantic enforcement on
ReplyButton.title— the field previously only had documentation stating "max 20 characters", but no actual constraint, so WhatsApp Cloud API would reject over-length titles at request time. Addingmax_length=20makes validation happen before any API call. - Unhandled
binascii.Errorin_extract_media_bytes()— whencontentis astrthat is not valid base64 (e.g., a file path), callingbase64.b64decode(content)would crash the webhook handler. Atry/exceptguard now logs a warning and returnsNonegracefully.
Changes:
- Added
max_length=20to the PydanticFieldforReplyButton.titleto enforce the WhatsApp API constraint at the model level. - Wrapped
base64.b64decode(content)in_extract_media_bytes()with atry/exceptblock that logs a warning and returnsNoneon invalid base64 input.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
libs/agno/agno/tools/whatsapp.py |
Adds max_length=20 to ReplyButton.title Field to enforce WhatsApp's title character limit at the Pydantic model level |
libs/agno/agno/os/interfaces/whatsapp/router.py |
Guards base64.b64decode() in _extract_media_bytes() for the str branch against invalid base64 input, logging a warning and returning None instead of crashing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| id: str = Field(..., description="Unique button identifier (e.g. 'yes', 'no').") | ||
| title: str = Field(..., description="Button display text, max 20 characters.") | ||
| title: str = Field(..., max_length=20, description="Button display text, max 20 characters.") |
There was a problem hiding this comment.
The new max_length=20 constraint on ReplyButton.title is not covered by any test. The existing test suite is comprehensive (other boundary conditions like test_send_reply_buttons_max_3 are tested), and this validation fix is the primary code change in whatsapp.py. A test should be added that constructs a ReplyButton with a title longer than 20 characters and asserts that Pydantic raises a ValidationError.
| try: | ||
| return base64.b64decode(content) | ||
| except Exception: | ||
| log_warning("Failed to decode base64 content: not valid base64 data") | ||
| return None |
There was a problem hiding this comment.
The base64 decode error handling fix in _extract_media_bytes() for the str branch is not covered by any test. The PR description identifies this as a crash fix for a previously unhandled case. A unit test should be added that passes a mock media_obj whose .content is a non-base64 string (e.g., a file path like "/tmp/audio.mp3") and verifies that the function returns None without raising.
|
lgtm @Mustafa-Esoofally @uzaxirr any reviews from you before we merge? |
Summary
Fixes two bugs identified during review of #6466:
Bug 1: Missing button title length validation
libs/agno/agno/tools/whatsapp.pyReplyButton.titleusedField(description="...max 20 characters")which isjust documentation — Pydantic does NOT enforce it. WhatsApp Cloud API rejects
titles >20 characters with HTTP 400.
max_length=20to the Pydantic Field so validation happensbefore the API call.
Bug 2: Unhandled base64 decode crash in webhook handler
libs/agno/agno/os/interfaces/whatsapp/router.pybase64.b64decode(content)in_extract_media_bytes()crashes withbinascii.Errorwhen content is a non-base64 string (e.g., a file path),killing the entire webhook handler.
Nonegracefully, consistent with the existing error handling pattern in the function.
Related
Tests