From a4bf59b583d07a3a97cd8704b76870f7bba5c0a7 Mon Sep 17 00:00:00 2001 From: Pravin Date: Tue, 18 Aug 2026 22:10:20 -0400 Subject: [PATCH] fix(transform schema): preserve const constraints --- src/anthropic/lib/_parse/_transform.py | 4 ++++ tests/lib/_parse/test_transform.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/anthropic/lib/_parse/_transform.py b/src/anthropic/lib/_parse/_transform.py index ce0c83ac9..a2852f2e9 100644 --- a/src/anthropic/lib/_parse/_transform.py +++ b/src/anthropic/lib/_parse/_transform.py @@ -117,6 +117,10 @@ def transform_schema( if is_list(enum): strict_schema["enum"] = enum + # Use a key-presence check because `const: null` is a valid constraint. + if "const" in json_schema: + strict_schema["const"] = json_schema.pop("const") + description = json_schema.pop("description", None) if description is not None: strict_schema["description"] = description diff --git a/tests/lib/_parse/test_transform.py b/tests/lib/_parse/test_transform.py index 7a2799dce..adfd8e816 100644 --- a/tests/lib/_parse/test_transform.py +++ b/tests/lib/_parse/test_transform.py @@ -73,6 +73,22 @@ def test_enum_schema(): assert result == snapshot({"type": "string", "enum": ["foo", "bar"]}) +def test_const_schema(): + # Pydantic v2 emits `const` for single-value `Literal` fields, e.g. + # `Literal["ok"]` -> `{"type": "string", "const": "ok"}`. + schema = {"type": "string", "const": "ok"} + result = transform_schema(schema) + assert result == snapshot({"type": "string", "const": "ok"}) + + +def test_const_null_schema(): + # A present `const: None` constraint must not be confused with an + # absent `const` key. + schema = {"type": "null", "const": None} + result = transform_schema(schema) + assert result == snapshot({"type": "null", "const": None}) + + def test_allof(): schema = { "allOf": [