Repository navigation
fix: defer parsing incomplete streamed text #3378
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
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 |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| from typing import Any, List, Generic, Iterable, Awaitable, cast | ||
| from typing_extensions import Self, Callable, Iterator, AsyncIterator | ||
|
|
||
| import pydantic | ||
|
|
||
| from ._types import ParsedResponseSnapshot | ||
| from ._events import ( | ||
| ResponseStreamEvent, | ||
|
|
@@ -337,7 +339,7 @@ def handle_event(self, event: RawResponseStreamEvent) -> List[ResponseStreamEven | |
| logprobs=event.logprobs, | ||
| type="response.output_text.done", | ||
| text=event.text, | ||
| parsed=parse_text(event.text, text_format=self._text_format, phase=output.phase), | ||
| parsed=self._parse_text_done_event(event.text, phase=output.phase), | ||
| ) | ||
| ) | ||
| elif event.type == "response.function_call_arguments.delta": | ||
|
|
@@ -522,3 +524,21 @@ def _create_initial_response(self, event: RawResponseStreamEvent) -> ParsedRespo | |
| snapshot.output = [] | ||
| self._output_items = dict(enumerate(snapshot.output)) | ||
| return snapshot | ||
|
|
||
| def _parse_text_done_event(self, text: str, *, phase: str | None) -> TextFormatT | None: | ||
| try: | ||
| return parse_text(text, text_format=self._text_format, phase=phase) | ||
| except pydantic.ValidationError as exc: | ||
| if not _is_json_parse_error(exc): | ||
| raise | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def _is_json_parse_error(exc: pydantic.ValidationError) -> bool: | ||
| # Deferral only makes sense while the outer document itself fails to | ||
| # parse, since later chunks may still complete it. A nested Json field | ||
| # raises the same json_invalid / json_type codes but with a non-empty | ||
| # loc, and by then the document is already complete, so the error is | ||
| # deterministic and must keep surfacing at text-done. | ||
| return any(error.get("type") == "json_invalid" and error.get("loc") == () for error in exc.errors()) | ||
|
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. This only matches pydantic v2 error dicts. The SDK still supports v1 ( |
||
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 supported Pydantic 1.x environment,
_compat.model_parse_json()callsparse_raw(), whose truncated-JSON error hastype='value_error.jsondecode'andloc=('__root__',). This predicate rejects that error, so_parse_text_done_event()re-raises and the stream still terminates beforeresponse.incomplete. It also makes the two newly added deferral tests fail underscripts/test-pydantic-v1, whichscripts/testinvokes. Handle the v1 top-level decode signature as well as the v2 one.Useful? React with 👍 / 👎.