transform_schema() — anthropic-sdk-python Function Reference
Architecture documentation for the transform_schema() function in _transform.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD 18c66e29_88d3_b5f9_0448_f30338a098a7["transform_schema()"] 69ef29cd_4eb8_35c9_a323_d7cf936be862["_transform.py"] 18c66e29_88d3_b5f9_0448_f30338a098a7 -->|defined in| 69ef29cd_4eb8_35c9_a323_d7cf936be862 style 18c66e29_88d3_b5f9_0448_f30338a098a7 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
src/anthropic/lib/_parse/_transform.py lines 54–167
def transform_schema(
json_schema: type[pydantic.BaseModel] | dict[str, Any],
) -> dict[str, Any]:
"""
Transforms a JSON schema to ensure it conforms to the API's expectations.
Args:
json_schema (Dict[str, Any]): The original JSON schema.
Returns:
The transformed JSON schema.
Examples:
>>> transform_schema(
... {
... "type": "integer",
... "minimum": 1,
... "maximum": 10,
... "description": "A number",
... }
... )
{'type': 'integer', 'description': 'A number\n\n{minimum: 1, maximum: 10}'}
"""
if inspect.isclass(json_schema) and issubclass(json_schema, pydantic.BaseModel): # pyright: ignore[reportUnnecessaryIsInstance]
json_schema = json_schema.model_json_schema()
strict_schema: dict[str, Any] = {}
json_schema = {**json_schema}
ref = json_schema.pop("$ref", None)
if ref is not None:
strict_schema["$ref"] = ref
return strict_schema
defs = json_schema.pop("$defs", None)
if defs is not None:
strict_defs: dict[str, Any] = {}
strict_schema["$defs"] = strict_defs
for name, schema in defs.items():
strict_defs[name] = transform_schema(schema)
type_: Optional[SupportedTypes] = json_schema.pop("type", None)
any_of = json_schema.pop("anyOf", None)
one_of = json_schema.pop("oneOf", None)
all_of = json_schema.pop("allOf", None)
if is_list(any_of):
strict_schema["anyOf"] = [transform_schema(cast("dict[str, Any]", variant)) for variant in any_of]
elif is_list(one_of):
strict_schema["anyOf"] = [transform_schema(cast("dict[str, Any]", variant)) for variant in one_of]
elif is_list(all_of):
strict_schema["allOf"] = [transform_schema(cast("dict[str, Any]", variant)) for variant in all_of]
else:
if type_ is None:
raise ValueError("Schema must have a 'type', 'anyOf', 'oneOf', or 'allOf' field.")
strict_schema["type"] = type_
description = json_schema.pop("description", None)
if description is not None:
strict_schema["description"] = description
title = json_schema.pop("title", None)
if title is not None:
strict_schema["title"] = title
if type_ == "object":
strict_schema["properties"] = {
key: transform_schema(prop_schema) for key, prop_schema in json_schema.pop("properties", {}).items()
}
json_schema.pop("additionalProperties", None)
strict_schema["additionalProperties"] = False
required = json_schema.pop("required", None)
if required is not None:
strict_schema["required"] = required
elif type_ == "string":
format = json_schema.pop("format", None)
if format and format in SupportedStringFormats:
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does transform_schema() do?
transform_schema() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/lib/_parse/_transform.py.
Where is transform_schema() defined?
transform_schema() is defined in src/anthropic/lib/_parse/_transform.py at line 54.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free