fix(bedrock-invoke): retain clear_tool_uses_20250919 context_management edits and emit context-management-2025-06-27 beta (LIT-3393) - #32658
Conversation
…nt edits and emit context-management-2025-06-27 beta (LIT-3393) Copy of #29206 by oss-agent-shin, rebased onto litellm_internal_staging so CircleCI can run. Bedrock InvokeModel supports automatic tool-call clearing (clear_tool_uses_20250919) under the context-management-2025-06-27 beta, but LiteLLM stripped the edit and dropped the beta header, causing a Bedrock 400. This maps bedrock.context-management-2025-06-27 to itself in anthropic_beta_headers_config.json (bedrock_converse stays null) and rewrites _filter_context_management_for_bedrock_invoke around an allowlist of supported edit types that keeps each supported edit and adds its matching beta.
|
|
|
Generated by Claude Code |
Greptile SummaryThis PR fixes a two-part bug where
Confidence Score: 5/5Safe to merge — both the JSON config and the transformation logic are narrowly scoped to Bedrock InvokeModel and leave Converse unchanged. The fix is a two-line JSON change plus a small, well-documented allowlist expansion. The class-level dict is accessed correctly from the static method. The fixture teardown properly resets module state. No pre-existing behaviour is altered outside the targeted code path. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/anthropic_beta_headers_config.json | Changes bedrock mapping for context-management-2025-06-27 from null to the header string, allowing filter_and_transform_beta_headers to pass it through for Bedrock InvokeModel; bedrock_converse correctly stays null. |
| litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py | Extends _filter_context_management_for_bedrock_invoke from a compact-only allowlist to a two-entry dict covering compact_20260112 and clear_tool_uses_20250919, each mapped to its required beta; class-level constant _BEDROCK_INVOKE_SUPPORTED_CONTEXT_MANAGEMENT_EDITS is clean and correctly accessed from the static method via the class name. |
| tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py | Adds four new tests behind a shared local_beta_headers_config fixture that correctly handles module-state setup and teardown (reload_beta_headers_config called both before yield and after); previous concern about missing teardown is fully addressed. |
Reviews (3): Last reviewed commit: "test(bedrock-invoke): restore beta-heade..." | Re-trigger Greptile
Greptile SummaryThis PR fixes a two-part bug where
Confidence Score: 4/5The production code change is correct and well-scoped; only the test teardown pattern is inconsistent. The allowlist-based rewrite and the JSON config change are both small and correct — the enum values resolve to the right strings, and beta_set.update() with a generator of strings works as intended. The only gap is that three of the four new tests do not call reload_beta_headers_config() after the test ends, so a mid-test failure leaves the global module cache in local mode for any subsequent tests that run in the same process. The three new test functions that lack a try/finally teardown in test_anthropic_claude3_transformation.py (lines 2095-2232).
|
| Filename | Overview |
|---|---|
| litellm/anthropic_beta_headers_config.json | Changes bedrock.context-management-2025-06-27 from null to "context-management-2025-06-27" so the beta is no longer filtered out for Bedrock InvokeModel; bedrock_converse correctly remains null. |
| litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py | Rewrites _filter_context_management_for_bedrock_invoke around a _BEDROCK_INVOKE_SUPPORTED_CONTEXT_MANAGEMENT_EDITS allowlist; clear_tool_uses_20250919 now survives alongside compact_20260112, and each retained edit contributes its matching beta header via beta_set.update(...). |
| tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py | Adds four new unit tests covering the LIT-3393 scenarios; the first three tests lack the try/finally cleanup pattern (calling reload_beta_headers_config()) that the fourth test correctly uses, risking stale module state for subsequent tests on failure. |
Reviews (2): Last reviewed commit: "fix(bedrock-invoke): retain clear_tool_u..." | Re-trigger Greptile
… fixture in LIT-3393 tests Greptile flagged that three of the four new tests reloaded the module-level beta-headers config into local mode without restoring it on teardown, leaking state into later tests in the same process. Move setup/teardown into a local_beta_headers_config fixture used by all four tests.
|
Generated by Claude Code |
|
bugbot run Generated by Claude Code |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 0568931. Configure here.
Relevant issues
Copy of #29206 by @oss-agent-shin, moved to an in-repo litellm_ branch so CircleCI can run. Sister to the compaction fix in #27532
Linear ticket
Resolves LIT-3393
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
/v1/messages->bedrock/invoke/...requests that carrycontext_management.edits = [{"type": "clear_tool_uses_20250919"}]had that edit silently stripped from the InvokeModel body, even though Bedrock InvokeModel supports automatic tool call clearing under thecontext-management-2025-06-27beta (see the AWS docs)The bug is entirely in request construction, so the proof drives
AmazonAnthropicClaudeMessagesConfig.transform_anthropic_messages_requestformodel="anthropic.claude-haiku-4-5-20251001-v1:0"and prints thecontext_managementandanthropic_betafields of the exact InvokeModel body LiteLLM would sign and POST to Bedrock. No live AWS call is needed to observe the strip, matching the evidence approach on #29206. Before was captured atlitellm_internal_stagingHEAD131aa050bb, after at6761d19da8(this PR)Before, at
131aa050bb:After, at
6761d19da8:Note the third scenario: the LiteLLM internal
clear_thinking_20251015edit is still stripped (it is consumed via thinking injection in_ensure_thinking_for_clear_thinking_context_management), while the Bedrock supportedclear_tool_uses_20250919now survives alongside its betaUnit tests:
Type
🐛 Bug Fix
Changes
litellm/anthropic_beta_headers_config.jsonmapsbedrock.context-management-2025-06-27to"context-management-2025-06-27"instead ofnull, sofilter_and_transform_beta_headers(..., provider="bedrock")stops dropping the beta the transformation adds.bedrock_conversestaysnullbecause per the AWS docs the Converse API genuinely lacks this beta_filter_context_management_for_bedrock_invokeinlitellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.pyis rewritten around a small allowlist of Bedrock InvokeModel supported edit types,compact_20260112->compact-2026-01-12andclear_tool_uses_20250919->context-management-2025-06-27. Each supported edit is kept and its matching beta added to the beta set;context_managementis dropped entirely when nothing survivesFour new tests cover the retained clear_tool_uses edit plus beta, the mixed compact plus clear_tool_uses case, clear_thinking still being stripped while clear_tool_uses survives, and a regression guard locking the JSON mapping in place (pinned to the bundled config via
LITELLM_LOCAL_ANTHROPIC_BETA_HEADERS). After the first Greptile review, the four tests' beta-headers config setup/teardown moved into a sharedlocal_beta_headers_configfixture so the module-level config cache is restored after each testTwo small deviations from #29206 while rebasing onto
litellm_internal_staging, with identical observable behavior: the allowlist dict references the existingANTHROPIC_BETA_HEADER_VALUESenum members instead of hardcoded strings, and the retained edit filtering is a comprehension rather than an accumulate loopNote
Medium Risk
Changes only Bedrock Invoke
/v1/messagesrequest shaping and beta header mapping; scope is narrow with regression tests, but wrong filtering could still break or mis-forward context-management payloads to AWS.Overview
Fixes Bedrock InvokeModel request building so
clear_tool_uses_20250919context_managementedits are forwarded (with thecontext-management-2025-06-27beta) instead of being dropped like unsupported edits.anthropic_beta_headers_config.jsonnow mapsbedrock.context-management-2025-06-27to a non-null value sofilter_and_transform_beta_headersno longer strips that header for Invoke;bedrock_conversestays unsupported._filter_context_management_for_bedrock_invokeis generalized from compact-only filtering to an allowlist:compact_20260112andclear_tool_uses_20250919, each paired with its beta;clear_thinking_20251015remains internal-only and is still removed from the body.New unit tests cover clear-tool-uses-only, mixed compact + clear-tool-uses, clear-thinking stripped alongside clear-tool-uses, and the bedrock beta mapping regression guard.
Reviewed by Cursor Bugbot for commit 0568931. Bugbot is set up for automated code reviews on this repo. Configure here.