diff --git a/litellm/llms/anthropic/pass_through/responses_adapters/transformation.py b/litellm/llms/anthropic/pass_through/responses_adapters/transformation.py index 26f82d66bfc..d7c6bb658ba 100644 --- a/litellm/llms/anthropic/pass_through/responses_adapters/transformation.py +++ b/litellm/llms/anthropic/pass_through/responses_adapters/transformation.py @@ -305,9 +305,13 @@ class LiteLLMAnthropicToResponsesAPIAdapter: elif isinstance(inner, str): output_text = inner elif isinstance(inner, list): - parts = [ - c.get("text", "") for c in inner if isinstance(c, dict) and c.get("type") == "text" - ] + parts = ( + c.get("text", "") + if c.get("type") == "text" + else f"[Loaded tool: {c.get('tool_name', '')}]" + for c in inner + if isinstance(c, dict) and c.get("type") in {"text", "tool_reference"} + ) output_text = "\n".join(parts) image_candidates = tuple( self._translate_anthropic_image_source_to_url(c.get("source")) diff --git a/tests/unit/llms/anthropic/pass_through/responses_adapters/test_responses_adapters_transformation.py b/tests/unit/llms/anthropic/pass_through/responses_adapters/test_responses_adapters_transformation.py index 9b6b44c3d05..97f8512e55f 100644 --- a/tests/unit/llms/anthropic/pass_through/responses_adapters/test_responses_adapters_transformation.py +++ b/tests/unit/llms/anthropic/pass_through/responses_adapters/test_responses_adapters_transformation.py @@ -481,6 +481,33 @@ class TestTranslateMessagesToResponsesInput: result = _translate_messages(messages) assert result[0]["output"] == "Line 1\nLine 2" + def test_user_tool_result_list_content_preserves_tool_references(self): + messages = [ + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "call_search", + "content": [ + {"type": "text", "text": "Available:"}, + {"type": "tool_reference", "tool_name": "get_weather"}, + ], + } + ], + } + ] + + result = _translate_messages(messages) + + assert result == [ + { + "type": "function_call_output", + "call_id": "call_search", + "output": "Available:\n[Loaded tool: get_weather]", + } + ] + def test_user_tool_result_null_content(self): """tool_result with null content becomes empty string output.""" messages = [