fix(responses): handle incomplete response with reasoning-only output

When a Responses API response has status "incomplete" and all output
tokens were consumed by reasoning (no text output), the transformation
raised ValueError because no choices were created. Now creates a choice
with the reasoning content and empty text, with finish_reason="length".
This commit is contained in:
Vigilans 2026-04-15 11:08:24 +08:00
parent 26fcbc93e5
commit 4aee9fb560

View file

@ -581,6 +581,26 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
reasoning_content = None
pending_reasoning_item = None
# If no choices were created but we have pending reasoning content
# (e.g. incomplete response where all tokens went to reasoning),
# create a choice with empty text so the reasoning is not lost.
if len(choices) == 0 and (
reasoning_content is not None or pending_reasoning_item is not None
):
msg = Message(
content="",
reasoning_content=reasoning_content,
reasoning_items=cast(
Optional[List[ChatCompletionReasoningItem]],
[pending_reasoning_item]
if pending_reasoning_item is not None
else None,
),
)
choices.append(
Choices(message=msg, finish_reason="length", index=index)
)
return choices
def transform_response( # noqa: PLR0915