fix(responses): type reasoning content parts as reasoning_text

A `reasoning` output item built from `reasoning_content` carried its text in
an `output_text` content part. The Responses API types a reasoning item's
parts as `reasoning_text` with a `text` field and no annotations, while a
message's parts are `output_text` with them.

The response is usable either way, so this is not a wire break, but
openai-python types the field as `Literal["reasoning_text"]` and 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', input_type=str]

Adds `OutputReasoningText`, widens `GenericResponseOutputItem.content` to
accept either part, and emits the reasoning one from
`_extract_reasoning_output_items`. Message items are untouched and keep
`output_text`.

The input side already round-trips this: `_reasoning_text_from_content`
accepts any block carrying `text` that is not opaque, so a client replaying
a `reasoning_text` part gets the same `reasoning_content` back. Its docstring
now says so.

Tests: the existing reasoning assertion moves to `reasoning_text`, and a new
test validates the emitted part through openai-python's own
`ResponseReasoningItem` with warnings captured, so the regression is caught
by the same mechanism that reported it.
This commit is contained in:
Luis Castillo 2026-09-09 15:35:14 +02:00
parent 328a5f5d60
commit 3d62c4d66e
No known key found for this signature in database
3 changed files with 96 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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"""