Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/openai/lib/_parsing/_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def parse_response(
) -> ParsedResponse[TextFormatT]:
output_list: List[ParsedResponseOutputItem[TextFormatT]] = []

for output in response.output:
for output in response.output or []:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve accumulated stream output when completion output is null

When a streamed response has valid response.output_item.* events but its final response.completed payload contains output: null, this fallback silently returns an empty output list. ResponseStreamState has already accumulated those items in its snapshot, but it calls parse_response() with event.response at src/openai/lib/streaming/responses/_responses.py:359-364, so the accumulated output is discarded. This avoids the prior exception but causes get_final_response() and the emitted completion event to lose all streamed content; the streaming completion path should fall back to the snapshot's output rather than converting null to an empty result.

Useful? React with 👍 / 👎.

if output.type == "message":
content_list: List[ParsedContent[TextFormatT]] = []
for item in output.content:
Expand Down
55 changes: 55 additions & 0 deletions tests/lib/responses/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from inline_snapshot import snapshot

from openai import OpenAI, AsyncOpenAI
from openai._types import omit
from openai._utils import assert_signatures_in_sync
from openai._models import construct_type_unchecked
from openai.types.responses import Response
from openai.lib._parsing._responses import parse_response

from ...conftest import base_url
from ..snapshots import make_snapshot_request
Expand Down Expand Up @@ -61,3 +65,54 @@ def test_parse_method_definition_in_sync(sync: bool, client: OpenAI, async_clien
checking_client.responses.parse,
exclude_params={"tools"},
)


def test_parse_response_allows_null_output() -> None:
response = construct_type_unchecked(
type_=Response,
value={
"id": "resp_689a0b2545288193953c892439b42e2800b2e36c65a1fd4b",
"object": "response",
"created_at": 1754925861,
"status": "completed",
"background": False,
"error": None,
"incomplete_details": None,
"instructions": None,
"max_output_tokens": None,
"max_tool_calls": None,
"model": "gpt-4o-mini-2024-07-18",
"output": None,
"parallel_tool_calls": True,
"previous_response_id": None,
"prompt_cache_key": None,
"reasoning": {"effort": None, "summary": None},
"safety_identifier": None,
"service_tier": "default",
"store": True,
"temperature": 1.0,
"text": {"format": {"type": "text"}, "verbosity": "medium"},
"tool_choice": "auto",
"tools": [],
"top_logprobs": 0,
"top_p": 1.0,
"truncation": "disabled",
"usage": {
"input_tokens": 14,
"input_tokens_details": {"cached_tokens": 0},
"output_tokens": 50,
"output_tokens_details": {"reasoning_tokens": 0},
"total_tokens": 64,
},
"user": None,
"metadata": {},
},
)
Comment on lines +70 to +110

parsed = parse_response(
text_format=omit,
input_tools=omit,
response=response,
)

assert parsed.output == []