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 streamed output when terminal output is null

When a streamed response has already accumulated response.output_item.* / text events but the terminal response.completed payload has response.output is None, this fallback causes parse_response() to return a completed ParsedResponse with output=[]. I checked the streaming path in src/openai/lib/streaming/responses/_responses.py: ResponseStreamState.accumulate_event() assigns _completed_response = parse_response(... response=event.response ...), and get_final_response() returns that object rather than the accumulated snapshot, so callers lose all streamed content instead of just avoiding the TypeError.

Useful? React with 👍 / 👎.

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

from openai import OpenAI, AsyncOpenAI
from openai._types import NOT_GIVEN
from openai._utils import assert_signatures_in_sync
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 @@ -41,6 +44,23 @@ def test_output_text(client: OpenAI, respx_mock: MockRouter) -> None:
)


def test_parse_response_allows_null_output() -> None:
response = Response.construct(
id="resp_123",
object="response",
created_at=0,
model="gpt-4o-mini",
output=None,
parallel_tool_calls=True,
tool_choice="auto",
tools=[],
)

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

assert parsed.output == []


@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
def test_stream_method_definition_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None:
checking_client: OpenAI | AsyncOpenAI = client if sync else async_client
Expand Down