Skip to content

fix(bedrock): align toolUse/toolSpec names and allow hyphens#28874

Merged
mateo-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_bedrock-tool-name-consistency
May 26, 2026
Merged

fix(bedrock): align toolUse/toolSpec names and allow hyphens#28874
mateo-berri merged 2 commits into
litellm_internal_stagingfrom
litellm_bedrock-tool-name-consistency

Conversation

@Sameerlite

@Sameerlite Sameerlite commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Sanitize toolUse.name in message history via make_valid_bedrock_tool_name (same as toolSpec.name) so multi-turn Bedrock Converse requests no longer send mismatched names like my_tool vs my-tool.
  • Allow hyphens in tool names per current Bedrock pattern [a-zA-Z0-9_-]+.
  • Sanitize tool_choice.function.name for forced tool selection.

Test plan

  • pytest tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py::test_bedrock_tool_name_sanitized_consistently_in_tools_and_tool_use
  • pytest tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py::test_bedrock_converse_messages_pt_tool_use_matches_tool_spec_hyphen_name
  • pytest tests/llm_translation/test_bedrock_completion.py::test_bedrock_tools_pt_invalid_names
image curl in litellm: POST Request Sent from LiteLLM: ``` curl -X POST \ https://bedrock-runtime.us-east-1.amazonaws.com/model/global.anthropic.claude-sonnet-4-5-20250929-v1%3A0/converse \ -H 'Content-Type: application/json' -H 'Authorization: Be****0=' -H 'Content-Length: 636' \ -d '{"messages": [{"role": "user", "content": [{"text": "Use my-tool."}]}, {"role": "assistant", "content": [{"toolUse": {"input": {"location": "Boston"}, "name": "my-tool", "toolUseId": "call_abc123"}}]}, {"role": "user", "content": [{"toolResult": {"content": [{"text": "{\"temperature\": 72}"}], "toolUseId": "call_abc123"}}, {"text": "Summarize."}]}], "additionalModelRequestFields": {}, "system": [], "inferenceConfig": {}, "toolConfig": {"tools": [{"toolSpec": {"inputSchema": {"json": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}, "name": "my-tool", "description": "Example tool"}}]}}'

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Localized Bedrock prompt/transformation changes with added regression tests; no auth, billing, or broad API surface impact.
> 
> **Overview**
> **Bedrock Converse tool naming** is aligned so `toolSpec`, `toolUse`, and forced `tool_choice` all go through the same `make_valid_bedrock_tool_name` normalization—fixing multi-turn failures where history used raw names (e.g. `my-tool`) while definitions used sanitized names (e.g. `my_tool`).
> 
> **Hyphens are kept** in valid names per Bedrock’s `[a-zA-Z][a-zA-Z0-9_-]*` pattern (previously hyphens were turned into underscores). Tests cover hyphen preservation, invalid-character sanitization parity between tools and tool calls, and converse message translation.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 200a8bd7f50ece3e8d455dc9ee11e74b9f67d58a. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

…names

Sanitize tool names consistently in tool history and tool_choice, and
preserve hyphens per current Bedrock [a-zA-Z0-9_-]+ constraint.

Co-authored-by: Cursor <cursoragent@cursor.com>
@greptile-apps

greptile-apps Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a multi-turn Bedrock Converse breakage where toolSpec.name and toolUse.name diverged because only the former ran through make_valid_bedrock_tool_name; hyphens are now also kept valid per Bedrock's [a-zA-Z][a-zA-Z0-9_-]* pattern.

  • _convert_to_bedrock_tool_call_invoke in factory.py now wraps the tool name with make_valid_bedrock_tool_name, matching what _bedrock_tools_pt already did for toolSpec.
  • converse_transformation.py applies the same normalization to tool_choice.function.name for forced-tool-selection requests.
  • Existing test assertions updated and three new regression tests added, all using local calls only (no network).

Confidence Score: 5/5

Localized bug fix with clear before/after semantics and dedicated regression tests; safe to merge.

The change is narrowly scoped to Bedrock tool name normalization. Both the core logic and the three call sites (toolSpec, toolUse, tool_choice) are internally consistent after the fix, and the new tests demonstrate the previously-failing multi-turn scenario now works correctly.

No files require special attention.

Important Files Changed

Filename Overview
litellm/litellm_core_utils/prompt_templates/factory.py Updated make_valid_bedrock_tool_name to preserve hyphens and applied it to _convert_to_bedrock_tool_call_invoke so toolUse names are normalized consistently with toolSpec names.
litellm/llms/bedrock/chat/converse_transformation.py Applies make_valid_bedrock_tool_name to tool_choice.function.name so forced tool selection uses the same normalized name as the toolSpec.
tests/llm_translation/test_bedrock_completion.py Existing test assertions updated from a123_invalid_name to a123-invalid_name to reflect the hyphen-preserving behavior; assertions correctly cover both the numeric-prefix fix and invalid-char replacement.
tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py Three new unit tests added: hyphen preservation, parity between toolSpec and toolUse sanitization, and multi-turn message translation; all use only local function calls (no network).

Reviews (2): Last reviewed commit: "docs(bedrock): docstring matches alpha-f..." | Re-trigger Greptile

Comment thread litellm/litellm_core_utils/prompt_templates/factory.py Outdated
@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Greptile: pattern is [a-zA-Z][a-zA-Z0-9_-]* to reflect prepend-'a' behavior.
Co-authored-by: Cursor <cursoragent@cursor.com>
@Sameerlite

Copy link
Copy Markdown
Collaborator Author

@greptileai

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM; thanks!

@mateo-berri mateo-berri merged commit f17dfcd into litellm_internal_staging May 26, 2026
116 of 119 checks passed
@mateo-berri mateo-berri deleted the litellm_bedrock-tool-name-consistency branch May 26, 2026 18:58
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