Skip to content

fix: encode bytes in SessionAgent/Session.to_dict() for JSON serialization#1899

Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/s3-session-bytes-serialization
Open

fix: encode bytes in SessionAgent/Session.to_dict() for JSON serialization#1899
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/s3-session-bytes-serialization

Conversation

@giulio-leone
Copy link
Contributor

Problem

S3SessionManager and FileSessionManager crash with TypeError: Object of type bytes is not JSON serializable when agent state contains binary content (e.g., inline PDF bytes from multimodal prompts via LiteLLM proxy).

Root Cause

Inconsistent to_dict() implementations:

Class to_dict() Bytes-safe?
SessionMessage encode_bytes_values(asdict(self)) Yes
SessionAgent asdict(self) No
Session asdict(self) No

SessionMessage.to_dict() correctly uses encode_bytes_values() (which converts bytes{"__bytes_encoded__": True, "data": "<base64>"}) before JSON serialization. But SessionAgent.to_dict() and Session.to_dict() skip this encoding, so any bytes values in agent state cause json.dumps() to crash.

Fix

  1. SessionAgent.to_dict(): Apply encode_bytes_values(asdict(self)) — matching SessionMessage
  2. SessionAgent.from_dict(): Apply decode_bytes_values() for round-trip correctness (matching SessionMessage.from_dict())
  3. Session.to_dict(): Apply encode_bytes_values(asdict(self)) for consistency

Tests

Added two regression tests:

  • test_session_agent_with_bytes: Verifies SessionAgent with binary document content survives JSON round-trip
  • test_session_with_bytes_in_session_type: Verifies Session.to_dict() is JSON-safe

All 116 existing session tests + 9 type tests continue to pass.

Fixes #1864

…ation

SessionAgent.to_dict() and Session.to_dict() call asdict() directly
without encoding bytes values, while SessionMessage.to_dict() correctly
uses encode_bytes_values(). This inconsistency causes S3SessionManager
and FileSessionManager to crash with:

  TypeError: Object of type bytes is not JSON serializable

when agent state contains binary content (e.g., inline PDF bytes from
multimodal prompts passed through LiteLLM proxy).

Fix: Apply encode_bytes_values() in SessionAgent.to_dict() and
Session.to_dict() (matching SessionMessage.to_dict()), and add
decode_bytes_values() in SessionAgent.from_dict() for round-trip
correctness.

Added regression tests for bytes serialization in SessionAgent.

Fixes strands-agents#1864
@giulio-leone giulio-leone force-pushed the fix/s3-session-bytes-serialization branch from 2ae3acf to 87df583 Compare March 15, 2026 16:12
@github-actions github-actions bot added size/s and removed size/s labels Mar 15, 2026
@giulio-leone
Copy link
Contributor Author

Friendly ping — encodes bytes values in SessionAgent/Session.to_dict() for JSON serialization compatibility, preventing TypeError on export.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] S3SessionManager JSON serialization fails when graph state contains binary document content from multimodal prompts

1 participant