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
9 changes: 8 additions & 1 deletion src/openai/lib/_parsing/_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ def parse_response(
) -> ParsedResponse[TextFormatT]:
output_list: List[ParsedResponseOutputItem[TextFormatT]] = []

for output in response.output:
# Some Responses-API backends emit a terminal payload with
# ``output: null`` instead of ``output: []`` (observed on the ChatGPT
# codex backend for newer models). The Response type annotation is
# ``List[ResponseOutputItem]`` so pydantic doesn't coerce None to [],
# which would raise ``TypeError: 'NoneType' object is not iterable``
# here. Treat None defensively as an empty list so streaming clients
# can recover from the deltas they already received.
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.

P2 Badge Preserve streamed output when the terminal payload is null

When this path is reached from streaming, ResponseStreamState.accumulate_event() calls parse_response(..., response=event.response) for the response.completed event, and get_final_response() later returns that _completed_response. For the backend case described in this change, the terminal event.response.output is None even though earlier response.output_item.added / delta events populated the stream snapshot, so treating it as [] makes the final ResponseCompletedEvent.response and stream.get_final_response() lose all accumulated message/tool output. This avoids the crash but still breaks callers that rely on the final parsed response for exactly the null-terminal-output stream being fixed.

Useful? React with 👍 / 👎.

if output.type == "message":
content_list: List[ParsedContent[TextFormatT]] = []
for item in output.content:
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/responses/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

from openai import OpenAI, AsyncOpenAI
from openai._utils import assert_signatures_in_sync
from openai._types import omit
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 +64,34 @@ def test_parse_method_definition_in_sync(sync: bool, client: OpenAI, async_clien
checking_client.responses.parse,
exclude_params={"tools"},
)


def test_parse_response_handles_null_output() -> None:
"""Regression test: some Responses-API backends emit a terminal
payload with ``output: null`` (observed on the ChatGPT codex backend
for newer models). ``parse_response`` should treat None defensively
as an empty list rather than raising
``TypeError: 'NoneType' object is not iterable``.
"""
response = Response.model_construct(
id="resp_test_null_output",
object="response",
created_at=0,
status="completed",
error=None,
incomplete_details=None,
instructions=None,
model="test-model",
output=None, # the bug-trigger
parallel_tool_calls=True,
temperature=1.0,
tool_choice="auto",
tools=[],
top_p=1.0,
metadata={},
)

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

assert parsed.output == []
assert parsed.output_parsed is None