fix(lib): handle null and empty text in parse_text
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 38/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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'whentext is NoneValidationError: JSON decode error: EOF while parsing a value(orjson.decoder.JSONDecodeError) whentext == ""
Consistency with _completions.py and #3851:
- 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 - In #3851, null
output.contentwas handled by treating it as empty. Adding an early guardif not text: return Noneinparse_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)
- Lenguaje dominante
- Python
- Estrellas
- 31.7k
- Forks
- 6.8k
- Merge medio
- 1 d 4 h
- PR fusionados (30 d)
- 126
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openai/openai-python
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
openai/openai-python#4022 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providersPosiblemente ocupada @mohmedmm la tomó hace 3 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
openai/openai-python#4017 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Querystring drops explicit empty-string scalar valuesPosiblemente ocupada @sylvesterkaczmarek la tomó hace 26 días. Abiertosdk-breaking-change v4
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
openai/openai-python#3837 ·
Los mantenedores suelen responder en 1 día
-
Define + export `ServiceTiers` string literalPosiblemente ocupada @SparshGarg999 la tomó hace 52 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
openai/openai-python#3556 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
Empty OPENAI_BASE_URL prevents fallback to default API endpointPosiblemente ocupada @Sehastrajit-S la tomó hace 18 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
openai/openai-python#2927 · 7 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de openai/openai-python
Issues similares
-
needs-human needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
gke-labs/kube-agents#2400 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Device Details tables: FS/SF columns contradict each other (nfet_01v8 Vt row, pfet_01v8 Idsat row)Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
google/skywater-pdk#450 ·
-
Drained trajectory arrays are overwritten when the sequence buffer is reusedPosiblemente ocupada @sylvesterkaczmarek la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
google-deepmind/bsuite#56 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
LearningCircuit/local-deep-research#7206 ·
Los mantenedores suelen responder en 1 día
-
[TASK] Document technology stackAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
chingu-voyages/V62-tier3-team-33#285 ·
Los mantenedores suelen responder en 1 día