Skip to content

Comments

fix: insert retrieved LTM before last user message to avoid prefill error#271

Merged
notgitika merged 1 commit intomainfrom
fix/ltm-prefill-insertion-order
Feb 19, 2026
Merged

fix: insert retrieved LTM before last user message to avoid prefill error#271
notgitika merged 1 commit intomainfrom
fix/ltm-prefill-insertion-order

Conversation

@notgitika
Copy link
Contributor

@notgitika notgitika commented Feb 19, 2026

Summary

  • Fixes assistant-prefill error on Claude 4.6+ models (Opus 4.6, Sonnet 4.6) when using AgentCoreMemorySessionManager with LTM retrieval

  • Previously, retrieved memory was appended as an assistant message after the last user message, making it the final message sent to the model. Claude 4.6 rejects this with: "This model does not support assistant message prefill. The conversation must end with a user message."

  • Now inserts the assistant memory message before the last user message, keeping the user's query last

  • For the first message in a conversation (where prepending an assistant message would violate Bedrock's "must start with user" rule), injects memory as a content block within the user message instead

  • Renames <user_context> tag to <retrieved_memory> for semantic accuracy

Closes #191

Related issue strands-agents/sdk-python#1694

Testing

  • Ran all unit tests
  • Reproduced the prefill error against Opus 4.6 with the old code path (ValidationException: does not support assistant message prefill)
  • Verified both fix paths (insert-before and inline-in-first-msg) work against Opus 4.6
  • Full end-to-end test with AgentCoreMemorySessionManager + Opus 4.6 — no prefill error
  • Integration tests updated (<user_context><retrieved_memory>)

@jsgsamzn
Copy link

If I'm reading this correctly:

  1. If there's only one user message, the memory is placed in a content block in that initial user's message.
  2. If there's more than one message, the memory is placed in a new "assistant" message before the latest user message.

Is there a potential issue with the role not being consistent here?

@notgitika notgitika force-pushed the fix/ltm-prefill-insertion-order branch from 4dd7162 to 1c82c35 Compare February 19, 2026 22:14
@notgitika
Copy link
Contributor Author

Is there a potential issue with the role not being consistent here?

You're right, there could be. I've jsut updated it to always inline as a content block in the last user message so the role is consistent.

Copy link

@vivdalal vivdalal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@notgitika notgitika merged commit 232d05c into main Feb 19, 2026
20 checks passed
@notgitika
Copy link
Contributor Author

Related issue: strands-agents/sdk-python#1736

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strands session_manager: insert retrieved memory before user msg; rename <user_context>

3 participants