Skip to content

Conversation

@kevmyung
Copy link
Contributor

Summary

Fixes the TypeError: Object of type bytes is not JSON serializable error in message_to_payload when messages contain binary data (e.g., images from tool results).

Problem

The _filter_empty_text function was applied to session_message.message (which contains raw bytes) before to_dict() was called. The filtered message (with raw bytes) then overwrote the encoded version, causing json.dumps() to fail.

Solution

Reorder operations to:

  1. Call to_dict() first (encodes bytes to base64)
  2. Apply _filter_empty_text to the encoded message

Testing

Verified with test script that:

  • v1.1.1: PASS (no _filter_empty_text)
  • v1.1.2: FAIL (message_to_payload with image bytes)
  • v1.1.2 + this fix: PASS

Fixes #198

The _filter_empty_text function was applied to the raw session_message.message
which contains unencoded bytes. This caused json.dumps() to fail with
'Object of type bytes is not JSON serializable' when messages contained
image data (e.g., tool results with screenshots or diagrams).

The fix reorders the operations to call to_dict() first (which encodes
bytes to base64 via encode_bytes_values()), then applies _filter_empty_text
to the already-encoded message.

Fixes aws#198
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@4f8c625). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #199   +/-   ##
=======================================
  Coverage        ?   90.94%           
=======================================
  Files           ?       34           
  Lines           ?     3224           
  Branches        ?      459           
=======================================
  Hits            ?     2932           
  Misses          ?      166           
  Partials        ?      126           
Flag Coverage Δ
unittests 90.94% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bergjaak bergjaak merged commit 3f01653 into aws:main Jan 7, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: message_to_payload fails with 'Object of type bytes is not JSON serializable' when message contains image bytes

3 participants