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

Abierto
#2,064 0 comentarios 0 reacciones 0 asignados Ver en GitHub

@xgbtxy ya está trabajando en esto.

Desde el 15/11/2026.

  • #2065 de @xgbtxy — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
20/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python
Área
api

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
Python
Estrellas
4.1k
Forks
915
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de anthropics/anthropic-sdk-python

Todos los issues de anthropics/anthropic-sdk-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.