fix(lib): handle null and empty text in parse_text
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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)
- 主要语言
- Python
- 星标
- 31.8k
- 派生
- 7.3k
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 131
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
openai/openai-python#4022 · 18 条评论 ·
维护者通常 1 天内回复
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providers可能已有人在做 @mohmedmm 于 7 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#4017 · 3 条评论 ·
维护者通常 1 天内回复
-
Querystring drops explicit empty-string scalar values可能已有人在做 @sylvesterkaczmarek 于 30 天前认领。 未关闭sdk-breaking-change v4
难度 2/5 1-3 小时 新手友好度 86/100
openai/openai-python#3837 ·
维护者通常 1 天内回复
-
Define + export `ServiceTiers` string literal可能已有人在做 @SparshGarg999 于 56 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#3556 · 3 条评论 ·
维护者通常 1 天内回复
-
Empty OPENAI_BASE_URL prevents fallback to default API endpoint可能已有人在做 @Sehastrajit-S 于 22 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 68/100
openai/openai-python#2927 · 6 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
Performance: deprecated DeviceEntry.config_entries access blocks the event loop for tens of seconds未关闭
难度 2/5 1-3 小时 新手友好度 75/100
tuya/tuya_cloud_ha_bridge#14 ·
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
semantica-agi/semantica#1968 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
agent: ready area: submission priority: high type: docs
难度 2/5 1-3 小时 新手友好度 62/100
dkritarth/scopewatch#213 ·
维护者通常 1 天内回复
-
Broken link in RELEASE.md可能已有人在做 @Jah-yee 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 88/100
sphinx-contrib/httpdomain#143 ·
维护者通常 1 天内回复