-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix: handle null output in response.completed streaming events #3331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xodn348
wants to merge
1
commit into
openai:main
Choose a base branch
from
xodn348:fix/issue-3321
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from openai._types import omit as _omit | ||
| from openai._models import construct_type_unchecked | ||
| from openai.types.responses import Response | ||
| from openai.lib._parsing._responses import parse_response | ||
| from openai.lib.streaming.responses._responses import ResponseStreamState | ||
| from openai.types.responses.response_stream_event import ( | ||
| ResponseCreatedEvent, | ||
| ResponseCompletedEvent, | ||
| ResponseOutputItemAddedEvent, | ||
| ) | ||
|
|
||
|
|
||
| def _make_response(**overrides: object) -> Response: | ||
| base = { | ||
| "id": "resp_1", | ||
| "object": "response", | ||
| "created_at": 1700000000.0, | ||
| "model": "gpt-5.2", | ||
| "output": [], | ||
| "status": "completed", | ||
| "parallel_tool_calls": True, | ||
| "tool_choice": "auto", | ||
| "tools": [], | ||
| "text": {"format": {"type": "text"}}, | ||
| "truncation": "disabled", | ||
| } | ||
| base.update(overrides) | ||
| return construct_type_unchecked(type_=Response, value=base) | ||
|
|
||
|
|
||
| class TestParseResponseNullOutput: | ||
| def test_null_output_does_not_crash(self) -> None: | ||
| response = _make_response(output=None) | ||
| # Force output to None (bypassing Pydantic validation) | ||
| object.__setattr__(response, "output", None) | ||
|
|
||
| parsed = parse_response( | ||
| text_format=_omit, | ||
| input_tools=None, | ||
| response=response, | ||
| ) | ||
| assert parsed.output == [] | ||
|
|
||
| def test_empty_output_returns_empty(self) -> None: | ||
| response = _make_response(output=[]) | ||
| parsed = parse_response( | ||
| text_format=_omit, | ||
| input_tools=None, | ||
| response=response, | ||
| ) | ||
| assert parsed.output == [] | ||
|
|
||
|
|
||
| class TestStreamAccumulatorSnapshotFallback: | ||
| def test_snapshot_fallback_when_completed_output_is_null(self) -> None: | ||
| state = ResponseStreamState(text_format=_omit, input_tools=[]) | ||
|
|
||
| created_response = _make_response(output=[]) | ||
| created_event = construct_type_unchecked( | ||
| type_=ResponseCreatedEvent, | ||
| value={ | ||
| "type": "response.created", | ||
| "response": created_response.to_dict(), | ||
| "sequence_number": 0, | ||
| }, | ||
| ) | ||
| state.handle_event(created_event) | ||
|
|
||
| message_item = { | ||
| "id": "msg_1", | ||
| "type": "message", | ||
| "status": "completed", | ||
| "role": "assistant", | ||
| "content": [ | ||
| { | ||
| "type": "output_text", | ||
| "text": "Hello world", | ||
| "annotations": [], | ||
| } | ||
| ], | ||
| } | ||
| added_event = construct_type_unchecked( | ||
| type_=ResponseOutputItemAddedEvent, | ||
| value={ | ||
| "type": "response.output_item.added", | ||
| "output_index": 0, | ||
| "item": message_item, | ||
| "sequence_number": 1, | ||
| }, | ||
| ) | ||
| state.handle_event(added_event) | ||
|
|
||
| completed_response_dict = created_response.to_dict() | ||
| completed_response_dict["output"] = None | ||
| completed_response_dict["status"] = "completed" | ||
|
|
||
| completed_event = construct_type_unchecked( | ||
| type_=ResponseCompletedEvent, | ||
| value={ | ||
| "type": "response.completed", | ||
| "response": completed_response_dict, | ||
| "sequence_number": 2, | ||
| }, | ||
| ) | ||
| object.__setattr__(completed_event.response, "output", None) | ||
|
|
||
| state.handle_event(completed_event) | ||
|
|
||
| assert state._completed_response is not None | ||
| assert len(state._completed_response.output) == 1 | ||
| assert state._completed_response.output[0].type == "message" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In environments using Pydantic v1, which this package still supports via
pydantic>=1.9.0, <3,Responseinstances do not have amodel_copymethod. When a streamedresponse.completedevent hasoutput: nulland prior output items were accumulated, this fallback path will raiseAttributeErrorbefore parsing, so the crash remains for supported Pydantic v1 users. Use the repository's compatibility helper or the v1copy(update=...)path here.Useful? React with 👍 / 👎.