diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.py index b2d1a69e0d8..4768ec5936b 100644 --- a/litellm/responses/litellm_completion_transformation/transformation.py +++ b/litellm/responses/litellm_completion_transformation/transformation.py @@ -78,6 +78,7 @@ from litellm.types.responses.main import ( OutputCodeInterpreterCall, OutputFunctionToolCall, OutputImageGenerationCall, + OutputReasoningText, OutputText, ) from litellm.types.utils import ( @@ -1305,8 +1306,9 @@ class LiteLLMCompletionResponsesConfig: Plaintext a ResponseReasoningItemParam carries in ``content``. Handles content as a string and content as a list of blocks - (output_text / summary_text / text). Returns None when the item has - no content, or only opaque blocks (e.g. encrypted_content). + (reasoning_text / output_text / summary_text / text). Returns None when + the item has no content, or only opaque blocks (e.g. + encrypted_content). """ content: Final[object] = input_item.get("content") if isinstance(content, str) and content.strip(): @@ -2401,11 +2403,17 @@ class LiteLLMCompletionResponsesConfig: choice.finish_reason ), role="assistant", + # `reasoning_text`, not `output_text`: the + # Responses API types a reasoning item's content + # parts differently from a message's. Emitting + # `output_text` here is accepted on the wire but + # makes `openai-python` raise a + # PydanticSerializationUnexpectedValue warning for + # every reasoning-bearing response. content=[ - OutputText( - type="output_text", + OutputReasoningText( + type="reasoning_text", text=text, - annotations=[], ) for text in (reasoning_content,) if text diff --git a/litellm/types/responses/main.py b/litellm/types/responses/main.py index 00635a8e1ef..35413b7dbe7 100644 --- a/litellm/types/responses/main.py +++ b/litellm/types/responses/main.py @@ -27,6 +27,20 @@ class OutputText(BaseLiteLLMOpenAIResponseObject): annotations: list[GenericResponseOutputItemContentAnnotation] | None +class OutputReasoningText(BaseLiteLLMOpenAIResponseObject): + """Reasoning text content inside a ``reasoning`` output item. + + Distinct from :class:`OutputText`: the Responses API types a reasoning + item's content parts as ``reasoning_text`` with a ``text`` field and no + annotations, while a message's content parts are ``output_text``. Emitting + the latter inside a reasoning item makes ``openai-python`` warn on every + reasoning-bearing response. + """ + + type: Literal["reasoning_text"] + text: str | None + + class OutputFunctionToolCall(BaseLiteLLMOpenAIResponseObject): """A tool call to run a function""" @@ -110,7 +124,9 @@ class GenericResponseOutputItem(BaseLiteLLMOpenAIResponseObject): id: str status: str # "completed", "in_progress", etc. role: str # "assistant", "user", etc. - content: list[OutputText] + # `reasoning` items carry `reasoning_text` parts; `message` items carry + # `output_text` ones. + content: list[Union[OutputText, OutputReasoningText]] phase: Phase = None diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py index 2068f10ea2d..54d66705bce 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py @@ -1,6 +1,8 @@ import json +import warnings import pytest +from openai.types.responses.response_reasoning_item import ResponseReasoningItem from litellm.responses.litellm_completion_transformation.transformation import ( @@ -332,7 +334,7 @@ class TestLiteLLMCompletionResponsesConfig: assert reasoning_item.status == "completed" assert reasoning_item.role == "assistant" assert len(reasoning_item.content) == 1 - assert reasoning_item.content[0].type == "output_text" + assert reasoning_item.content[0].type == "reasoning_text" assert "step by step" in reasoning_item.content[0].text assert "42" in reasoning_item.content[0].text @@ -343,6 +345,69 @@ class TestLiteLLMCompletionResponsesConfig: message_item = message_items[0] assert message_item.content[0].text == "The answer is 42." + # A message's parts stay `output_text`; only the reasoning item changes. + assert message_item.content[0].type == "output_text" + + def test_reasoning_content_part_validates_against_the_openai_reasoning_item(self): + """A reasoning item must survive openai-python's own model without warnings. + + The Responses API types a reasoning item's content parts as + `reasoning_text` with a `text` field, while a message's parts are + `output_text` with annotations. Emitting the latter inside a reasoning + item is accepted on the wire, so the response is usable, but + `openai-python` then raises a PydanticSerializationUnexpectedValue + warning for every affected field on every reasoning-bearing response: + + Expected `literal['reasoning_text']` - serialized value may not be + as expected [field_name='type', input_value='output_text'] + """ + chat_completion_response = ModelResponse( + id="test-response-id", + created=1234567890, + model="test-model", + object="chat.completion", + choices=[ + Choices( + finish_reason="stop", + index=0, + message=Message( + content="42.", + role="assistant", + reasoning_content="Working it out.", + ), + ) + ], + ) + + responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response( + request_input="What is the meaning of life?", + responses_api_request={}, + chat_completion_response=chat_completion_response, + ) + + reasoning_item = next( + item for item in responses_api_response.output if item.type == "reasoning" + ) + part = reasoning_item.content[0].model_dump() + + assert part["type"] == "reasoning_text" + assert part["text"] == "Working it out." + # `annotations` belongs to `output_text`, not to a reasoning part. + assert "annotations" not in part + + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + ResponseReasoningItem.model_validate( + { + "id": "rs_test", + "type": "reasoning", + "summary": [], + "content": [part], + } + ).model_dump() + + unexpected = [w for w in caught if "reasoning_text" in str(w.message)] + assert not unexpected, f"openai-python warned on the reasoning part: {unexpected}" def test_transform_chat_completion_response_without_reasoning_content(self): """Test that transformation works normally when no reasoning content is present"""