Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

fix(lib): handle null and empty text in parse_text

Aperta
#3,966 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
api

Direzione di ricerca

Start with src/openai/lib/_parsing/_responses.py and compare parse_text() with maybe_parse_content() in _completions.py. Run the existing parsing tests, including static and streaming cases, and verify that null or empty text returns None without raising for both synchronous and asynchronous paths.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Confirm this is an issue with the Python library and not an underlying OpenAI API
  • This is an issue with the Python library
Describe the bug

In src/openai/lib/_parsing/_responses.py, parse_text() attempts to deserialize text into structured output (model_parse_json or json.loads) without verifying that text is non-empty / non-null:

def parse_text(text: str, text_format: type[TextFormatT] | Omit, *, phase: str | None) -> TextFormatT | None:
    if phase not in (None, "final_answer"):
        return None

    if not is_given(text_format):
        return None

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

    return cast(TextFormatT, json.loads(text))

When an output message contains empty or null text (for instance, in streaming response.output_text.done events where no tokens were emitted yet, message parts with content: [{"type": "output_text", "text": ""}], or custom endpoints/mocked transports), parse_text() raises:

  • TypeError: argument 'data': 'NoneType' object cannot be converted to 'PyString' when text is None
  • ValidationError: JSON decode error: EOF while parsing a value (or json.decoder.JSONDecodeError) when text == ""
Consistency with _completions.py and #3851:
  1. In src/openai/lib/_parsing/_completions.py:195, maybe_parse_content() already guards against falsy content before attempting parsing:
    if has_rich_response_format(response_format) and message.content and not message.refusal:
        return _parse_content(response_format, message.content)
    return None
    
  2. In #3851, null output.content was handled by treating it as empty. Adding an early guard if not text: return None in parse_text() provides the same defensive handling for output text.
Proposed fix

In src/openai/lib/_parsing/_responses.py:

def parse_text(text: str | None, text_format: type[TextFormatT] | Omit, *, phase: str | None) -> TextFormatT | None:
    if phase not in (None, "final_answer"):
        return None

    if not is_given(text_format):
        return None

    if not text:
        return None

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

    return cast(TextFormatT, json.loads(text))
Ready branch & tests

The change (+5/-2) and comprehensive sync/async unit tests covering both null and empty text in static parsing and streaming (+44 lines) are tested and ready in fork branch:
👉 https://github.com/Kuldeeep18/openai-python/tree/fix/responses-parse-null-text
(Commit: 6fd8a7b)

To Reproduce
from pydantic import BaseModel
from openai.lib._parsing._responses import parse_text

class Result(BaseModel):
    answer: str

# 1. Null text:
parse_text(None, Result, phase="final_answer")
# -> TypeError: argument 'data': 'NoneType' object cannot be converted to 'PyString'

# 2. Empty text:
parse_text("", Result, phase="final_answer")
# -> pydantic_core._pydantic_core.ValidationError: JSON decode error: EOF while parsing a value
OS

All platforms (cross-platform library parsing issue)

Python version

Python 3.10+

Library version

Latest main (v3.x)

Lingua principale
Python
Stelle
31.7k
Fork
6.8k
Merge medio
1g 4h
PR unite (30g)
126

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openai/openai-python

Tutte le issue di openai/openai-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.