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

オープン
#2,064 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@xgbtxy がすでに取り組んでいます。

2026年11月15日 から。

  • #2065 @xgbtxy による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
20/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
api

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
Python
スター
4.1k
フォーク
915
PR マージ指標
30日以内にマージされた PR はありません

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

anthropics/anthropic-sdk-python のほかの issue

anthropics/anthropic-sdk-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。