Skip to content

fix(bedrock): preserve tool choice for parallel tool calls - #28508

Closed
fuchengwarrenzhu wants to merge 1 commit into
BerriAI:mainfrom
fuchengwarrenzhu:fix/bedrock-parallel-tool-choice-type
Closed

fuchengwarrenzhu wants to merge 1 commit into
BerriAI:mainfrom
fuchengwarrenzhu:fix/bedrock-parallel-tool-choice-type

Conversation

@fuchengwarrenzhu

Copy link
Copy Markdown

Relevant issues

Fixes #22637

Related stale PR: #22638

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Screenshots / Proof of Fix

Focused local checks:

uv run pytest tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py -k parallel_tool_calls -q
# 6 passed, 119 deselected in 0.22s

uv run ruff check litellm/llms/bedrock/chat/converse_transformation.py
# All checks passed

git diff --check
# passed

I did not run full make test-unit.

uv run ruff check tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py currently reports unrelated pre-existing unused imports / print statements elsewhere in that file, so this PR keeps the lint scope to the changed source file and the focused test run.

Type

🐛 Bug Fix
✅ Test

Changes

This fixes the Bedrock Claude 4.5+ parallel_tool_calls request shape for additionalModelRequestFields.tool_choice.

Current main still creates:

{"tool_choice": {"disable_parallel_tool_use": bool}}

Anthropic's Bedrock extension requires a type discriminator, otherwise Bedrock/Anthropic rejects the request with tool_choice.type: Field required.

This PR:

  • adds the missing Anthropic type for parallel_tool_calls=True and parallel_tool_calls=False;
  • preserves explicit caller tool_choice intent when parallel_tool_calls is also present:
    • tool_choice="auto" -> {"type": "auto", ...};
    • tool_choice="required" -> {"type": "any", ...};
    • named tool choice -> {"type": "tool", "name": "...", ...};
  • removes the duplicate Bedrock-native toolChoice from toolConfig when that explicit choice has been moved into additionalModelRequestFields.tool_choice;
  • adds focused Bedrock Converse transformation tests under tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py.

Difference from #22638: that PR adds type: "auto" for the base bug, but it does not preserve explicit tool_choice="required" or named-tool choices when combined with parallel_tool_calls, and it is currently merge-conflicting.

Preserve explicit Bedrock tool_choice intent when moving parallel_tool_calls
configuration into Anthropic's Bedrock extension.
@CLAassistant

CLAassistant commented May 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@fuchengwarrenzhu

Copy link
Copy Markdown
Author

@greptileai

@codspeed

codspeed Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing fuchengwarrenzhu:fix/bedrock-parallel-tool-choice-type (b2b9d95) with main (79b4578)

Open in CodSpeed

@fuchengwarrenzhu

Copy link
Copy Markdown
Author

Superseded by #28509.

LiteLLM's branch guard rejects external fork PRs targeting main, so I reopened this against litellm_oss_staging with the same patch and focused tests.

@codecov

codecov Bot commented May 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...tellm/llms/bedrock/chat/converse_transformation.py 88.23% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@greptile-apps

greptile-apps Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a bug in Bedrock's Converse transformation where parallel_tool_calls produced a tool_choice object missing the required Anthropic type discriminator, causing Bedrock/Anthropic to reject requests with tool_choice.type: Field required. It also correctly removes the duplicate Bedrock-native toolChoice from toolConfig when the parallel-tool extension is active.

  • Adds _map_bedrock_tool_choice_to_anthropic_tool_choice to convert the Bedrock ToolChoiceValuesBlock format to the Anthropic extension format ({\"type\": \"auto|any|tool\", ...}), then merges it with the disable_parallel_tool_use flag before placing it in additionalModelRequestFields.
  • Pops tool_choice from inference_params early (inside _prepare_request_params) for Claude 4.5+ on Bedrock so the downstream toolConfig builder receives None and leaves toolChoice unset, avoiding the dual-field conflict.
  • Adds focused unit tests covering parallel_tool_calls=True/False with auto, required, and named-tool choices, though the parallel_tool_calls=True + explicit tool_choice combination is not yet tested.

Confidence Score: 4/5

The change is well-scoped: it only activates for Claude 4.5+ on Bedrock and only when parallel_tool_calls is set, leaving all other call paths untouched.

The core mapping and merge logic is correct for all combinations covered by the tests. The one gap is that parallel_tool_calls=True combined with an explicit tool_choice is not tested, and test_parallel_tool_calls_true_includes_type_field omits the toolChoice-absent-from-toolConfig assertion. These are test coverage gaps rather than defects in the production code path.

The test file would benefit from an additional parametrized case for parallel_tool_calls=True with an explicit tool_choice, and the true test should assert toolChoice is absent from toolConfig.

Important Files Changed

Filename Overview
litellm/llms/bedrock/chat/converse_transformation.py Adds _map_bedrock_tool_choice_to_anthropic_tool_choice helper and uses it to inject the required Anthropic type discriminator into additionalModelRequestFields.tool_choice, then pops the Bedrock-native toolChoice from toolConfig to avoid dual-field conflicts. Logic is correct for all mapped cases.
tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py Adds focused Bedrock Converse parallel-tool-calls tests; covers parallel_tool_calls=True (no explicit tool_choice) and parallel_tool_calls=False with auto/required/named-tool choices, but misses the parallel_tool_calls=True + explicit tool_choice combination.

Reviews (1): Last reviewed commit: "Fix Bedrock parallel tool choice type" | Re-trigger Greptile

Comment on lines +3794 to +3831
assert "parallel_tool_calls" not in request_data["additionalModelRequestFields"]


@pytest.mark.parametrize(
("openai_tool_choice", "expected_anthropic_tool_choice"),
[
(
"auto",
{"type": "auto", "disable_parallel_tool_use": True},
),
(
"required",
{"type": "any", "disable_parallel_tool_use": True},
),
(
{"type": "function", "function": {"name": "get_weather"}},
{
"type": "tool",
"name": "get_weather",
"disable_parallel_tool_use": True,
},
),
],
ids=["auto", "required", "named-tool"],
)
def test_parallel_tool_calls_moves_explicit_tool_choice_into_anthropic_extension(
openai_tool_choice,
expected_anthropic_tool_choice,
):
"""Explicit tool_choice should not conflict with Anthropic's parallel tool extension."""
config = AmazonConverseConfig()
model = "anthropic.claude-sonnet-4-5-20250929-v1:0"
messages = [{"role": "user", "content": "What's the weather in SF and NYC?"}]

optional_params = config.map_openai_params(
non_default_params={
"tools": _TOOL_PARAM,
"tool_choice": openai_tool_choice,

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.

P2 Missing coverage: parallel_tool_calls=True + explicit tool_choice

The parametrized test exercises every explicit tool_choice value, but only with parallel_tool_calls=False. The complementary case — parallel_tool_calls=True combined with, for example, tool_choice="required" — is not tested. The expected merged output would be {"type": "any", "disable_parallel_tool_use": False} and toolChoice should again be absent from toolConfig. Without this variant, a regression in the {**anthropic_tool_choice, **value} merge when disable_parallel_tool_use is False would go undetected.

test_parallel_tool_calls_true_includes_type_field also does not assert that toolChoice is absent from toolConfig, which is the other half of the contract this PR establishes.

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.

2 participants