Skip to content

fix(transform schema): preserve const constraints - #1860

Open
pravinireri wants to merge 17 commits into
anthropics:nextfrom
pravinireri:fix/transform-schema-const
Open

fix(transform schema): preserve const constraints#1860
pravinireri wants to merge 17 commits into
anthropics:nextfrom
pravinireri:fix/transform-schema-const

Conversation

@pravinireri

Copy link
Copy Markdown

Summary

  • Preserve JSON Schema const constraints in transform_schema() instead of moving them into description text.
  • Add regression coverage for scalar and null const values.

Problem

Pydantic v2 emits const for single-value Literal fields. transform_schema() currently has explicit handling for enum but not const, so the keyword reaches the unsupported-property fallback and is moved into description. The request schema therefore loses a supported grammar-level constraint (const is 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.

class Job(pydantic.BaseModel):
    status: Literal["ok"]

# property schema emitted by pydantic:   {"const": "ok", "title": "Status", "type": "string"}
# transform_schema() before this change: {"type": "string", "title": "Status", "description": "{const: ok}"}
# transform_schema() after this change:  {"type": "string", "const": "ok", "title": "Status"}

Fix

Preserve const next to the existing enum handling. Use a key-presence check rather than pop("const", None) so a valid const: null constraint is not confused with an absent key.

Tests

Both new tests fail on unmodified upstream/next because const is demoted into description ({"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)
  • same test file with UV_PYTHON=">=3.14.0" — 16 passed (Python 3.14, pydantic 2.12)
  • uv run ruff check on the two touched files — passed
  • uv run ruff format --check on the two touched files — already formatted
  • uv run pyright on the two touched files — 0 errors, 0 warnings

Scope

Hand-maintained schema-transformer logic (src/anthropic/lib/_parse/_transform.py) and its focused regression tests only.

stainless-app Bot and others added 17 commits August 13, 2026 19:12
…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>
@pravinireri
pravinireri requested a review from a team as a code owner August 19, 2026 02:33
Copilot AI lite review requested due to automatic review settings August 19, 2026 02:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 const in transform_schema() similarly to existing enum handling.
  • Implement const handling via key-presence check to correctly retain const: null.
  • Add regression tests covering scalar and null const values.

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.

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.

4 participants