-
Notifications
You must be signed in to change notification settings - Fork 5k
fix(parsing): guard against None response.output in parse_response #3517
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
base: main
Are you sure you want to change the base?
Changes from all commits
cb66641
1c050f6
479179e
217dc74
da9a9fb
b097d55
103680f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -356,12 +356,76 @@ def accumulate_event(self, event: RawResponseStreamEvent) -> ParsedResponseSnaps | |
| output = snapshot.output[event.output_index] | ||
| if output.type == "function_call": | ||
| output.arguments += event.delta | ||
| elif event.type == "response.output_text.done": | ||
| output = snapshot.output[event.output_index] | ||
| if output.type == "message": | ||
| content = output.content[event.content_index] | ||
| assert content.type == "output_text" | ||
| content.text = event.text | ||
| elif event.type == "response.output_item.done": | ||
| # Replace the item in the snapshot with the finalized item from the | ||
| # done event. The server sends the authoritative item payload here, | ||
| # which may include final fields like `results`/`outputs` on tool | ||
| # items, or a final `status` of `failed`/`incomplete`. Simply | ||
| # setting `status = "completed"` would discard those fields and | ||
| # produce a stale final response in the null-output fallback path. | ||
| if event.output_index < len(snapshot.output): | ||
| snapshot.output[event.output_index] = construct_type_unchecked( | ||
| type_=type(snapshot.output[event.output_index]), | ||
| value=event.item.to_dict(), | ||
| ) | ||
| elif event.type == "response.content_part.done": | ||
| # Replace the content part in the snapshot with the finalized part | ||
| # from the done event. The server sends the authoritative part | ||
| # payload here, which may include metadata like annotations, | ||
| # logprobs, or finalized text/refusal content that the delta | ||
| # accumulation may not fully capture. | ||
| output = snapshot.output[event.output_index] | ||
| if output.type == "message" and event.content_index < len(output.content): | ||
| output.content[event.content_index] = construct_type_unchecked( | ||
| type_=type(output.content[event.content_index]), | ||
| value=event.part.to_dict(), | ||
| ) | ||
| elif event.type == "response.function_call_arguments.done": | ||
| # Apply the finalized arguments string from the done event. | ||
| # The server sends the authoritative arguments payload here, which | ||
| # may differ from the accumulated deltas. Using the finalized | ||
| # arguments ensures `parse_response()` can correctly parse | ||
| # `parsed_arguments` in the null-output fallback path. | ||
| output = snapshot.output[event.output_index] | ||
| if output.type == "function_call": | ||
| output.arguments = event.arguments | ||
| if hasattr(output, "status"): | ||
| output.status = "completed" | ||
| elif event.type == "response.completed": | ||
| self._completed_response = parse_response( | ||
| text_format=self._text_format, | ||
| response=event.response, | ||
| input_tools=self._input_tools, | ||
| ) | ||
| # The chatgpt.com Codex backend sometimes sends `response.output: null` | ||
| # in the consolidated `response.completed` event even when valid | ||
| # `output_item.done` events were streamed earlier (see issue #3325). | ||
| # `parse_response()` guards against `None` with `response.output or []`, | ||
| # but that would discard the already-accumulated `snapshot.output` and | ||
| # emit an empty final response. When the completed event has no | ||
| # output but the snapshot has accumulated items, inject the streamed | ||
| # items into a shallow copy of the response so `parse_response()` can | ||
| # still run its text_format / parsed_arguments logic on them. | ||
| if event.response.output is None and snapshot.output: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the backend sends Useful? React with 👍 / 👎. |
||
| response_with_output = construct_type_unchecked( | ||
| type_=type(event.response), | ||
| value={ | ||
| **event.response.to_dict(), | ||
| "output": [item.to_dict() for item in snapshot.output], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For streams that emit Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| }, | ||
| ) | ||
| self._completed_response = parse_response( | ||
| text_format=self._text_format, | ||
| response=response_with_output, | ||
| input_tools=self._input_tools, | ||
| ) | ||
| else: | ||
| self._completed_response = parse_response( | ||
| text_format=self._text_format, | ||
| response=event.response, | ||
| input_tools=self._input_tools, | ||
| ) | ||
|
|
||
| return snapshot | ||
|
|
||
|
|
||
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 the null-output fallback path,
parse_response()parses tool arguments fromsnapshot.output, but this done handler ignoresevent.arguments, the finalized argument string sent byresponse.function_call_arguments.done. If the accumulated deltas are incomplete or differ from the final payload, the final response can expose staleargumentsand fail or mis-parseparsed_argumentseven though the stream included the authoritative arguments.Useful? React with 👍 / 👎.