fix(bedrock): align toolUse/toolSpec names and allow hyphens#28874
Conversation
…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 SummaryFixes a multi-turn Bedrock Converse breakage where
Confidence Score: 5/5Localized 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.
|
| 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
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>
f17dfcd
into
litellm_internal_staging
Summary
toolUse.namein message history viamake_valid_bedrock_tool_name(same astoolSpec.name) so multi-turn Bedrock Converse requests no longer send mismatched names likemy_toolvsmy-tool.[a-zA-Z0-9_-]+.tool_choice.function.namefor 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_usepytest 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_namepytest tests/llm_translation/test_bedrock_completion.py::test_bedrock_tools_pt_invalid_names