Skip to content

fix(responses): handle null and empty text in parse_text - #3897

Open
Kuldeeep18 wants to merge 2 commits into
openai:mainfrom
Kuldeeep18:fix/responses-parse-null-text
Open

Kuldeeep18 wants to merge 2 commits into
openai:mainfrom
Kuldeeep18:fix/responses-parse-null-text

Conversation

@Kuldeeep18

Copy link
Copy Markdown
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

This PR fixes a runtime crash in structured outputs for the Responses API (client.responses.parse() and client.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 assumed text is always a valid JSON string:

if is_basemodel_type(text_format):
    return cast(TextFormatT, model_parse_json(text_format, text))

return cast(TextFormatT, TypeAdapter(text_format).validate_json(text))

If an output_text content item has text=None or text="" (e.g., from compatible third-party gateways, edge cases, or intermediate states), model_parse_json() or TypeAdapter.validate_json() raises an unhandled pydantic.ValidationError or JSON decode error instead of setting .parsed to None.

In contrast, the Chat Completions parsing helper (src/openai/lib/_parsing/_completions.py:maybe_parse_content) already safely guards against missing text:

if message.content and not message.refusal:
    message.parsed = _parse_chat_response(...)

Solution

  1. In src/openai/lib/_parsing/_responses.py, updated parse_text() type signature to accept text: str | None and added an early guard:
    if not text:
        return None
  2. Added comprehensive unit and streaming regression tests in tests/lib/responses/test_commentary_parsing.py covering both sync and async paths for None and "" text.

Note: Per CONTRIBUTING.md, code under src/openai/lib/ is handwritten and not modified by the code generator.

Additional context & links

  • Passes all unit tests: uv run --all-extras pytest tests/lib/responses/ (434 passed)
  • Passes type checks and linter: uv run ruff check, uv run ruff format --check, uv run mypy src/openai/lib/_parsing/_responses.py

@Kuldeeep18
Kuldeeep18 requested a review from a team as a code owner September 18, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant