JSON Schema API Reference¶
Auto-generated API documentation for the jsonschema module.
jsonschema
¶
JSON Schema flattening & sanitization — zero dependencies, stdlib only.
Flatten complex JSON Schemas containing $ref, allOf, anyOf, and
oneOf into simple, LLM-provider-compatible schemas. Designed for tool
schemas consumed by Anthropic, OpenAI, and Google GenAI APIs.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Example::
>>> from jsonschema import flatten_schema
>>> schema = {
... "type": "object",
... "properties": {
... "user": {"$ref": "#/$defs/User"},
... },
... "$defs": {
... "User": {
... "type": "object",
... "properties": {"name": {"type": "string"}},
... }
... },
... }
>>> flatten_schema(schema)
{'type': 'object', 'properties': {'user': {'type': 'object', 'properties': {'name': {'type': 'string'}}}}}
Pipeline::
resolve_refs → merge_allof → simplify_unions → sanitize
resolve_refs(schema)
¶
Resolve all local $ref pointers and inline their targets.
Supports any JSON Pointer fragment (RFC 6901), including
#/$defs/, #/definitions/, #/components/schemas/, etc.
After resolution, $defs and definitions maps are removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with all |
Source code in jsonschema/jsonschema.py
merge_allof(schema)
¶
Deep-merge all allOf sub-schemas into single schemas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict ( |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with all |
Source code in jsonschema/jsonschema.py
simplify_unions(schema)
¶
Simplify anyOf/oneOf constructs.
- Nullable pattern
[{type: T}, {type: null}]→{type: T, nullable: true} - Single-variant: unwrap.
- Multi-variant: keep first non-null variant (lossy but safe for LLM tool schemas).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with |
Source code in jsonschema/jsonschema.py
sanitize(schema, *, strip_keys=None)
¶
Strip unsupported schema keywords and validate required arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict. |
required |
strip_keys
|
set[str] | None
|
Additional keys to strip beyond
:data: |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with unsupported keys removed and |
dict[str, Any]
|
pruned so that |
Source code in jsonschema/jsonschema.py
flatten_schema(schema, *, strip_keys=None)
¶
One-call full pipeline: resolve → merge → simplify → sanitize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict, possibly containing |
required |
strip_keys
|
set[str] | None
|
Additional keys to strip beyond
:data: |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A flattened, sanitized schema safe for LLM provider consumption. |