Repository navigation
fix(memory): skip stored events that are not a SessionMessage on restore - #749
Open
abhiramshaji-cyber wants to merge 3 commits into
Open
abhiramshaji-cyber wants to merge 3 commits into
abhiramshaji-cyber wants to merge 3 commits into
Conversation
AgentCoreMemoryConverter.events_to_messages parsed every conversational payload as a SessionMessage with no guard, so one plain text event, or any JSON that is not a SessionMessage, raised inside list_messages and the restored agent started with an empty history. The blob branch had the same gap for non string entries and non object JSON. Both branches now go through one parser that returns None for anything that is not a SessionMessage with a Strands Message shape, and that one payload is skipped and logged while the rest of the history is restored. Thanks to @kimnamu for reproducing this on main, finding the 3, null and {"note": "x"} shapes the first suggested fix missed, and writing the parametrized test this change extends. Fixes aws#743
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 15:48 — with
GitHub Actions
Waiting
Contributor
Author
|
Small update after kimnamu's testing on #743: the blob parse error now logs only the event id instead of the whole |
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
abhiramshaji-cyber
requested a deployment
to
manual-approval
November 20, 2026 21:42 — with
GitHub Actions
Waiting
This branch is waiting to be deployed
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.
Fixes #743
A Strands agent restored from AgentCore Memory came back with no history at all when one stored event was not a
SessionMessage, and now only that one event is skipped.What this code is for
AgentCoreMemorySessionManager.list_messagesreads the session's events from AgentCore Memory and hands them toAgentCoreMemoryConverter.events_to_messages, which turns eachconversationalorblobpayload back into a StrandsSessionMessage.RepositorySessionManager.initializecalls this when an agent starts, so it decides which history the agent starts with.Before this PR
A developer runs a Strands agent with
AgentCoreMemorySessionManager, and the same session also holds one plain text conversational event, for example one written byMemoryClient.save_conversationor theAgentCoreMemoryStoresender.initializecallslist_messages.list_messagespasses every event toevents_to_messages(session_manager.py L804).conversationalbranch runsSessionMessage.from_dict(json.loads(conv["content"]["text"]))with notryaround it (bedrock_converter.py L79 to L84):"I prefer window seats"raisesJSONDecodeError.3andnullparse, thenfrom_dictcallsenv.items()and raisesAttributeError.{"note": "x"}raisesTypeErrorfor the missingmessageandmessage_id.except Exceptioninlist_messages(L812 to L814) logsFailed to list messages from AgentCore Memoryand returns[].The
blobbranch below it only catchesjson.JSONDecodeError, ValueError, so a blob pair whose first entry is a number, or JSON that is not an object, empties the history the same way.After this PR
Same developer, same session.
_parse_session_message. It returnsNonewhen the text is not JSON, is JSON but not an object, is missingSessionMessagefields, has bad base64 bytes, or has amessage(orredact_message) that is not a Strands message: aroleofuserorassistantand acontentlist of objects whosetext, if present, is a string.None,events_to_messageslogsSkipping a payload that is not a SessionMessage in event <eventId>and moves on to the next payload.list_messagesnow returns 2 messages for each of"I prefer window seats",3,nulland{"note": "x"}, wheremainreturned 0.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: one odd event, 0 messages restored"] direction TB B1["agent starts, initialize"] --> B2["list_messages"] B2 --> B3["events_to_messages"] B3 --> B4{"json.loads then SessionMessage.from_dict"} B4 -->|"plain text, 3, null, other JSON"| B5["raises, list_messages returns empty list"] B4 -->|"stored SessionMessage"| B6["message restored"] end subgraph AFTER["AFTER: odd event skipped, every other message restored"] direction TB A1["agent starts, initialize"] --> A2["list_messages"] A2 --> A3["events_to_messages"] A3 --> A4{"THE CHANGE: _parse_session_message"} A4 -->|"plain text, 3, null, other JSON"| A5["returns None, payload logged and skipped"] A4 -->|"stored SessionMessage"| A6["message restored, same as before"] end B1 ~~~ A1 class B1,B2,B3,B6 box class A1,A2,A3,A6 box class B4,B5 bad class A4,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
bedrock_converter.py,_parse_session_messagewrapsjson.loadsandSessionMessage.from_dictinexcept (TypeError, ValueError, RecursionError)(JSONDecodeErrorand the base64binascii.Errorare bothValueError;RecursionErrorcovers deeply nested JSON that still fits the 100000 character payload limit), rejects JSON that is not an object beforefrom_dictsees it, and checks the message shape with_is_message. That check is what keeps a payload like{"message": {"role": "user", "content": "hi"}, "message_id": 0}from reaching_filter_empty_textand raisingAttributeErrorthere. This is the fix.The
conversationalandblobbranches now share that parser, so theblobbranch gets the same coverage, and its outerjson.loadsalso catchesTypeErrorandRecursionError. Both error log lines, the skip line and the blob parse error, name theeventIdinstead of printing the payload, so user text does not end up in error logs. A dict blob now reaches the blob parseexcept, so that line matters too (thanks @kimnamu for catching it on #743).Tests: @kimnamu's parametrized test from #743, extended to 15 shapes, each run once as a
conversationalpayload and once as ablobpayload, plus one for ablobpair whose first entry is a number. 29 of the 31 fail onmain, all pass here. One more test feeds a dict blob and a non JSON string blob and checks neither value shows up in the error log.tests/bedrock_agentcore/memory827 passed, fulltests3858 passed on 9259dcb,ruff checkandruff format --checkclean.Thanks a lot to @kimnamu for reproducing this on
main, finding the3,nulland{"note": "x"}shapes my first suggestedexcepttuple missed, and writing the test this builds on.What changes for a user
redact_messageand base64 encoded bytes.blobbranch andOpenAIConverseConverter.events_to_messagesalready behave. If you would rather filter these earlier inlist_messages, or raise instead, I'm happy to change it.roleis notuserorassistantis now skipped. Strands only writes those two roles, so nothing the session manager wrote is affected.