fix: encode bytes in SessionAgent/Session.to_dict() for JSON serialization#1899
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: encode bytes in SessionAgent/Session.to_dict() for JSON serialization#1899giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
3 tasks
…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
2ae3acf to
87df583
Compare
Contributor
Author
|
Friendly ping — encodes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
S3SessionManagerandFileSessionManagercrash withTypeError: Object of type bytes is not JSON serializablewhen agent state contains binary content (e.g., inline PDF bytes from multimodal prompts via LiteLLM proxy).Root Cause
Inconsistent
to_dict()implementations:to_dict()SessionMessageencode_bytes_values(asdict(self))✅SessionAgentasdict(self)❌Sessionasdict(self)❌SessionMessage.to_dict()correctly usesencode_bytes_values()(which convertsbytes→{"__bytes_encoded__": True, "data": "<base64>"}) before JSON serialization. ButSessionAgent.to_dict()andSession.to_dict()skip this encoding, so anybytesvalues in agent state causejson.dumps()to crash.Fix
SessionAgent.to_dict(): Applyencode_bytes_values(asdict(self))— matchingSessionMessageSessionAgent.from_dict(): Applydecode_bytes_values()for round-trip correctness (matchingSessionMessage.from_dict())Session.to_dict(): Applyencode_bytes_values(asdict(self))for consistencyTests
Added two regression tests:
test_session_agent_with_bytes: VerifiesSessionAgentwith binary document content survives JSON round-triptest_session_with_bytes_in_session_type: VerifiesSession.to_dict()is JSON-safeAll 116 existing session tests + 9 type tests continue to pass.
Fixes #1864