fix(bedrock/messages): preserve compact_20260112 context_management on /v1/messages - #27534
Conversation
…n /v1/messages Bedrock InvokeModel supports the compact-2026-01-12 beta when context_management is sent with the matching anthropic-beta header — only Converse rejects it. The current /v1/messages → Bedrock Invoke transformation strips context_management unconditionally, so users hit "context_management: Extra inputs are not permitted" or quietly lose compaction. Filter context_management before the safety-net allowlist: - keep edits with type=compact_20260112 - auto-add compact-2026-01-12 to anthropic_beta when any survive - drop the field entirely when nothing supported remains, so LiteLLM-internal edits like clear_thinking_20251015 stay out of the request body - allow context_management in BedrockInvokeAnthropicMessagesRequest so the filtered field passes the allowlist Fixes BerriAI#27532
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR fixes silent compaction loss when Claude Code clients send
Confidence Score: 4/5Safe to merge; the change is narrowly scoped to the Bedrock InvokeModel transformation path and is well-tested. The logic is sound and mirrors the established Vertex AI pattern. One small defensive gap exists: when
|
| Filename | Overview |
|---|---|
| litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py | Adds _filter_context_management_for_bedrock_invoke to pre-filter context_management before the safety-net allowlist, preserving only compact_20260112 edits and auto-injecting the compact-2026-01-12 beta header; the hook is inserted at the right point in transform_anthropic_messages_request |
| litellm/types/llms/bedrock.py | Adds context_management: dict to BedrockInvokeAnthropicMessagesRequest so the pre-filtered field passes the safety-net allowlist; well-documented with a comment explaining the invariant |
| tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py | Adds two new unit tests for compact edge cases and updates docstrings on an existing test; no real-network calls, all mock-based, and no existing assertions were weakened |
Reviews (1): Last reviewed commit: "fix(bedrock/messages): preserve compact_..." | Re-trigger Greptile
| cm = anthropic_messages_request.get("context_management") | ||
| if not isinstance(cm, dict): | ||
| return | ||
| edits = cm.get("edits") |
There was a problem hiding this comment.
When
context_management is present but not a dict (e.g. a string or None due to a client bug), the function returns early and leaves the value in anthropic_messages_request untouched. Before this PR the safety-net allowlist would have stripped the field; now that context_management is in the allowlist it passes through to Bedrock verbatim, which would cause a 400. Popping it instead of returning early preserves the previous defensive behavior.
| cm = anthropic_messages_request.get("context_management") | |
| if not isinstance(cm, dict): | |
| return | |
| edits = cm.get("edits") | |
| cm = anthropic_messages_request.get("context_management") | |
| if not isinstance(cm, dict): | |
| anthropic_messages_request.pop("context_management", None) | |
| return | |
| edits = cm.get("edits") |
f8b078b
into
BerriAI:shin_agent_oss_staging_05_09_2026
|
🤖 litellm-agent: Squash-merged into staging branch Triage Summary 137 lines across 3 files (+132 / -5) Merge Confidence: 5/5 ✅ READY All checks green. Greptile 4/5, no blocking pattern findings, no CircleCI runs (OSS-typical). |
…n /v1/messages (BerriAI#27534) Squash-merged by litellm-agent from Anai-Guo's PR.
Summary
Fixes #27532
Bedrock InvokeModel supports Anthropic compaction (
compact-2026-01-12beta) whencontext_managementis sent with the matchinganthropic-betaheader. Only the Converse API rejects it. The current/v1/messages→ Bedrock Invoke transformation inanthropic_claude3_transformation.pystripscontext_managementunconditionally via the allowlist safety net, so:context_management.edits=[{type: compact_20260112}]lose compaction silently, or"context_management: Extra inputs are not permitted"because the beta header isn't auto-injected.Fix
Pre-filter
context_managementbefore the existing safety-net strip intransform_anthropic_messages_request:type=compact_20260112(the only Bedrock-supported edit type).compact-2026-01-12intoanthropic_betawhen any compact edits survive — same pattern as Vertex AI's_add_context_management_beta_headers.clear_thinking_20251015(which are consumed via thinking injection elsewhere) stay out of the outgoing body.context_management: dicttoBedrockInvokeAnthropicMessagesRequestso the filtered field passes the allowlist.anthropic_beta_headers_config.jsonalready mapscompact-2026-01-12 → compact-2026-01-12for thebedrockprovider, so no further header config change is needed.Test plan
test_bedrock_messages_strips_context_management(existing, clear_thinking-only) — still strips, since no compact edits survive the filter.test_bedrock_messages_allowlist_filters_anthropic_only_fields(existing, empty edits list) — still strips.test_bedrock_messages_preserves_compact_context_management_and_adds_beta(new) — compact edits stay in body andcompact-2026-01-12is added toanthropic_beta.test_bedrock_messages_filters_unsupported_context_management_edits(new) — mixed list keeps only compact edits and adds the beta.🤖 Generated with Claude Code