diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.py index 833ae206024..773b289bb09 100644 --- a/litellm/llms/openai/responses/transformation.py +++ b/litellm/llms/openai/responses/transformation.py @@ -381,8 +381,19 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): item_type: Final = item.get("type") item_id: Final = item.get("id") genuine_prefix: Final = TOOL_CALL_ITEM_ID_PREFIX_BY_TYPE.get(item_type) if isinstance(item_type, str) else None - if genuine_prefix is None or not isinstance(item_id, str) or item_id.startswith(genuine_prefix): + if genuine_prefix is None or not isinstance(item_id, str): return item + from litellm.litellm_core_utils.prompt_templates.factory import ( + THOUGHT_SIGNATURE_SEPARATOR, + ) + + clean_id: Final = ( + item_id.split(THOUGHT_SIGNATURE_SEPARATOR, 1)[0] if THOUGHT_SIGNATURE_SEPARATOR in item_id else item_id + ) + if clean_id.startswith(genuine_prefix) and len(clean_id) <= 64: + if clean_id == item_id: + return item + return {**item, "id": clean_id} # mutable-ok: outgoing JSON request item return {key: value for key, value in item.items() if key != "id"} # mutable-ok: outgoing JSON request item def _sanitized_tool_schemas_for_openai( diff --git a/litellm/responses/litellm_completion_transformation/custom_tools.py b/litellm/responses/litellm_completion_transformation/custom_tools.py index 7888a07e248..0f508ed60af 100644 --- a/litellm/responses/litellm_completion_transformation/custom_tools.py +++ b/litellm/responses/litellm_completion_transformation/custom_tools.py @@ -15,6 +15,7 @@ extending this module without touching the streaming iterator or transformation logic. """ +import hashlib import json from collections.abc import Mapping, Sequence from types import MappingProxyType @@ -34,9 +35,22 @@ TOOL_CALL_ITEM_ID_PREFIX_BY_TYPE: Final = MappingProxyType({"function_call": "fc def openai_shaped_tool_call_item_id(item_type: str, tool_id: str) -> str: prefix: Final = TOOL_CALL_ITEM_ID_PREFIX_BY_TYPE.get(item_type) - if prefix is None or not tool_id or tool_id.startswith(prefix): + if prefix is None or not tool_id: return tool_id - return f"{prefix}_{tool_id}" + from litellm.litellm_core_utils.prompt_templates.factory import ( + THOUGHT_SIGNATURE_SEPARATOR, + ) + + clean_id: Final = ( + tool_id.split(THOUGHT_SIGNATURE_SEPARATOR, 1)[0] if THOUGHT_SIGNATURE_SEPARATOR in tool_id else tool_id + ) + if not clean_id: + return tool_id + candidate: Final = clean_id if clean_id.startswith(prefix) else f"{prefix}_{clean_id}" + if len(candidate) <= 64: + return candidate + digest: Final = hashlib.sha256(candidate.encode("utf-8")).hexdigest()[:16] + return f"{candidate[:47]}_{digest}" class _ToolNameFields(BaseModel): diff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py index 4cf8767764b..95d6f6d6bcc 100644 --- a/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py +++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_transformation.py @@ -273,6 +273,79 @@ class TestOpenAIResponsesAPIConfig: assert replayed_input[1]["id"] == "toolu_01Foreign" assert replayed_input[3]["id"] == "srvtoolu_01Foreign" + def test_transform_strips_thought_signature_from_tool_call_item_ids(self): + """Replayed tool call items containing Gemini thought signatures in their ids + (e.g., fc_call_xxx__thought__) must have the signature stripped so that + the resulting id conforms to OpenAI's 64-char maximum, while call_id is preserved.""" + from litellm.litellm_core_utils.prompt_templates.factory import ( + THOUGHT_SIGNATURE_SEPARATOR, + ) + + long_sig: Final = "A" * 900 + raw_call_id: Final = f"call_gemini_123{THOUGHT_SIGNATURE_SEPARATOR}{long_sig}" + replayed_input: Final = [ + { + "type": "function_call", + "id": f"fc_{raw_call_id}", + "call_id": raw_call_id, + "name": "get_weather", + "arguments": '{"city": "Paris"}', + }, + { + "type": "custom_tool_call", + "id": f"ctc_{raw_call_id}", + "call_id": raw_call_id, + "name": "run_terminal", + "input": "ls", + }, + { + "type": "function_call_output", + "call_id": raw_call_id, + "output": "sunny", + }, + ] + + result: Final = self.config.transform_responses_api_request( + model=self.model, + input=replayed_input, + response_api_optional_request_params={}, + litellm_params={}, + headers={}, + ) + + assert result["input"][0]["id"] == "fc_call_gemini_123" + assert len(result["input"][0]["id"]) <= 64 + assert result["input"][0]["call_id"] == raw_call_id + assert result["input"][1]["id"] == "ctc_call_gemini_123" + assert len(result["input"][1]["id"]) <= 64 + assert result["input"][1]["call_id"] == raw_call_id + assert result["input"][2]["call_id"] == raw_call_id + + def test_transform_drops_oversized_tool_call_item_ids(self): + """Replayed tool call items with genuine prefixes whose ids exceed OpenAI's 64-char + maximum (without thought signatures) must have the id dropped to prevent 400.""" + oversized_id: Final = "fc_" + "x" * 70 + replayed_input: Final = [ + { + "type": "function_call", + "id": oversized_id, + "call_id": "call_123", + "name": "get_weather", + "arguments": "{}", + } + ] + + result: Final = self.config.transform_responses_api_request( + model=self.model, + input=replayed_input, + response_api_optional_request_params={}, + litellm_params={}, + headers={}, + ) + + assert "id" not in result["input"][0] + assert result["input"][0]["call_id"] == "call_123" + def test_transform_keeps_foreign_tool_call_item_ids_for_other_providers(self): """Providers reusing this config that do not enforce OpenAI's id shapes must keep replayed ids untouched.""" diff --git a/tests/test_litellm/responses/test_custom_tool_call.py b/tests/test_litellm/responses/test_custom_tool_call.py index 2ed71ee3ecf..f3d08eab5f6 100644 --- a/tests/test_litellm/responses/test_custom_tool_call.py +++ b/tests/test_litellm/responses/test_custom_tool_call.py @@ -10,7 +10,7 @@ LiteLLM bridge correctly: import json import pytest -from typing import Dict, Any, List +from typing import Any, Dict, Final, List from openai.types.responses import ResponseFunctionToolCall @@ -160,6 +160,65 @@ class TestCustomToolUtilities: assert openai_shaped_tool_call_item_id("function_call", "") == "" assert openai_shaped_tool_call_item_id("message", "toolu_01Abc") == "toolu_01Abc" + def test_openai_shaped_tool_call_item_id_strips_thought_signature(self): + """Gemini thought signatures encoded into tool ids (call_xxx__thought__) + must be stripped from the emitted item id (producing fc_call_xxx <= 64 chars) + so that subsequent turns / fallbacks to OpenAI do not 400 with 'string too long'.""" + from litellm.litellm_core_utils.prompt_templates.factory import ( + THOUGHT_SIGNATURE_SEPARATOR, + ) + + long_sig: Final = "a" * 900 + raw_id: Final = f"call_12345{THOUGHT_SIGNATURE_SEPARATOR}{long_sig}" + shaped_fc_id: Final = openai_shaped_tool_call_item_id("function_call", raw_id) + assert shaped_fc_id == "fc_call_12345" + assert len(shaped_fc_id) <= 64 + + shaped_ctc_id: Final = openai_shaped_tool_call_item_id("custom_tool_call", raw_id) + assert shaped_ctc_id == "ctc_call_12345" + assert len(shaped_ctc_id) <= 64 + + already_prefixed: Final = f"fc_call_12345{THOUGHT_SIGNATURE_SEPARATOR}{long_sig}" + assert openai_shaped_tool_call_item_id("function_call", already_prefixed) == "fc_call_12345" + + long_provider_id: Final = "a" * 62 + shaped_long_id: Final = openai_shaped_tool_call_item_id("function_call", long_provider_id) + assert len(shaped_long_id) == 64 + assert shaped_long_id.startswith("fc_") + + only_sig: Final = f"{THOUGHT_SIGNATURE_SEPARATOR}{long_sig}" + assert openai_shaped_tool_call_item_id("function_call", only_sig) == only_sig + + shared_prefix_id1: Final = ("x" * 61) + "1" + shared_prefix_id2: Final = ("x" * 61) + "2" + shortened1: Final = openai_shaped_tool_call_item_id("function_call", shared_prefix_id1) + shortened2: Final = openai_shaped_tool_call_item_id("function_call", shared_prefix_id2) + assert len(shortened1) == 64 + assert len(shortened2) == 64 + assert shortened1.startswith("fc_") + assert shortened2.startswith("fc_") + assert shortened1 != shortened2 + + def test_without_foreign_tool_call_item_id_strips_thought_signature(self): + from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig + from litellm.litellm_core_utils.prompt_templates.factory import ( + THOUGHT_SIGNATURE_SEPARATOR, + ) + + long_sig: Final = "a" * 900 + raw_id: Final = f"call_12345{THOUGHT_SIGNATURE_SEPARATOR}{long_sig}" + item: Final = {"type": "function_call", "id": f"fc_{raw_id}", "call_id": raw_id} + sanitized: Final = OpenAIResponsesAPIConfig._without_foreign_tool_call_item_id(item) + assert sanitized["id"] == "fc_call_12345" + + oversized_item: Final = {"type": "function_call", "id": "fc_" + ("x" * 70), "call_id": "call_123"} + dropped: Final = OpenAIResponsesAPIConfig._without_foreign_tool_call_item_id(oversized_item) + assert "id" not in dropped + + normal_item: Final = {"type": "function_call", "id": "fc_normal", "call_id": "call_normal"} + kept: Final = OpenAIResponsesAPIConfig._without_foreign_tool_call_item_id(normal_item) + assert kept["id"] == "fc_normal" + def test_build_tool_call_item_kwargs_normalizes_item_id_keeps_call_id(self): """The streaming item id gets the OpenAI shape while call_id stays raw so tool_result pairing (which keys off call_id) keeps working."""