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

for output in response.output:
# `response.output` can be None for incomplete/failed/reasoning-only
# responses (and via some proxy/aggregator backends); guard so the stream
# parser yields an empty output instead of raising TypeError.
for output in (response.output or []):
if output.type == "message":
content_list: List[ParsedContent[TextFormatT]] = []
for item in output.content:
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/responses/test_parse_response_none_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

from openai._types import omit
from openai.lib._parsing._responses import parse_response
from openai.types.responses import Response


def _minimal_response(**overrides: object) -> Response:
# Build a Response with the required scalar fields set; `output` is the
# field under test. Uses construct() to avoid pulling a full live payload.
base: dict[str, object] = dict(
id="resp_test",
created_at=0.0,
error=None,
incomplete_details=None,
instructions=None,
metadata=None,
model="gpt-4o-mini",
object="response",
output=[],
parallel_tool_calls=True,
temperature=1.0,
tool_choice="auto",
tools=[],
top_p=1.0,
)
base.update(overrides)
return Response.construct(**base)


def test_parse_response_handles_none_output() -> None:
# `output` can be None for incomplete/failed/reasoning-only responses and
# via some proxy/aggregator backends. parse_response must not raise
# TypeError('NoneType' object is not iterable) — it should yield empty output.
response = _minimal_response(output=None)

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

assert parsed.output == []