Formatron v0.4.2
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
formatron.schemas.json_schema Namespace Reference

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[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 _create_custom_type (typing.Type|None obtained_type, dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 
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 level.
 
 _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
 

Detailed Description

This module contains utilities for creating schemas from JSON schemas.

Function Documentation

◆ _convert_json_schema_to_our_schema()

typing.Type formatron.schemas.json_schema._convert_json_schema_to_our_schema ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Recursively handle all types needed to fully determine the type of a schema.

Definition at line 140 of file json_schema.py.

◆ _create_custom_type()

typing.Type formatron.schemas.json_schema._create_custom_type ( typing.Type|None obtained_type,
dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Definition at line 193 of file json_schema.py.

◆ _extract_fields_from_object_type()

formatron.schemas.json_schema._extract_fields_from_object_type ( typing.Type object_type)
protected

Definition at line 157 of file json_schema.py.

◆ _get_literal()

typing.Any formatron.schemas.json_schema._get_literal ( dict[str, typing.Any] schema)
protected

Definition at line 180 of file json_schema.py.

◆ _handle_literal()

typing.Type formatron.schemas.json_schema._handle_literal ( typing.Any literal,
typing.Type obtained_type,
dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Definition at line 185 of file json_schema.py.

◆ _infer_type()

typing.Type[typing.Any | None] formatron.schemas.json_schema._infer_type ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Infer more specific types.

Definition at line 169 of file json_schema.py.

◆ _merge_key()

formatron.schemas.json_schema._merge_key ( dict[str, typing.Any] schema,
str ref_key,
typing.Any reference_value )
protected

Definition at line 273 of file json_schema.py.

◆ _merge_referenced_schema()

formatron.schemas.json_schema._merge_referenced_schema ( dict[str, typing.Any] schema,
set[int] memo )
protected

Definition at line 254 of file json_schema.py.

◆ _obtain_type()

typing.Type[typing.Any|None] formatron.schemas.json_schema._obtain_type ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Directly obtain type information from this schema level.

Definition at line 216 of file json_schema.py.

◆ _recursive_resolve_reference()

formatron.schemas.json_schema._recursive_resolve_reference ( str base_uri,
typing.Any schema,
Registry registry,
set[int] memo )
protected

Definition at line 292 of file json_schema.py.

◆ _resolve_new_url()

str formatron.schemas.json_schema._resolve_new_url ( str uri,
str ref )
protected

◆ _resolve_reference()

formatron.schemas.json_schema._resolve_reference ( dict[str, typing.Any] schema,
str key,
typing.Any resolver )
protected

Definition at line 314 of file json_schema.py.

◆ _validate_json_schema()

None formatron.schemas.json_schema._validate_json_schema ( dict[str, typing.Any] schema)
protected

Definition at line 130 of file json_schema.py.

◆ create_schema()

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 keyword
  • items keyword
  • properties keyword
    • Due to implementation limitations, we always assume additionalProperties is false.
  • enum and const keyword
    • This includes advanced enum types such as array and object.
    • Note that if both enum(or const) and type are present, type will be ignored.
  • required keyword
  • Schema references ($ref and $dynamicRef)
    • Hence, all types of schema identifications($defs, $id, $anchor, $dynamicAnchor) are supported.
    • This includes recursive schema references.
    • Due to implementation limitations, duplicate constraint keywords in both referrers and referents are not allowed.
      • This bound is expected to be loosened in future versions of Formatron where "easily mergeable" constraint keywords will be merged.
Requirements
  • The input schema must be a valid JSON Schema according to the JSON Schema Draft 2020-12 standard
  • The root schema's type must be exactly "object"
  • The schema must have a valid '$id' and '$schema' fields
  • All references must be resolvable within the given schema and registry
Parameters
schemaA dictionary representing a valid JSON schema.
registryA Registry object containing additional schema definitions. Defaults to an empty Registry.
Returns
schemas A Schema object representing the input JSON schema.
Exceptions
jsonschemaIf the input schema is not a valid JSON Schema.
ValueErrorIf there are issues with schema references, constraints or requirements.

Definition at line 107 of file json_schema.py.

Variable Documentation

◆ _counter

int formatron.schemas.json_schema._counter
protected

Definition at line 67 of file json_schema.py.