fix(responses): handle null and empty text in parse_text - #3897
Open
Kuldeeep18 wants to merge 2 commits into
Open
Kuldeeep18 wants to merge 2 commits into
Kuldeeep18 wants to merge 2 commits into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes being requested
This PR fixes a runtime crash in structured outputs for the Responses API (
client.responses.parse()andclient.responses.stream(..., text_format=...)) when an output item contains null (None) or empty string ("") text.Problem
In
src/openai/lib/_parsing/_responses.py,parse_text()previously assumedtextis always a valid JSON string:If an
output_textcontent item hastext=Noneortext=""(e.g., from compatible third-party gateways, edge cases, or intermediate states),model_parse_json()orTypeAdapter.validate_json()raises an unhandledpydantic.ValidationErroror JSON decode error instead of setting.parsedtoNone.In contrast, the Chat Completions parsing helper (
src/openai/lib/_parsing/_completions.py:maybe_parse_content) already safely guards against missing text:Solution
src/openai/lib/_parsing/_responses.py, updatedparse_text()type signature to accepttext: str | Noneand added an early guard:tests/lib/responses/test_commentary_parsing.pycovering both sync and async paths forNoneand""text.Note: Per
CONTRIBUTING.md, code undersrc/openai/lib/is handwritten and not modified by the code generator.Additional context & links
uv run --all-extras pytest tests/lib/responses/(434 passed)uv run ruff check,uv run ruff format --check,uv run mypy src/openai/lib/_parsing/_responses.py