fix(transform schema): preserve const constraints - #1860
Open
pravinireri wants to merge 17 commits into
Open
Conversation
…ics#399) Squash merge of 3 commits
…thropics#459) Co-authored-by: Claude <noreply@anthropic.com>
…TL (anthropics#453) * fix(session-runner): retry tool-result sends for at least the lease TTL * fix(session-runner): sync the send retry window to the lease TTL from each heartbeat * refactor(session-runner): feed the lease TTL through the private _run_session_tools shim instead of public API * refactor(session-runner): simplify send retry loop and comments --------- Co-authored-by: Claude <noreply@anthropic.com>
… from the package root (anthropics#468) Co-authored-by: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This pull request updates the SDK’s JSON Schema transformer (transform_schema()) to preserve JSON Schema const constraints (notably emitted by Pydantic v2 for single-value Literal fields) so that request schemas retain grammar-level constraints rather than demoting them into descriptive text.
Changes:
- Preserve
constintransform_schema()similarly to existingenumhandling. - Implement
consthandling via key-presence check to correctly retainconst: null. - Add regression tests covering scalar and null
constvalues.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/anthropic/lib/_parse/_transform.py |
Adds explicit const preservation logic (including correct handling for const: null). |
tests/lib/_parse/test_transform.py |
Adds regression tests verifying const is preserved for both scalar and null values. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
stainless-app
Bot
force-pushed
the
next
branch
from
August 19, 2026 13:31
c6cbffd to
5c49897
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
constconstraints intransform_schema()instead of moving them into description text.constvalues.Problem
Pydantic v2 emits
constfor single-valueLiteralfields.transform_schema()currently has explicit handling forenumbut notconst, so the keyword reaches the unsupported-property fallback and is moved intodescription. The request schema therefore loses a supported grammar-level constraint (constis listed as supported for structured outputs), while SDK-side parsing still validates against the original Pydantic model.Related to the enum handling added in #1275.
Fix
Preserve
constnext to the existingenumhandling. Use a key-presence check rather thanpop("const", None)so a validconst: nullconstraint is not confused with an absent key.Tests
Both new tests fail on unmodified
upstream/nextbecauseconstis demoted intodescription({"type": "string", "const": "ok"}becomes{"type": "string", "description": "{const: ok}"}).Commands run (all exited zero):
uv run --isolated --all-extras pytest tests/lib/_parse/test_transform.py -n0— 16 passed (Python 3.9, pydantic 2.12.5)uv run --isolated --all-extras --no-extra=mcp --group=pydantic-v1 pytest tests/lib/_parse/test_transform.py -n0— 16 passed (Python 3.9, pydantic v1)UV_PYTHON=">=3.14.0"— 16 passed (Python 3.14, pydantic 2.12)uv run ruff checkon the two touched files — passeduv run ruff format --checkon the two touched files — already formatteduv run pyrighton the two touched files — 0 errors, 0 warningsScope
Hand-maintained schema-transformer logic (
src/anthropic/lib/_parse/_transform.py) and its focused regression tests only.