fix: filter non-serializable objects from stream events in prepare()#1941
Open
Exploreunive wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: filter non-serializable objects from stream events in prepare()#1941Exploreunive wants to merge 1 commit intostrands-agents:mainfrom
Exploreunive wants to merge 1 commit intostrands-agents:mainfrom
Conversation
Non-serializable objects (Agent instances, OpenTelemetry Spans, etc.) from invocation_state were being merged into event dicts via prepare(), causing JSON serialization failures when consumers attempt to serialize yielded events. Added _is_json_serializable() and _sanitize_invocation_state() helpers to dynamically filter non-serializable values before merging. This approach is more robust than maintaining a hardcoded blocklist. Fixes strands-agents#1928
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
ModelStreamEvent.prepare()merges the entireinvocation_statedict into yielded event dicts.invocation_statecontains non-serializable objects likeAgentinstances, OpenTelemetrySpans, andTraceobjects. When consumers (e.g., AgentCore SSE streaming) attempt to serialize these events, it fails withTypeErrorand potentially dumps >100MB of object repr data.Related issue: #1928
Solution
Added
_is_json_serializable()and_sanitize_invocation_state()helpers to dynamically filter out non-serializable values before merging inprepare(). This approach is:json.loads(json.dumps(v))Changes
src/strands/types/_events.py: Added_is_json_serializable()and_sanitize_invocation_state(), updatedModelStreamEvent.prepare()tests/strands/agent/hooks/test_agent_events.py: Updatedany_propsto remove filtered fieldstests/strands/agent/test_agent.py: Updated stream test expectationTesting
agent,event_loop_cycle_span,event_loop_cycle_traceare properly filtered from stream events