-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: handle null response.output in parse_response #3441
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
C1-BA-B1-F3
wants to merge
2
commits into
openai:main
Choose a base branch
from
C1-BA-B1-F3:fix/parse-response-null-output
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.
+194
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,185 @@ | ||
| """Tests for parse_response handling of null/None output fields.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from openai._models import construct_type_unchecked | ||
| from openai._types import Omit | ||
| from openai.lib._parsing._responses import parse_response | ||
| from openai.lib.streaming.responses._responses import ResponseStreamState | ||
| from openai.types.responses import Response, ParsedResponse | ||
| from openai.types.responses.response_created_event import ResponseCreatedEvent | ||
| from openai.types.responses.response_completed_event import ResponseCompletedEvent | ||
| from openai.types.responses.response_output_item_added_event import ResponseOutputItemAddedEvent | ||
| from openai.types.responses.response_content_part_added_event import ResponseContentPartAddedEvent | ||
| from openai.types.responses.response_text_delta_event import ResponseTextDeltaEvent | ||
|
|
||
|
|
||
| def _make_response(output=None, **kwargs): | ||
| """Helper to construct a Response with a given output field.""" | ||
| base = { | ||
| "id": "resp_test123", | ||
| "created_at": 1234567890.0, | ||
| "model": "gpt-4o", | ||
| "object": "response", | ||
| "status": "completed", | ||
| "output": output, | ||
| "parallel_tool_calls": True, | ||
| "tool_choice": "auto", | ||
| "tools": [], | ||
| "temperature": 1.0, | ||
| "top_p": 1.0, | ||
| } | ||
| base.update(kwargs) | ||
| return construct_type_unchecked(type_=Response, value=base) | ||
|
|
||
|
|
||
| def test_parse_response_with_none_output(): | ||
| """Test that parse_response handles null output without crashing.""" | ||
| response = _make_response(output=None) | ||
| assert response.output is None | ||
|
|
||
| result = parse_response( | ||
| text_format=None, | ||
| input_tools=None, | ||
| response=response, | ||
| ) | ||
|
|
||
| assert isinstance(result, ParsedResponse) | ||
| assert result.output == [] | ||
|
|
||
|
|
||
| def test_parse_response_with_empty_list_output(): | ||
| """Test that parse_response handles empty list output correctly.""" | ||
| response = _make_response(output=[]) | ||
| assert response.output == [] | ||
|
|
||
| result = parse_response( | ||
| text_format=None, | ||
| input_tools=None, | ||
| response=response, | ||
| ) | ||
|
|
||
| assert isinstance(result, ParsedResponse) | ||
| assert result.output == [] | ||
|
|
||
|
|
||
| def test_parse_response_with_message_output(): | ||
| """Test that parse_response still works correctly with actual output items.""" | ||
| output_data = [ | ||
| { | ||
| "id": "msg_test123", | ||
| "type": "message", | ||
| "status": "completed", | ||
| "role": "assistant", | ||
| "content": [ | ||
| { | ||
| "type": "output_text", | ||
| "text": "Hello, world!", | ||
| "annotations": [], | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| response = _make_response(output=output_data) | ||
|
|
||
| result = parse_response( | ||
| text_format=Omit(), | ||
| input_tools=None, | ||
| response=response, | ||
| ) | ||
|
|
||
| assert isinstance(result, ParsedResponse) | ||
| assert len(result.output) == 1 | ||
| assert result.output[0].type == "message" | ||
|
|
||
|
|
||
| def test_streaming_accumulated_items_preserved_on_null_output(): | ||
| """When response.completed arrives with null output, items accumulated | ||
| from prior streaming events should be preserved in get_final_response().""" | ||
| state = ResponseStreamState(text_format=Omit(), input_tools=Omit()) | ||
|
|
||
| response_created = _make_response(output=[], status="in_progress") | ||
| created_event = construct_type_unchecked( | ||
| type_=ResponseCreatedEvent, | ||
| value={ | ||
| "type": "response.created", | ||
| "response": response_created.to_dict(), | ||
| "sequence_number": 0, | ||
| }, | ||
| ) | ||
| state.handle_event(created_event) | ||
|
|
||
| item_added_event = construct_type_unchecked( | ||
| type_=ResponseOutputItemAddedEvent, | ||
| value={ | ||
| "type": "response.output_item.added", | ||
| "output_index": 0, | ||
| "item": { | ||
| "id": "msg_abc", | ||
| "type": "message", | ||
| "status": "in_progress", | ||
| "role": "assistant", | ||
| "content": [], | ||
| }, | ||
| "sequence_number": 1, | ||
| }, | ||
| ) | ||
| state.handle_event(item_added_event) | ||
|
|
||
| part_added_event = construct_type_unchecked( | ||
| type_=ResponseContentPartAddedEvent, | ||
| value={ | ||
| "type": "response.content_part.added", | ||
| "output_index": 0, | ||
| "content_index": 0, | ||
| "item_id": "msg_abc", | ||
| "part": {"type": "output_text", "text": "", "annotations": []}, | ||
| "sequence_number": 2, | ||
| }, | ||
| ) | ||
| state.handle_event(part_added_event) | ||
|
|
||
| delta_event_1 = construct_type_unchecked( | ||
| type_=ResponseTextDeltaEvent, | ||
| value={ | ||
| "type": "response.output_text.delta", | ||
| "output_index": 0, | ||
| "content_index": 0, | ||
| "item_id": "msg_abc", | ||
| "delta": "Hello, ", | ||
| "logprobs": [], | ||
| "sequence_number": 3, | ||
| }, | ||
| ) | ||
| state.handle_event(delta_event_1) | ||
|
|
||
| delta_event_2 = construct_type_unchecked( | ||
| type_=ResponseTextDeltaEvent, | ||
| value={ | ||
| "type": "response.output_text.delta", | ||
| "output_index": 0, | ||
| "content_index": 0, | ||
| "item_id": "msg_abc", | ||
| "delta": "world!", | ||
| "logprobs": [], | ||
| "sequence_number": 4, | ||
| }, | ||
| ) | ||
| state.handle_event(delta_event_2) | ||
|
|
||
| response_completed = _make_response(output=None, status="completed") | ||
| completed_event = construct_type_unchecked( | ||
| type_=ResponseCompletedEvent, | ||
| value={ | ||
| "type": "response.completed", | ||
| "response": response_completed.to_dict(), | ||
| "sequence_number": 5, | ||
| }, | ||
| ) | ||
| 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" | ||
| msg = state._completed_response.output[0] | ||
| assert msg.content[0].text == "Hello, world!" |
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.
When this runs from
ResponseStreamState.accumulate_event()for aresponse.completedevent,event.response.output == Noneis now converted to an empty finalParsedResponse.outputeven though the stream state may already contain items accumulated from priorresponse.output_item.added/ text delta events. In that null-output backend scenario, consumers ofresponse.completed.response,get_final_response().output, orget_final_response().output_textsilently get an empty response instead of the text/tool calls they just streamed, so the stream no longer crashes but still drops the completed result.Useful? React with 👍 / 👎.
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.
Thanks for raising this! You're correct that a null in was overwriting the accumulated stream content, resulting in empty final responses.
The fix preserves accumulated items from prior / text delta events when is null, so consumers get the complete streamed result rather than an empty response. The null output case now only applies when there's genuinely nothing streamed.
Happy to adjust the approach if you prefer a different strategy for handling this edge case!