diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index 02d9566d396..5e9d791aa99 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -98,6 +98,7 @@ from litellm.types.llms.openai import ( ResponsesAPIResponse, ResponsesAPIStatus, ToolChoice, + ToolMessageContentPart, ValidChatCompletionMessageContentTypes, ValidChatCompletionMessageContentTypesLiteral, ) @@ -2293,7 +2294,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. @@ -2306,7 +2307,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 "" @@ -2315,7 +2316,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): @@ -2326,6 +2327,12 @@ 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 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): @@ -2340,9 +2347,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..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 @@ -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,54 @@ 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"}, + ], + ), + ( + [ + {"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", "malformed-references-omitted"], +) +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, + }