Repository navigation
fix(memory): encode bytes in OpenAI converter reasoning blocks - #718
Conversation
OpenAIConverseConverter stored reasoningContent blocks as is, so a redactedContent value (bytes) made json.dumps raise and broke the agent call. Encode bytes the same way SessionMessage.to_dict does when saving, and decode them on restore, matching AgentCoreMemoryConverter. Fixes aws#680
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #718 +/- ##
=======================================
Coverage ? 89.68%
=======================================
Files ? 125
Lines ? 10885
Branches ? 1694
=======================================
Hits ? 9762
Misses ? 735
Partials ? 388
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for the review @jariy17! The integration test jobs are waiting on the |
|
Hi @jariy17, hope you're doing well! Thanks again for reviewing this. I've been spending a fair bit of my time after work contributing to agent SDKs and developer tools, so it's been nice getting familiar with this project. Would be good to stay in touch. Is LinkedIn a good place to connect? https://www.linkedin.com/in/abhiram-kace/ |
|
Thanks for the report, @abhiramshaji-cyber — feedback like this is exactly Would you mind opening a new issue so we can track it properly? If this is a security issue, please report it privately via |
Fixes #680
Agents using the OpenAI memory format crashed on any reply that carried hidden reasoning, and now that reply is saved and restored like any other.
What this code is for
OpenAIConverseConverterinconverters/openai.pyturns each Strands message into an OpenAI shaped JSON string beforeAgentCoreMemorySessionManagersaves it to AgentCore Memory, and turns stored events back into Strands messages when a session is restored. Reasoning blocks have no OpenAI field, so they are kept as they are under_strands_reasoning_content.Before this PR
A developer runs a Strands agent with
AgentCoreMemorySessionManager(converter=OpenAIConverseConverter)on a model that returns reasoning asreasoningContent.redactedContent.{"reasoningContent": {"redactedContent": b"..."}}, where the value is rawbytes.append_messagecallscreate_message, which callsOpenAIConverseConverter.message_to_payload._bedrock_to_openaicopies the block into_strands_reasoning_contentunchanged,bytesincluded.json.dumps(openai_msg)raisesTypeError: Object of type bytes is not JSON serializable.EventLoopException, on every turn that has redacted reasoning.After this PR
Same developer, same model, same turn.
_bedrock_to_openaipasses each reasoning block throughencode_bytes_values, sobytesbecomes{"__bytes_encoded__": true, "data": "<base64>"}.json.dumpssucceeds and the event is saved._openai_to_bedrockpasses each stored block throughdecode_bytes_values, so the model gets back the originalbytes.The diagram
flowchart LR classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827 classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px subgraph BEFORE["BEFORE: every redacted reasoning turn fails"] direction TB B1["model returns redactedContent bytes"] --> B2["message_to_payload"] B2 --> B3["_bedrock_to_openai keeps raw block"] B3 --> B4{"json.dumps"} B4 -->|"bytes in block"| B5["TypeError, agent call fails"] B4 -->|"text only reasoning"| B6["saved"] end subgraph AFTER["AFTER: saved and restored as bytes"] direction TB A1["model returns redactedContent bytes"] --> A2["message_to_payload"] A2 --> A3{"THE CHANGE: encode_bytes_values on block"} A3 --> A4{"json.dumps"} A4 -->|"bytes now base64"| A5["saved, decode_bytes_values on restore"] A4 -->|"text only reasoning"| A6["saved, unchanged"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A6 box class B4,B5 bad class A3,A5 good linkStyle 3 stroke:#cc0000,stroke-width:5px linkStyle 8 stroke:#008800,stroke-width:5px style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827 style AFTER fill:#f3fff3,stroke:#008800,color:#111827The change
In
converters/openai.py,reasoning_blocks.append(item)becomesreasoning_blocks.append(encode_bytes_values(item))andreasoning_items.append(rc)becomesreasoning_items.append(decode_bytes_values(rc)). Both helpers come fromstrands.types.session, the same pairSessionMessage.to_dictandSessionMessage.from_dictuse, which is howAgentCoreMemoryConverteralready handles this. A new test,test_redacted_reasoning_bytes_round_trip_in_openai_converter, saves and restores a redacted block and checks thebytescome back equal.What changes for a user
bytes, so the model receives it back on the next turn.