|
Formatron v0.4.9
Formatron empowers everyone to control the output format of language models with minimal overhead.
|
This module contains utilities for creating schemas from JSON schemas. More...
Classes | |
| class | FieldInfo |
Functions | |
| schemas.schema.Schema | create_schema (dict[str, typing.Any] schema, registry=Registry()) |
| Create a Schema object from a JSON schema object. | |
| str | _resolve_new_url (str uri, str ref) |
| Adapted from https://github.com/python-jsonschema/referencing/blob/main/referencing/_core.py#L667. | |
| None | _validate_json_schema (dict[str, typing.Any] schema) |
| typing.Type | _convert_json_schema_to_our_schema (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| Recursively handle all types needed to fully determine the type of a schema. | |
| _extract_fields_from_object_type (typing.Type object_type) | |
| typing.Type | _handle_anyOf (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| typing.Type[typing.Any|None] | _infer_type (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| Infer more specific types. | |
| typing.Any | _get_literal (dict[str, typing.Any] schema) |
| typing.Type | _handle_literal (typing.Any literal, typing.Type obtained_type, dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| typing.Type | _handle_str_with_metadata (dict[str, typing.Any] schema) |
| Handle string type with metadata such as maxLength, minLength, and pattern. | |
| typing.Type | _handle_numeric_with_metadata (dict[str, typing.Any] schema, typing.Type numeric_type) |
| Handle numeric types (int or float) with metadata such as minimum, maximum, exclusiveMinimum, and exclusiveMaximum. | |
| typing.Type | _create_custom_type (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| typing.Type | _handle_list_metadata (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| Handle cases where the obtained type is a list. | |
| typing.Type[typing.Any|None] | _obtain_type (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema) |
| Directly obtain type information from this schema's type keyword. | |
| _merge_referenced_schema (dict[str, typing.Any] schema, set[int] memo) | |
| _merge_key (dict[str, typing.Any] schema, str ref_key, typing.Any reference_value) | |
| _recursive_resolve_reference (str base_uri, typing.Any schema, Registry registry, set[int] memo) | |
| _resolve_reference (dict[str, typing.Any] schema, str key, typing.Any resolver) | |
Variables | |
| int | _counter |
This module contains utilities for creating schemas from JSON schemas.
|
protected |
Recursively handle all types needed to fully determine the type of a schema.
Definition at line 156 of file json_schema.py.
|
protected |
Definition at line 280 of file json_schema.py.
|
protected |
Definition at line 173 of file json_schema.py.
|
protected |
Definition at line 225 of file json_schema.py.
|
protected |
Definition at line 180 of file json_schema.py.
|
protected |
Handle cases where the obtained type is a list.
Definition at line 296 of file json_schema.py.
|
protected |
Definition at line 230 of file json_schema.py.
|
protected |
Handle numeric types (int or float) with metadata such as minimum, maximum, exclusiveMinimum, and exclusiveMaximum.
Definition at line 263 of file json_schema.py.
|
protected |
Handle string type with metadata such as maxLength, minLength, and pattern.
Definition at line 243 of file json_schema.py.
|
protected |
Infer more specific types.
Definition at line 193 of file json_schema.py.
|
protected |
Definition at line 380 of file json_schema.py.
|
protected |
Definition at line 361 of file json_schema.py.
|
protected |
Directly obtain type information from this schema's type keyword.
Definition at line 324 of file json_schema.py.
|
protected |
Definition at line 399 of file json_schema.py.
|
protected |
Adapted from https://github.com/python-jsonschema/referencing/blob/main/referencing/_core.py#L667.
Definition at line 133 of file json_schema.py.
|
protected |
Definition at line 421 of file json_schema.py.
|
protected |
Definition at line 138 of file json_schema.py.
| schemas.schema.Schema formatron.schemas.json_schema.create_schema | ( | dict[str, typing.Any] | schema, |
| registry = Registry() ) |
Create a Schema object from a JSON schema object.
This function takes a JSON schema and converts it into a Schema object that can be used for data validation and serialization. Currently, only the following JSON Schema features are supported:
type keywordminLength, maxLength, pattern keywords for string typesubstringOf keyword for string typeminimum, maximum, exclusiveMinimum, exclusiveMaximum keywords for number type and integer typeitems keywordminItems, maxItems, prefixItems constraintsproperties keywordadditionalProperties is false.properties is optional for object type.enum and const keywordenum(or const) and type are present, type will be ignored.required keywordanyOf keyword$defs, $id, $anchor, $dynamicAnchor) are supported.| schema | A dictionary representing a valid JSON schema. |
| registry | A Registry object containing additional schema definitions. Defaults to an empty Registry. |
| jsonschema | If the input schema is not a valid JSON Schema. |
| ValueError | If there are issues with schema references, constraints or requirements. |
Definition at line 115 of file json_schema.py.
|
protected |
Definition at line 67 of file json_schema.py.