From 299cd084f9e90e2c4e31024e45a599fdb0395773 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 12 Sep 2026 20:29:16 -0700 Subject: [PATCH] refactor(prompt_templates): share the tool use id sanitizer between the Anthropic and Bedrock paths --- .../prompt_templates/factory.py | 29 +++++++++---------- ...llm_core_utils_prompt_templates_factory.py | 17 +++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index f7f4a964c9b..d61c3235c5e 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -1500,32 +1500,29 @@ def convert_to_gemini_tool_call_result( return _part -def _sanitize_anthropic_tool_use_id(tool_use_id: str) -> str: - """ - Sanitize tool_use_id to match Anthropic's required pattern: ^[a-zA-Z0-9_-]+$ - - Anthropic requires tool_use_id to only contain alphanumeric characters, underscores, and hyphens. - This function replaces any invalid characters with underscores. - """ - # Replace any character that's not alphanumeric, underscore, or hyphen with underscore - sanitized = re.sub(r"[^a-zA-Z0-9_-]", "_", tool_use_id) - # Ensure it's not empty (fallback to a default if needed) - if not sanitized: - sanitized = "tool_use_id" - return sanitized - - +_TOOL_USE_ID_FALLBACK: Final = "tool_use_id" +_ANTHROPIC_TOOL_USE_ID_INVALID_CHARS: Final = re.compile(r"[^a-zA-Z0-9_-]") +_BEDROCK_TOOL_USE_ID_INVALID_CHARS: Final = re.compile(r"[^a-zA-Z0-9_.:-]") _BEDROCK_TOOL_USE_ID_MAX_LEN: Final = 64 _BEDROCK_TOOL_USE_ID_HASH_LEN: Final = 8 +def _replace_invalid_tool_use_id_chars(tool_use_id: str, invalid_chars: re.Pattern[str]) -> str: + return invalid_chars.sub("_", tool_use_id) or _TOOL_USE_ID_FALLBACK + + +def _sanitize_anthropic_tool_use_id(tool_use_id: str) -> str: + """Anthropic requires tool_use_id to match ^[a-zA-Z0-9_-]+$.""" + return _replace_invalid_tool_use_id_chars(tool_use_id, _ANTHROPIC_TOOL_USE_ID_INVALID_CHARS) + + def _sanitize_bedrock_tool_use_id(tool_use_id: str) -> str: """ Bedrock Converse requires toolUseId to match [a-zA-Z0-9_.:-]+ and be at most 64 chars. Ids that need rewriting get a short hash of the original appended so two ids that only differ in a replaced char or past the cut still map to distinct values. """ - sanitized: Final = re.sub(r"[^a-zA-Z0-9_.:-]", "_", tool_use_id) or "tool_use_id" + sanitized: Final = _replace_invalid_tool_use_id_chars(tool_use_id, _BEDROCK_TOOL_USE_ID_INVALID_CHARS) if sanitized == tool_use_id and len(sanitized) <= _BEDROCK_TOOL_USE_ID_MAX_LEN: return sanitized digest: Final = hashlib.sha256(tool_use_id.encode()).hexdigest()[:_BEDROCK_TOOL_USE_ID_HASH_LEN] diff --git a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py index fe8a9bd5205..034062826f6 100644 --- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py +++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py @@ -20,6 +20,7 @@ from litellm.litellm_core_utils.prompt_templates.factory import ( _convert_to_bedrock_tool_call_invoke, _convert_to_bedrock_tool_call_result, anthropic_messages_pt, + convert_to_anthropic_tool_result, convert_to_gemini_tool_call_result, make_valid_bedrock_tool_name, ollama_pt, @@ -2219,6 +2220,7 @@ _BEDROCK_TOOL_USE_ID_RE = re.compile(r"^[a-zA-Z0-9_.:-]{1,64}$") "call|with|pipes", "call_" + "y" * 60 + "|end", "call:ok.dots-and_under", + "", ], ) def test_bedrock_tool_use_id_is_sanitized_consistently_for_invoke_and_result(tool_call_id): @@ -2291,6 +2293,21 @@ def test_bedrock_tool_call_invoke_concatenated_json_long_id_stays_within_limit() assert all(_BEDROCK_TOOL_USE_ID_RE.match(i) for i in ids) +@pytest.mark.parametrize( + ("tool_call_id", "expected"), + [ + ("call|with|pipes", "call_with_pipes"), + ("call:ok.dots", "call_ok_dots"), + ("call_" + "x" * 100, "call_" + "x" * 100), + ("toolu_01AbC-xyz", "toolu_01AbC-xyz"), + ("", "tool_use_id"), + ], +) +def test_anthropic_tool_use_id_keeps_pattern_only_rewrite_with_no_cap_or_hash(tool_call_id, expected): + result = convert_to_anthropic_tool_result({"role": "tool", "tool_call_id": tool_call_id, "content": "ok"}) + assert result["tool_use_id"] == expected + + def test_bedrock_tool_call_invoke_concatenated_json(): """ Tool call whose arguments contain multiple concatenated JSON objects