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

Offen
#2,016 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

@Parsiffall1 arbeitet bereits daran.

Seit 11.10.2026.

  • #2017 von @Parsiffall1 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
api, backend

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
4.1k
Forks
915
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus anthropics/anthropic-sdk-python

Alle Issues in anthropics/anthropic-sdk-python

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.