Skip to content

fix(transform schema): keep const instead of moving it into the description - #2019

Open
chrikrah wants to merge 1 commit into
anthropics:mainfrom
chrikrah:fix/transform-schema-keep-const
Open

chrikrah wants to merge 1 commit into
anthropics:mainfrom
chrikrah:fix/transform-schema-keep-const

Conversation

@chrikrah

Copy link
Copy Markdown

This carries @pravinireri's #1860 forward against main. It's the same fix. #1860 targeted next, and it closed unreviewed on 2026-10-08 with the other open next pull requests.

transform_schema() has no case for const, so it falls through to the unsupported-keyword fallback in src/anthropic/lib/_parse/_transform.py. It reaches the API as text in description. The structured outputs docs list const as supported. Pydantic emits it for every single-value Literal, including the tag of a discriminated union, so messages.parse() sends that tag as a free string.

Before, on b4b7916, for Pet with pet: Union[Cat, Dog] = Field(discriminator="kind") and kind: Literal["cat"] / Literal["dog"]:

$ python -I probe_const.py   # json.dumps(transform_schema(Pet)["$defs"]["Cat"]["properties"]["kind"])
{"type": "string", "title": "Kind", "description": "{const: cat}"}

After:

$ python -I probe_const.py
{"type": "string", "const": "cat", "title": "Kind"}

const now sits beside enum, which the function already passes through since #1275. It checks for the key, so const: null survives too. No issue exists. Refs #1860.

Verification

$ pytest tests/lib/_parse/test_transform.py -q -p no:cacheprovider
16 passed in 6.52s                # b4b7916: 14 passed
$ pytest tests/lib/_parse tests/lib/test_output_format_helpers.py tests/api_resources/beta/test_output_format_conversion.py -q -n 4
42 passed                         # b4b7916: 40 passed
$ ruff check src/anthropic/lib/_parse tests/lib/_parse && ruff format --check src/anthropic/lib/_parse tests/lib/_parse   # ruff 0.15.6
All checks passed!
6 files already formatted
$ pyright src/anthropic/lib/_parse/_transform.py tests/lib/_parse/test_transform.py   # 1.1.399
0 errors, 0 warnings, 0 informations
# not run: ./scripts/test (mock server, Pydantic v1 matrix) and a live request

Reverting _transform.py and keeping the tests gives 2 failed, 14 passed: test_const_schema and test_const_from_pydantic_literal_discriminator.

@meorphis-anthropic, was the close of #1860 about the next branch or about the change? @craigie-ant, this and my #2015 are both small transform_schema fixes, and I can fold them into one pull request if that's easier to review.

…cription

`const` is a supported keyword for structured outputs, and pydantic emits it
for every single-value `Literal`, including the tag field of a discriminated
union. transform_schema() did not handle it, so it fell through to the
unsupported-keyword fallback and reached the API as description text.
@chrikrah
chrikrah requested a review from a team as a code owner October 12, 2026 13:57

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant