Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

refactor(prompt_templates): share the tool use id sanitizer between the Anthropic and Bedrock paths

mateo-berri299cd084f9e9
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,23 +1500,20 @@ 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_-]+$
+_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
- 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
+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
-_BEDROCK_TOOL_USE_ID_MAX_LEN: Final = 64
-_BEDROCK_TOOL_USE_ID_HASH_LEN: Final = 8
+
+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:
@@ -1525,7 +1522,7 @@ def _sanitize_bedrock_tool_use_id(tool_use_id: str) -> str:
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)
+ ("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