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

Aperta
#2,064 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@xgbtxy ci sta già lavorando.

Dal 15/11/2026.

  • #2065 di @xgbtxy — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
20/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
api

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
Python
Stelle
4.1k
Fork
915
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di anthropics/anthropic-sdk-python

Tutte le issue di anthropics/anthropic-sdk-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.