Skip to content

fix(bedrock-invoke): retain clear_tool_uses_20250919 context_management edits and emit context-management-2025-06-27 beta (LIT-3393) - #32658

Merged
mateo-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_lit_3393_bedrock_clear_tool_uses
Jul 9, 2026
Merged

fix(bedrock-invoke): retain clear_tool_uses_20250919 context_management edits and emit context-management-2025-06-27 beta (LIT-3393)#32658
mateo-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_lit_3393_bedrock_clear_tool_uses

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

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

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to 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 carry context_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 the context-management-2025-06-27 beta (see the AWS docs)

The bug is entirely in request construction, so the proof drives AmazonAnthropicClaudeMessagesConfig.transform_anthropic_messages_request for model="anthropic.claude-haiku-4-5-20251001-v1:0" and prints the context_management and anthropic_beta fields 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 at litellm_internal_staging HEAD 131aa050bb, after at 6761d19da8 (this PR)

Before, at 131aa050bb:

===== clear_tool_uses only =====
{
  "context_management": "<absent>",
  "anthropic_beta": "<absent>"
}
===== compact + clear_tool_uses (mixed) =====
{
  "context_management": {
    "edits": [
      {
        "type": "compact_20260112"
      }
    ]
  },
  "anthropic_beta": [
    "compact-2026-01-12"
  ]
}
===== clear_thinking + clear_tool_uses =====
{
  "context_management": "<absent>",
  "anthropic_beta": "<absent>"
}

After, at 6761d19da8:

===== clear_tool_uses only =====
{
  "context_management": {
    "edits": [
      {
        "type": "clear_tool_uses_20250919"
      }
    ]
  },
  "anthropic_beta": [
    "context-management-2025-06-27"
  ]
}
===== compact + clear_tool_uses (mixed) =====
{
  "context_management": {
    "edits": [
      {
        "type": "compact_20260112"
      },
      {
        "type": "clear_tool_uses_20250919"
      }
    ]
  },
  "anthropic_beta": [
    "compact-2026-01-12",
    "context-management-2025-06-27"
  ]
}
===== clear_thinking + clear_tool_uses =====
{
  "context_management": {
    "edits": [
      {
        "type": "clear_tool_uses_20250919"
      }
    ]
  },
  "anthropic_beta": [
    "context-management-2025-06-27"
  ]
}

Note the third scenario: the LiteLLM internal clear_thinking_20251015 edit is still stripped (it is consumed via thinking injection in _ensure_thinking_for_clear_thinking_context_management), while the Bedrock supported clear_tool_uses_20250919 now survives alongside its beta

Unit tests:

$ python -m pytest tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py -q
77 passed in 3.83s

$ python -m pytest tests/test_litellm/test_anthropic_beta_headers_filtering.py -q
22 passed, 3 warnings in 0.87s

Type

🐛 Bug Fix

Changes

litellm/anthropic_beta_headers_config.json maps bedrock.context-management-2025-06-27 to "context-management-2025-06-27" instead of null, so filter_and_transform_beta_headers(..., provider="bedrock") stops dropping the beta the transformation adds. bedrock_converse stays null because per the AWS docs the Converse API genuinely lacks this beta

_filter_context_management_for_bedrock_invoke in litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py is rewritten around a small allowlist of Bedrock InvokeModel supported edit types, compact_20260112 -> compact-2026-01-12 and clear_tool_uses_20250919 -> context-management-2025-06-27. Each supported edit is kept and its matching beta added to the beta set; context_management is dropped entirely when nothing survives

Four 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 shared local_beta_headers_config fixture so the module-level config cache is restored after each test

Two small deviations from #29206 while rebasing onto litellm_internal_staging, with identical observable behavior: the allowlist dict references the existing ANTHROPIC_BETA_HEADER_VALUES enum members instead of hardcoded strings, and the retained edit filtering is a comprehension rather than an accumulate loop


Note

Medium Risk
Changes only Bedrock Invoke /v1/messages request 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_20250919 context_management edits are forwarded (with the context-management-2025-06-27 beta) instead of being dropped like unsupported edits.

anthropic_beta_headers_config.json now maps bedrock.context-management-2025-06-27 to a non-null value so filter_and_transform_beta_headers no longer strips that header for Invoke; bedrock_converse stays unsupported.

_filter_context_management_for_bedrock_invoke is generalized from compact-only filtering to an allowlist: compact_20260112 and clear_tool_uses_20250919, each paired with its beta; clear_thinking_20251015 remains 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.

…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.
@CLAassistant

CLAassistant commented Jul 9, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ mateo-berri
❌ oss-agent-shin
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Contributor Author

@greptileai


Generated by Claude Code

@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a two-part bug where clear_tool_uses_20250919 context-management edits were silently stripped from Bedrock InvokeModel requests and the matching context-management-2025-06-27 beta header was simultaneously filtered out, causing Bedrock to return a 400. Both halves of the fix are targeted and minimally scoped.

  • The JSON config entry for bedrock.context-management-2025-06-27 is changed from null to its string value so filter_and_transform_beta_headers stops dropping the header; bedrock_converse stays null per AWS docs.
  • _filter_context_management_for_bedrock_invoke is rewritten around a two-entry allowlist (compact_20260112, clear_tool_uses_20250919), each mapped to its required beta; clear_thinking_20251015 remains intentionally absent and filtered.
  • Four new tests use a shared fixture that properly calls reload_beta_headers_config() in both setup and teardown, preventing module-state leakage between test runs.

Confidence Score: 5/5

Safe 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.

Important Files Changed

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-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a two-part bug where clear_tool_uses_20250919 context-management edits were silently stripped from Bedrock InvokeModel requests and the corresponding context-management-2025-06-27 beta header was filtered out. The fix correctly preserves supported edits and adds their betas.

  • _filter_context_management_for_bedrock_invoke is rewritten around a _BEDROCK_INVOKE_SUPPORTED_CONTEXT_MANAGEMENT_EDITS allowlist that maps each supported edit type to its required beta; compact_20260112 and clear_tool_uses_20250919 are both retained, clear_thinking_20251015 is still stripped as LiteLLM-internal.
  • anthropic_beta_headers_config.json flips bedrock.context-management-2025-06-27 from null to its value so filter_and_transform_beta_headers no longer drops it; bedrock_converse stays null per the AWS docs.
  • Four new tests cover the key scenarios; the first three lack the try/finally / reload_beta_headers_config() teardown that the fourth test uses, leaving stale module state after a failure.

Confidence Score: 4/5

The 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).

Important Files Changed

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.

Copy link
Copy Markdown
Contributor Author

@greptileai


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

bugbot run


Generated by Claude Code

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ 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.

@codspeed-hq

codspeed-hq Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_lit_3393_bedrock_clear_tool_uses (0568931) with litellm_internal_staging (131aa05)

Open in CodSpeed

@mateo-berri
mateo-berri merged commit 41e9cc4 into litellm_internal_staging Jul 9, 2026
131 checks passed
@mateo-berri
mateo-berri deleted the litellm_lit_3393_bedrock_clear_tool_uses branch July 9, 2026 21:31
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.

4 participants