Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/anthropic/lib/_parse/_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/_parse/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down