fix(lib): handle null and empty text in parse_text
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 38/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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)
- Ngôn ngữ chính
- Python
- Star
- 31.8k
- Fork
- 7.3k
- Merge trung bình
- 1 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 131
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openai/openai-python
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
openai/openai-python#4022 · 18 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
fix(auth): SubjectTokenProviderError drops response and duplicates error message in workload identity providersCó thể đã có người làm @mohmedmm đã nhận 8 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
openai/openai-python#4017 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Querystring drops explicit empty-string scalar valuesCó thể đã có người làm @sylvesterkaczmarek đã nhận 30 ngày trước. Đang mởsdk-breaking-change v4
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
openai/openai-python#3837 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Define + export `ServiceTiers` string literalCó thể đã có người làm @SparshGarg999 đã nhận 57 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openai/openai-python#3556 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Empty OPENAI_BASE_URL prevents fallback to default API endpointCó thể đã có người làm @Sehastrajit-S đã nhận 23 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
openai/openai-python#2927 · 6 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openai/openai-python
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
awslabs/visual-asset-management-system#414 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug v1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
modelcontextprotocol/python-sdk#3670 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
aicell-lab/bioengine#232 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
modelscope/evalscope#1836 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100