aws / aws/bedrock-agentcore-sdk-python
Restored history with `filter_restored_tool_context` can be Converse-invalid (assistant-prefill ValidationException)
- Vorherrschende Sprache
- Python
- Sterne
- 761
- Forks
- 147
- Ø Merge
- 1 T. 23 Std.
- Gemergte PRs (30 T.)
- 7
Beschreibung
**Describe the bug**
When `AgentCoreMemorySessionManager` is configured with `filter_restored_tool_context=True`, restoring a session can produce a message array that Bedrock Converse rejects.
`_filter_restored_tool_context` strips `toolUse`/`toolResult` blocks from restored history. A user turn that carried *only* a `toolResult` becomes empty and is dropped. Dropping it leaves the two surrounding assistant turns adjacent, which breaks Converse's strict role alternation. The history can also end up starting on an assistant turn.
On the next invocation Bedrock rejects the array. On models that reject assistant prefill it surfaces as:
```
ValidationException (ConverseStream): This model does not support assistant message prefill.
The conversation must end with a user message.
```
In a Strands agent on AgentCore Runtime this fails the event loop and the invocation returns a 500. The invalid array is built by the tool-use loop plus restore regardless of model — a prefill-tolerant model silently accepts it, so the issue can stay hidden until a model switch surfaces it (observed: the same restored history failed on Claude Opus 4.8 but was accepted by Claude Sonnet 4.6).
**To Reproduce**
A restored conversation of this shape is enough to trigger it:
```
user(query) → assistant(text + toolUse) → user(toolResult only) → assistant(answer)
```
1. Configure `AgentCoreMemoryConfig(..., filter_restored_tool_context=True)`.
2. Persist a turn where the assistant calls a tool and the following user turn contains only the `toolResult`.
3. Restore the session (e.g. construct a new `Agent` with the same session manager) and invoke it.
4. After stripping, the toolResult-only user turn is dropped and the history collapses to two adjacent `assistant` turns; the next Converse call fails with the ValidationException above.
**Expected behavior**
After stripping tool blocks, the restored history should stay valid for Bedrock Converse: strict role alternation, starting on a user turn. Adjacent same-role turns left behind by the strip should be merged so the array remains valid once the runtime appends the next user turn before the model call.
**Environment**
- `bedrock-agentcore` (Strands integration: `bedrock_agentcore.memory.integrations.strands`)
- Strands agent running on Amazon Bedrock AgentCore Runtime
- Bedrock model that rejects assistant prefill, `ConverseStream`
Beitragsleitfaden
Rechercherichtung
Beginne in bedrock_agentcore.memory.integrations.strands und konzentriere dich auf AgentCoreMemorySessionManager und _filter_restored_tool_context. Rekonstruiere bei aktiviertem Filtering den Verlauf user → assistant/toolUse → user/toolResult → assistant und überprüfe anschließend, dass die wiederhergestellten Nachrichten mit einem user-Turn beginnen, eine strikte Rollenalternation beibehalten und vor dem nächsten Converse-Aufruf weiterhin gültig sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- aws, python
- Bereich
- ai, backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 70/100