diff --git a/litellm/llms/anthropic/messages/responses_adapters/transformation.py b/litellm/llms/anthropic/messages/responses_adapters/transformation.py index 0630bef0311..547a48e5fbc 100644 --- a/litellm/llms/anthropic/messages/responses_adapters/transformation.py +++ b/litellm/llms/anthropic/messages/responses_adapters/transformation.py @@ -287,6 +287,14 @@ class LiteLLMAnthropicToResponsesAPIAdapter: "arguments": json.dumps(first.get("input", {})), }, ) + if btype == "text": + return ( + { + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": first.get("text", "")}], + }, + ) return () def translate_messages_to_responses_input( @@ -440,19 +448,6 @@ class LiteLLMAnthropicToResponsesAPIAdapter: for _, group in groupby(enumerate(blocks), key=self._assistant_block_group_key) for item in self._assistant_group_to_input_items(tuple(block for _, block in group)) ) - asst_parts: list[dict[str, object]] = [ # mutable-ok: API message payload - {"type": "output_text", "text": block.get("text", "")} - for block in blocks - if block.get("type") == "text" - ] - if asst_parts: - input_items.append( - { - "type": "message", - "role": "assistant", - "content": asst_parts, - } - ) return input_items diff --git a/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py b/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py index ece0cf5d423..efe0c23cbcf 100644 --- a/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py +++ b/tests/unit/llms/anthropic/messages/responses_adapters/test_responses_adapters_transformation.py @@ -671,6 +671,51 @@ class TestTranslateMessagesToResponsesInput: assert result[0]["summary"] == [{"type": "summary_text", "text": "Before the call."}] assert result[2]["summary"] == [{"type": "summary_text", "text": "After the call."}] + def test_assistant_text_before_and_after_tool_use_preserves_order(self): + """Text around tool_use stays in block order, not replayed after (#45470).""" + messages: Final = [ + {"role": "user", "content": "weather in Paris?"}, + { + "role": "assistant", + "content": [ + {"type": "text", "text": "Let me check."}, + { + "type": "tool_use", + "id": "toolu_1", + "name": "get_weather", + "input": {"city": "Paris"}, + }, + {"type": "text", "text": "(after)"}, + ], + }, + { + "role": "user", + "content": [ + {"type": "tool_result", "tool_use_id": "toolu_1", "content": "sunny"} + ], + }, + ] + result: Final = _translate_messages(messages) + assert [item["type"] for item in result] == [ + "message", + "message", + "function_call", + "message", + "function_call_output", + ] + assert result[1] == { + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": "Let me check."}], + } + assert result[2]["type"] == "function_call" + assert result[2]["call_id"] == "toolu_1" + assert result[3] == { + "type": "message", + "role": "assistant", + "content": [{"type": "output_text", "text": "(after)"}], + } + def test_thinking_and_text_stay_separate(self): """The visible answer stays the only thing in the assistant message.""" messages = [