_ensure_strict_json_schema hits RecursionError on cyclic $ref with siblings, drops inherited allOf properties, and skips oneOf/prefixItems
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ó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 55/100
Hướng nghiên cứu
All three defects are in src/openai/lib/_pydantic.py, in _ensure_strict_json_schema and its caller to_strict_json_schema. Start by running the three repro snippets from the issue to confirm the RecursionError, the dropped Base.properties["a"], and the untouched oneOf/prefixItems objects. Fix each path minimally, then re-run the snippets plus the existing _pydantic tests; done means all three outputs match strict-schema expectations without regressing current behavior.
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
openai.lib._pydantic._ensure_strict_json_schema (to_strict_json_schema, src/openai/lib/_pydantic.py) has three schema-transformation bugs on main:
-
RecursionErroron self-referential$refwith sibling keywords (_pydantic.py:100-114):
When a$refentry carries a sibling keyword (e.g.,"description"),_ensure_strict_json_schemaresolves the target definition in-place viajson_schema.update({**resolved, **json_schema}), deletes"$ref", and recursively calls_ensure_strict_json_schema(json_schema, ...)without tracking active$reftargets. If the referenced$defsobject contains a self-reference that also carries a sibling keyword,to_strict_json_schemaenters unbounded recursion and raisesRecursionError. -
Silent loss of inherited
properties/requiredwhen flattening single-entryallOf(_pydantic.py:78-83, 108-110):
When an object schema extends a base definition via single-entryallOf: [{"$ref": "#/$defs/Base"}]alongside local"properties": {"b": ...}, lines 80–82 calljson_schema.update(_ensure_strict_json_schema(all_of[0], ...)), which places"$ref": "#/$defs/Base"ontojson_schema. Lines 108–110 then executejson_schema.update({**resolved, **json_schema}), wherejson_schema["properties"]({"b": ...}) andjson_schema["required"](["b"]) overwriteresolved["properties"]({"a": ...}) andresolved["required"](["a"]). BecauseadditionalProperties: Falseis enforced, every valid instance containing inherited field"a"({"a": 1, "b": 2}) is rejected. -
Missing strict traversal of
oneOf(Pydantic discriminated unions) andprefixItems(tuples) (_pydantic.py:64-88):
_ensure_strict_json_schemarecurses intoproperties,items(whendict),anyOf, andallOf, but never traversesoneOf(emitted by Pydantic v2 for discriminated unionsField(discriminator=...)) orprefixItems(emitted by Pydantic v2 fortuple[SubModel, int]). Any inline object schemas insideoneOforprefixItemsare left withoutadditionalProperties: falseor fullrequiredarrays, causing strict structured-output / tool-schema validation failures.
To Reproduce
from typing import Literal, Union
from pydantic import BaseModel, Field
from openai.lib._pydantic import _ensure_strict_json_schema, to_strict_json_schema
# 1. RecursionError on recursive $ref with sibling description:
cyclic_schema = {
"$defs": {
"Node": {
"type": "object",
"properties": {
"val": {"type": "integer"},
"next": {
"anyOf": [
{"$ref": "#/$defs/Node", "description": "Next node"},
{"type": "null"},
]
},
},
}
},
"$ref": "#/$defs/Node",
}
try:
_ensure_strict_json_schema(cyclic_schema, path=(), root=cyclic_schema)
except RecursionError:
print("Hit RecursionError")
# 2. Inherited properties deleted when flattening single-entry allOf:
allof_schema = {
"$defs": {
"Base": {
"type": "object",
"properties": {"a": {"type": "integer"}},
"required": ["a"],
}
},
"type": "object",
"allOf": [{"$ref": "#/$defs/Base"}],
"properties": {"b": {"type": "integer"}},
"required": ["b"],
}
strict_allof = _ensure_strict_json_schema(allof_schema, path=(), root=allof_schema)
print(strict_allof["properties"].keys())
# Actual: dict_keys(['b']) — 'a' from Base was overwritten and forbidden by additionalProperties: False!
# 3. Inline object inside oneOf / prefixItems misses additionalProperties: False and full required:
oneof_schema = {
"type": "object",
"properties": {
"choice": {
"oneOf": [
{"type": "object", "properties": {"kind": {"const": "a"}, "opt": {"type": "integer"}}},
]
},
"pair": {
"type": "array",
"prefixItems": [
{"type": "object", "properties": {"x": {"type": "integer"}}},
{"type": "integer"},
],
},
},
}
strict_oneof = _ensure_strict_json_schema(oneof_schema, path=(), root=oneof_schema)
print(strict_oneof["properties"]["choice"]["oneOf"][0])
# Missing 'additionalProperties': False and 'required': ['kind', 'opt']
print(strict_oneof["properties"]["pair"]["prefixItems"][0])
# Missing 'additionalProperties': False and 'required': ['x']
OS
Windows / Linux (OS-independent)
Python version
Python 3.12
Library version
openai v3.3.0 / main (3dae6090cc)
- Ngôn ngữ chính
- Python
- Star
- 32.2k
- Fork
- 5.8k
- Merge trung bình
- 12 giờ 29 phút
- Pull request đã merge (30 ngày)
- 112
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 76/100
openai/openai-python#4338 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
openai/openai-python#4337 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersCó thể đã có người làm @Rewaskc02-lang đã nhận 1 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#4311 · 4 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 78/100
openai/openai-python#4310 · 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
openai/openai-python#4309 · 1 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ự
-
triage
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
kornia/kornia-rs#1416 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
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 76/100
PolicyEngine/ai-inequality#64 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timeCó thể đã có người làm @PabloRoque đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
pymc-devs/pymc-extras#839 ·
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 76/100
Maintainer thường phản hồi trong vòng 1 ngày