Skip to content

Commit f3adae0

Browse files
author
Deepak kudi
committed
fix: handle null response output in parser
1 parent 2d955a1 commit f3adae0

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/openai/lib/_parsing/_responses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def parse_response(
5858
) -> ParsedResponse[TextFormatT]:
5959
output_list: List[ParsedResponseOutputItem[TextFormatT]] = []
6060

61-
for output in response.output:
61+
for output in response.output or []:
6262
if output.type == "message":
6363
content_list: List[ParsedContent[TextFormatT]] = []
6464
for item in output.content:

tests/lib/responses/test_responses.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from inline_snapshot import snapshot
88

99
from openai import OpenAI, AsyncOpenAI
10+
from openai._types import omit
1011
from openai._utils import assert_signatures_in_sync
12+
from openai._models import construct_type_unchecked
13+
from openai.types.responses import Response
14+
from openai.lib._parsing._responses import parse_response
1115

1216
from ...conftest import base_url
1317
from ..snapshots import make_snapshot_request
@@ -41,6 +45,38 @@ def test_output_text(client: OpenAI, respx_mock: MockRouter) -> None:
4145
)
4246

4347

48+
def test_parse_response_handles_null_output() -> None:
49+
response = construct_type_unchecked(
50+
type_=Response,
51+
value={
52+
"id": "resp_null_output",
53+
"object": "response",
54+
"created_at": 1754925861,
55+
"status": "completed",
56+
"error": None,
57+
"incomplete_details": None,
58+
"instructions": None,
59+
"max_output_tokens": None,
60+
"model": "gpt-4o-mini-2024-07-18",
61+
"output": None,
62+
"parallel_tool_calls": True,
63+
"previous_response_id": None,
64+
"reasoning": {"effort": None, "summary": None},
65+
"store": True,
66+
"temperature": 1.0,
67+
"text": {"format": {"type": "text"}},
68+
"tool_choice": "auto",
69+
"tools": [],
70+
"top_p": 1.0,
71+
"truncation": "disabled",
72+
},
73+
)
74+
75+
parsed = parse_response(text_format=omit, input_tools=omit, response=response)
76+
77+
assert parsed.output == []
78+
79+
4480
@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
4581
def test_stream_method_definition_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None:
4682
checking_client: OpenAI | AsyncOpenAI = client if sync else async_client

0 commit comments

Comments
 (0)