`BaseModel.construct()` leaks consumed field aliases into `model_extra`, emitting duplicate JSON keys and shadowing field mutations
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
- 78/100
Hướng nghiên cứu
Bắt đầu trong src/openai/_models.py tại BaseModel.construct(), nơi các trường được khai báo được phân giải qua field.alias và sau đó các extras được thu thập bằng if key not in model_fields. Ghi lại các khóa alias được vòng lặp đầu tiên tiêu thụ và loại trừ chúng khi xây dựng _extra, theo dõi cả hai nhánh Pydantic v1 và v2 trong phương thức đó. Hoàn thành nghĩa là construct_type() trên ResponseFunctionToolCall với async để model_extra trống, to_json() phát ra một khóa async duy nhất, và một async_ đã bị thay đổi xuất hiện trong to_dict().
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/_models.py, BaseModel.construct() populates declared fields by resolving each field's alias (field.alias), but then collects extra fields by checking only if key not in model_fields: (lines 269–293):
model_fields = get_model_fields(__cls)
for name, field in model_fields.items():
key = field.alias
if key is None or (key not in values and populate_by_name):
key = name
if key in values:
fields_values[name] = _construct_field(value=values[key], field=field, key=key)
_fields_set.add(name)
else:
fields_values[name] = field_get_default(field)
extra_field_type = _get_extra_fields_type(__cls)
_extra = {}
for key, value in values.items():
if key not in model_fields:
...
_extra[key] = parsed
Because model_fields is keyed by Python attribute names (e.g., "async_", "schema_") rather than wire aliases (e.g., "async", "schema"), every consumed alias key is also copied into __pydantic_extra__ (model_extra) (and into __dict__ / __fields_set__ on Pydantic v1).
Since default API response deserialization (construct_type) uses BaseModel.construct(), this affects every aliased field in src/openai/types/ (e.g., ResponseFunctionToolCall.async_, ResponseFormatTextJSONSchemaConfig.schema_, etc.):
model.model_extrais polluted with already-parsed declared fields.model.to_json()emits duplicate JSON keys out of the box (e.g.,"async":true,"async":trueor"schema":{...},"schema":{...}).model.model_dump()andmodel.to_dict(use_api_names=False)emit both the attribute name and the wire alias.- Mutating an aliased attribute (e.g.,
fc.async_ = False) is silently overwritten by the stale entry inmodel_extraonto_dict()and emits conflicting duplicate keys onto_json().
To Reproduce
- Construct any SDK model with an aliased field via
construct_type()orModel.construct(), such asResponseFunctionToolCall(async_/"async") orResponseFormatTextJSONSchemaConfig(schema_/"schema"). - Inspect
model.model_extra,model.to_json(indent=None), andmodel.to_dict(use_api_names=False). - Mutate the aliased field (
fc.async_ = False) and inspectfc.to_dict()["async"]andfc.to_json(indent=None).
Code snippets
from openai._models import construct_type
from openai.types.responses import (
ResponseFormatTextJSONSchemaConfig,
ResponseFunctionToolCall,
)
fc = construct_type(
type_=ResponseFunctionToolCall,
value={
"type": "function_call",
"call_id": "call_1",
"name": "do_work",
"arguments": "{}",
"async": True,
},
)
print("model_extra:", fc.model_extra)
# {'async': True}
print("to_json:", fc.to_json(indent=None))
# {"arguments":"{}","call_id":"call_1","name":"do_work","type":"function_call","async":true,"async":true}
print("to_dict(use_api_names=False):", fc.to_dict(use_api_names=False))
# {'arguments': '{}', 'call_id': 'call_1', 'name': 'do_work', 'type': 'function_call', 'async_': True, 'async': True}
fc.async_ = False
print("after mutation to_dict['async']:", fc.to_dict()["async"])
# True (stale extra overwrites mutated field!)
print("after mutation to_json:", fc.to_json(indent=None))
# {"arguments":"{}","call_id":"call_1","name":"do_work","type":"function_call","async":false,"async":true}
cfg = construct_type(
type_=ResponseFormatTextJSONSchemaConfig,
value={
"type": "json_schema",
"name": "answer",
"schema": {"type": "object", "properties": {}},
},
)
print("cfg to_json:", cfg.to_json(indent=None))
# {"name":"answer","schema":{"type":"object","properties":{}},"type":"json_schema","schema":{"type":"object","properties":{}}}
Suggested fix
Track consumed input keys during the field-construction loop and exclude them when populating extras:
--- a/src/openai/_models.py
+++ b/src/openai/_models.py
@@ -267,6 +267,7 @@ class BaseModel(pydantic.BaseModel):
_fields_set = set()
model_fields = get_model_fields(__cls)
+ consumed_keys: set[str] = set()
for name, field in model_fields.items():
key = field.alias
if key is None or (key not in values and populate_by_name):
@@ -275,6 +276,7 @@ class BaseModel(pydantic.BaseModel):
if key in values:
fields_values[name] = _construct_field(value=values[key], field=field, key=key)
_fields_set.add(name)
+ consumed_keys.add(key)
else:
fields_values[name] = field_get_default(field)
@@ -282,7 +284,7 @@ class BaseModel(pydantic.BaseModel):
_extra = {}
for key, value in values.items():
- if key not in model_fields:
+ if key not in consumed_keys and key not in model_fields:
parsed = construct_type(value=value, type_=extra_field_type) if extra_field_type is not None else value
if PYDANTIC_V1:
OS
macOS
Python version
Python 3.14
Library version
openai v3.57.0 (main@f63f14b)
- 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 82/100
openai/openai-python#4309 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
openai/openai-python#4308 · 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ự
-
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
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
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonCó thể đã có người làm @ronaldtse đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
pydantic/pytest-examples#102 · 1 bình luận ·