transform_schema crashes on boolean array items and drops/misconstrains tuple prefixItems

Đang mở
#2,064 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@xgbtxy đang làm issue này rồi.

Từ ngày 15/11/2026.

  • #2065 của @xgbtxy — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
20/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
api

Hướng nghiên cứu

Start in src/anthropic/lib/_parse/_transform.py around lines 200-211, where transform_schema handles items and crashes at json_schema = {**json_schema} when items is a bool or list. Reproduce the three cases in the issue body, and note that pull request #2065 is already open against this work. Done means the three cases no longer crash or drop constraints, with closed and open tuples handled as the expected-behavior section describes.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Describe the bug

anthropic.transform_schema (src/anthropic/lib/_parse/_transform.py:200-211) has three issues when transforming JSON Schema Draft 2020-12 / Pydantic tuple array schemas (type: "array"):

  1. TypeError crash on boolean items (items: False / items: True) and Draft-07 list items:
    In JSON Schema Draft 2020-12, closed tuple schemas specify "items": false alongside "prefixItems": [...] (and open tuples may specify "items": true). transform_schema checks if items is not None: strict_schema["items"] = transform_schema(items), passing bool into transform_schema and crashing with TypeError: 'bool' object is not a mapping at json_schema = {**json_schema}. Draft-07 tuple schemas ("items": [{...}, {...}]) similarly crash with TypeError: 'list' object is not a mapping.

  2. Empty-intersection grammar on open typed tuples (prefixItems + dict items):
    In Draft 2020-12, {"type": "array", "prefixItems": [{"type": "integer"}], "items": {"type": "string"}, "minItems": 2} applies "items" only to indices >= len(prefixItems) (so valid arrays are [42, "ok", ...]). transform_schema currently drops prefixItems into description while keeping "items": {"type": "string"} on the array, which constrains index 0 to string as well and prevents the model from producing any valid instance matching the original schema.

  3. Pydantic tuple[ModelA, int] drops element constraints and leaves $defs unreferenced:
    Pydantic v2 emits {"type": "array", "prefixItems": [{"$ref": "#/$defs/Step"}, {"type": "integer"}], "minItems": 2, "maxItems": 2} for tuple[Step, int]. Because prefixItems is ignored, strict_schema omits "items" completely (leaving #/$defs/Step unreferenced in $defs and allowing arbitrary untyped array elements) instead of lowering bounded/closed positional schemas into items (anyOf or single schema when homogeneous).

To Reproduce
import pydantic
from anthropic import transform_schema

# 1. Crash on valid Draft 2020-12 closed tuple schema:
transform_schema({
    "type": "array",
    "prefixItems": [{"type": "integer"}, {"type": "string"}],
    "items": False,
})
# TypeError: 'bool' object is not a mapping

# 2. Open typed tuple becomes unsatisfiable (index 0 forced to "string"):
print(transform_schema({
    "type": "array",
    "prefixItems": [{"type": "integer"}],
    "items": {"type": "string"},
    "minItems": 2,
}))
# {'type': 'array', 'items': {'type': 'string'}, 'description': "{prefixItems: [{'type': 'integer'}], minItems: 2}"}

# 3. Pydantic tuple[Step, int] loses element schema & orphans #/$defs/Step:
class Step(pydantic.BaseModel):
    action: str

class Plan(pydantic.BaseModel):
    pair: tuple[Step, int]

print(transform_schema(Plan))
# properties.pair has no 'items' constraint; $defs.Step is unreferenced
Expected behavior
  • transform_schema should never crash on boolean items (False/True) or list items.
  • For closed tuples (items: False or maxItems <= len(prefixItems)) and open typed-tail tuples (prefixItems + dict items), transform_schema should lower the allowed element schemas into strict_schema["items"] (deduplicated single schema when homogeneous, {"anyOf": [...]} when heterogeneous) while preserving the positional prefixItems/items constraints in description.
  • For open untyped-tail tuples (prefixItems without items and without maxItems <= len(prefixItems), or items: True), strict_schema should omit "items" so tail elements remain unconstrained.
Ngôn ngữ chính
Python
Star
4.1k
Fork
915
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của anthropics/anthropic-sdk-python

Tất cả issue của anthropics/anthropic-sdk-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.