From e77f87806100e7fed5e024805f79b20117c9d967 Mon Sep 17 00:00:00 2001 From: Lance Kuo Date: Mon, 25 May 2026 22:53:23 -0400 Subject: [PATCH] fix(types): revert make_valid_bedrock_tool_use_id to str -> str CI mypy failed on the Optional[str] annotation greptile suggested in #28825: factory.py:4033 - Incompatible types (expression has type 'str | None', TypedDict item 'toolUseId' has type 'str') (and two more at the same call sites) Downstream BedrockToolUseBlock and BedrockToolResultBlock require str. The sibling make_valid_bedrock_tool_name has the same None-passthrough and the same str -> str annotation for the same reason; keeping symmetry. Restored the # type: ignore[arg-type] on the empty-passthrough test with a comment explaining the signature trade-off. --- litellm/litellm_core_utils/prompt_templates/factory.py | 4 +--- .../test_litellm_core_utils_prompt_templates_factory.py | 7 ++++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.py index f42c912ee14..e95718469a6 100644 --- a/litellm/litellm_core_utils/prompt_templates/factory.py +++ b/litellm/litellm_core_utils/prompt_templates/factory.py @@ -5361,9 +5361,7 @@ def make_valid_bedrock_tool_name(input_tool_name: str) -> str: return valid_string -def make_valid_bedrock_tool_use_id( - input_tool_use_id: Optional[str], -) -> Optional[str]: +def make_valid_bedrock_tool_use_id(input_tool_use_id: str) -> str: """ Replaces any invalid characters in the input tool_use_id with underscores so it matches Bedrock Converse's required pattern ``^[a-zA-Z0-9_.:-]+$``. 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 eeb59a3e0c1..2796ed5ee9b 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 @@ -2818,7 +2818,12 @@ def test_make_valid_bedrock_tool_use_id_empty_passthrough(): ) assert make_valid_bedrock_tool_use_id("") == "" - assert make_valid_bedrock_tool_use_id(None) is None + # The signature is ``str -> str`` to match downstream TypedDict callers + # (``BedrockToolUseBlock.toolUseId``); the helper still defensively returns + # the input unchanged when None slips in. Suppress the resulting mypy + # error here — annotating the helper as ``Optional[str]`` would force a + # cascade of ``cast(str, ...)`` calls at the use sites. + assert make_valid_bedrock_tool_use_id(None) is None # type: ignore[arg-type] def test_bedrock_tool_call_invoke_sanitizes_drifted_name_and_id():