transform_schema drops description beside $ref, which the API accepts and follows

Aberta
#2,016 1 comentário 0 reações 0 responsáveis Ver no GitHub

@Parsiffall1 já está trabalhando nisso.

Desde 11/10/2026.

  • #2017 de @Parsiffall1 — aberto

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
35/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
api, backend

Direção de pesquisa

Start by reading transform_schema and the tests for schema transformation in the Python SDK; the issue does not name their paths. Reproduce the example with Answer.model_json_schema() and compare the transformed property. Done when the description beside $ref is retained, with a regression test covering a Pydantic enum or model field. An open linked pull request (#2017) is already addressing this issue.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

When a property has a $ref, transform_schema returns {"$ref": ...} alone and drops the keywords beside it, including description. Pydantic puts a field's Field(description=...) beside the $ref whenever the field's type is an enum or a model, so those descriptions never reach Claude when a model goes through transform_schema.

from enum import Enum
from pydantic import BaseModel, Field
from anthropic import transform_schema

class Level(str, Enum):
    high = "high"
    low = "low"

class Answer(BaseModel):
    level: Level = Field(description="Always choose low.")

Answer.model_json_schema()["properties"]["level"]
# {'$ref': '#/$defs/Level', 'description': 'Always choose low.'}
transform_schema(Answer)["properties"]["level"]
# {'$ref': '#/$defs/Level'}

The API accepts the description there and the model follows it. Sending the pydantic schema above (with additionalProperties: false) directly as output_config={"format": {"type": "json_schema", "schema": ...}} to claude-haiku-5-5, with the prompt "Rate your confidence that 2+2=4.", returned {"level":"low"}.

Expected: transform_schema keeps description beside $ref. A workaround is to put the text in the enum's docstring, which becomes the $defs entry's description.

anthropic 1.13.0, pydantic 2.14.0. Related: #1817, #1764. Found and reproduced with Claude Code.

Linguagem predominante
Python
Estrelas
4.1k
Forks
915
Métricas de merge de PRs
Nenhum PR com merge em 30d

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de anthropics/anthropic-sdk-python

Todas as issues de anthropics/anthropic-sdk-python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.