Skip to content

fix: add button title validation and base64 decode error handling#6773

Open
Himanshu040604 wants to merge 1 commit intoupdate/whatsapp-interface-v2from
fix/whatsapp-validation-bugs
Open

fix: add button title validation and base64 decode error handling#6773
Himanshu040604 wants to merge 1 commit intoupdate/whatsapp-interface-v2from
fix/whatsapp-validation-bugs

Conversation

@Himanshu040604
Copy link
Contributor

Summary

Fixes two bugs identified during review of #6466:

Bug 1: Missing button title length validation

  • File: libs/agno/agno/tools/whatsapp.py
  • ReplyButton.title used Field(description="...max 20 characters") which is
    just documentation — Pydantic does NOT enforce it. WhatsApp Cloud API rejects
    titles >20 characters with HTTP 400.
  • Fix: Added max_length=20 to the Pydantic Field so validation happens
    before the API call.

Bug 2: Unhandled base64 decode crash in webhook handler

  • File: libs/agno/agno/os/interfaces/whatsapp/router.py
  • base64.b64decode(content) in _extract_media_bytes() crashes with
    binascii.Error when content is a non-base64 string (e.g., a file path),
    killing the entire webhook handler.
  • Fix: Added try/except guard that logs a warning and returns None
    gracefully, consistent with the existing error handling pattern in the function.

Related

Tests

  • All 46 existing WhatsApp tests pass (23 tools + 13 security + 10 router)
  • No regressions introduced

- 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>
Copilot AI review requested due to automatic review settings February 28, 2026 09:18
@Himanshu040604 Himanshu040604 requested a review from a team as a code owner February 28, 2026 09:18
@Himanshu040604 Himanshu040604 added the bug Something isn't working label Feb 28, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes two bugs identified during review of the WhatsApp interface overhaul (#6466):

  1. 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. Adding max_length=20 makes validation happen before any API call.
  2. Unhandled binascii.Error in _extract_media_bytes() — when content is a str that is not valid base64 (e.g., a file path), calling base64.b64decode(content) would crash the webhook handler. A try/except guard now logs a warning and returns None gracefully.

Changes:

  • Added max_length=20 to the Pydantic Field for ReplyButton.title to enforce the WhatsApp API constraint at the model level.
  • Wrapped base64.b64decode(content) in _extract_media_bytes() with a try/except block that logs a warning and returns None on 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.")
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +372 to +376
try:
return base64.b64decode(content)
except Exception:
log_warning("Failed to decode base64 content: not valid base64 data")
return None
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@VirusDumb
Copy link
Contributor

VirusDumb commented Mar 2, 2026

lgtm @Mustafa-Esoofally @uzaxirr any reviews from you before we merge?
(apart from tests)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants