From 6d8a1fd7e6867c6ae692e013b4578b04702c83a8 Mon Sep 17 00:00:00 2001 From: Grainne Ni She McKnight Date: Thu, 15 Oct 2026 16:12:16 +0200 Subject: [PATCH 1/5] fix: preserve Anthropic tool references in Responses tool outputs --- .../transformation.py | 15 +++--- ...test_function_call_output_normalization.py | 49 +++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 7f5d143e98e..ab5075bb38c 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -92,6 +92,7 @@ from litellm.types.llms.openai import ( ResponsesAPIResponse, ResponsesAPIStatus, ToolChoice, + ToolMessageContentPart, ValidChatCompletionMessageContentTypes, ValidChatCompletionMessageContentTypesLiteral, ) @@ -2046,7 +2047,7 @@ class LiteLLMCompletionResponsesConfig: def _normalize_function_call_output_to_tool_content( output: object, - ) -> str | list[ChatCompletionTextObject | ChatCompletionImageObject]: + ) -> str | list[ToolMessageContentPart]: """ Normalize Responses API function_call_output.output into a shape that downstream chat adapters (esp. Gemini) can reliably consume. @@ -2059,7 +2060,7 @@ class LiteLLMCompletionResponsesConfig: For chat tool messages we normalize to either: - string (preferred) - - list of {"type": "text"|"image_url", ...} blocks (for multimodal tool outputs) + - list of {"type": "text"|"image_url"|"tool_reference", ...} blocks """ if output is None: return "" @@ -2068,7 +2069,7 @@ class LiteLLMCompletionResponsesConfig: # Some adapters represent tool output as a list of "input_*" parts if isinstance(output, list): - normalized_blocks: Final[list[ChatCompletionTextObject | ChatCompletionImageObject]] = [] + normalized_blocks: Final[list[ToolMessageContentPart]] = [] text_acc: Final[list[str]] = [] for part in output: if not isinstance(part, dict): @@ -2079,6 +2080,10 @@ class LiteLLMCompletionResponsesConfig: if isinstance(txt, str) and txt: text_acc.append(txt) normalized_blocks.append({"type": "text", "text": txt}) + elif part_type == "tool_reference": + match part: + case {"tool_name": str(tool_name)} if tool_name: + normalized_blocks.append({"type": "tool_reference", "tool_name": tool_name}) elif part_type in ("input_image", "image_url"): image_url_val = part.get("image_url") or part.get("url") if isinstance(image_url_val, dict): @@ -2093,9 +2098,7 @@ class LiteLLMCompletionResponsesConfig: } ) - # Prefer structured blocks if we have images; otherwise return a string. - if any(b.get("type") == "image_url" for b in normalized_blocks): - # Ensure we include any accumulated text as text blocks too + if any(b["type"] in ("image_url", "tool_reference") for b in normalized_blocks): return normalized_blocks if text_acc: return "".join(text_acc) diff --git a/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py b/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py index 3af1ef51e3e..e293d4626d3 100644 --- a/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py +++ b/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py @@ -6,9 +6,16 @@ as tool/function response parts; if the tool output is passed as a list of input we normalize it to text/image blocks or a string. """ +from collections.abc import Mapping +from typing import Final, cast + +import pytest + +from litellm.litellm_core_utils.prompt_templates.factory import convert_to_anthropic_tool_result from litellm.responses.litellm_completion_transformation.transformation import ( LiteLLMCompletionResponsesConfig, ) +from litellm.types.llms.openai import ChatCompletionToolMessage, ToolMessageContentPart def test_function_call_output_list_input_text_is_converted_to_tool_string_content(): @@ -40,3 +47,45 @@ def test_function_call_output_string_passthrough(): ) assert len(out) == 1 assert out[0]["content"] == '{"ok":true}' + + +@pytest.mark.parametrize( + ("output", "expected_content"), + [ + ( + [{"type": "tool_reference", "tool_name": "get_weather"}], + [{"type": "tool_reference", "tool_name": "get_weather"}], + ), + ( + [ + {"type": "input_text", "text": "Loaded tools"}, + {"type": "tool_reference", "tool_name": "get_weather"}, + {"type": "input_text", "text": " and "}, + {"type": "tool_reference", "tool_name": "get_forecast"}, + ], + [ + {"type": "text", "text": "Loaded tools"}, + {"type": "tool_reference", "tool_name": "get_weather"}, + {"type": "text", "text": " and "}, + {"type": "tool_reference", "tool_name": "get_forecast"}, + ], + ), + ], + ids=["reference-only", "interleaved-text-and-references"], +) +def test_function_call_output_preserves_tool_references_for_anthropic( + output: list[Mapping[str, object]], expected_content: list[ToolMessageContentPart] +) -> None: + messages: Final = ( + LiteLLMCompletionResponsesConfig._transform_responses_api_tool_call_output_to_chat_completion_message( + {"type": "function_call_output", "call_id": "call_load_tools", "output": output} + ) + ) + + assert messages == [{"role": "tool", "tool_call_id": "call_load_tools", "content": expected_content}] + message: Final = cast(ChatCompletionToolMessage, messages[0]) # cast-ok: complete tool message asserted above + assert convert_to_anthropic_tool_result(message) == { + "type": "tool_result", + "tool_use_id": "call_load_tools", + "content": expected_content, + } From ed4862465ebcc5a30d3017a20b8ff50bf52ae05d Mon Sep 17 00:00:00 2001 From: Grainne Ni She McKnight Date: Thu, 15 Oct 2026 16:14:10 +0200 Subject: [PATCH 2/5] fix: handle malformed tool references explicitly --- .../litellm_completion_transformation/transformation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index ab5075bb38c..b953bd4776a 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2084,6 +2084,8 @@ class LiteLLMCompletionResponsesConfig: match part: case {"tool_name": str(tool_name)} if tool_name: normalized_blocks.append({"type": "tool_reference", "tool_name": tool_name}) + case _: + continue elif part_type in ("input_image", "image_url"): image_url_val = part.get("image_url") or part.get("url") if isinstance(image_url_val, dict): From 96dad1871ec1da8e35b2c229ebd0bee16b1e0b15 Mon Sep 17 00:00:00 2001 From: Grainne Ni She McKnight Date: Thu, 15 Oct 2026 16:16:43 +0200 Subject: [PATCH 3/5] fix: narrow reference names without unknown type bindings --- .../transformation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index b953bd4776a..743d3fd088c 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2081,11 +2081,11 @@ class LiteLLMCompletionResponsesConfig: text_acc.append(txt) normalized_blocks.append({"type": "text", "text": txt}) elif part_type == "tool_reference": - match part: - case {"tool_name": str(tool_name)} if tool_name: - normalized_blocks.append({"type": "tool_reference", "tool_name": tool_name}) - case _: - continue + if isinstance( + tool_name := cast(Mapping[str, object], part).get("tool_name"), # cast-ok: dict checked above + str, + ) and tool_name: + normalized_blocks.append({"type": "tool_reference", "tool_name": tool_name}) elif part_type in ("input_image", "image_url"): image_url_val = part.get("image_url") or part.get("url") if isinstance(image_url_val, dict): From 24ef938130dd2374ea7388757ad562bf03dcc674 Mon Sep 17 00:00:00 2001 From: Grainne Ni She McKnight Date: Thu, 15 Oct 2026 16:17:42 +0200 Subject: [PATCH 4/5] refactor: keep tool reference validation concise and typed --- .../transformation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 743d3fd088c..aa5cb889030 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -2081,11 +2081,11 @@ class LiteLLMCompletionResponsesConfig: text_acc.append(txt) normalized_blocks.append({"type": "text", "text": txt}) elif part_type == "tool_reference": - if isinstance( - tool_name := cast(Mapping[str, object], part).get("tool_name"), # cast-ok: dict checked above - str, - ) and tool_name: - normalized_blocks.append({"type": "tool_reference", "tool_name": tool_name}) + match cast(Mapping[str, object], part): # cast-ok: dict checked above + case {"tool_name": str(tool_name)} if tool_name: + normalized_blocks.append({"type": "tool_reference", "tool_name": tool_name}) + case _: + continue elif part_type in ("input_image", "image_url"): image_url_val = part.get("image_url") or part.get("url") if isinstance(image_url_val, dict): From e8d6a03c26a4bc2462a6defa7b91710e15788f11 Mon Sep 17 00:00:00 2001 From: Grainne Ni She McKnight Date: Thu, 15 Oct 2026 16:27:27 +0200 Subject: [PATCH 5/5] test: cover malformed deferred tool references --- .../test_function_call_output_normalization.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py b/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py index e293d4626d3..9ce29b7ec63 100644 --- a/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py +++ b/tests/unit/responses/litellm_completion_transformation/test_function_call_output_normalization.py @@ -70,8 +70,17 @@ def test_function_call_output_string_passthrough(): {"type": "tool_reference", "tool_name": "get_forecast"}, ], ), + ( + [ + {"type": "tool_reference"}, + {"type": "tool_reference", "tool_name": None}, + {"type": "tool_reference", "tool_name": ""}, + {"type": "tool_reference", "tool_name": "get_weather"}, + ], + [{"type": "tool_reference", "tool_name": "get_weather"}], + ), ], - ids=["reference-only", "interleaved-text-and-references"], + ids=["reference-only", "interleaved-text-and-references", "malformed-references-omitted"], ) def test_function_call_output_preserves_tool_references_for_anthropic( output: list[Mapping[str, object]], expected_content: list[ToolMessageContentPart]