Skip to main content
Version: 0.0.6 (stable)

agrag.schema.formats.pydantic_models.from_pydantic

from_pydantic(models:Sequence[type[BaseModel]], *, name:str = 'custom', version:str = '1') -> ImportResult

Read a schema from Pydantic model classes.

Each class is an entity type: the class name is the label and the docstring is the description. A base class in models becomes a parent, and the child keeps only the fields it adds or overrides. A field typed as another class in models, alone or in a list, is a relation named after the field in upper case, from the class to the field type. Any other field is a property. A field without a default, and not optional, is required. A Literal or Enum field becomes a property with enum values. A single-valued relation field gets a limit of one per source, and max_length on a list field sets the limit. Field(description=...) is the description of a property or relation.

Parameters:

  • models (Sequence[type[BaseModel]]) – The classes to read. Every class that a field refers to must be in this sequence.
  • name (str) – The schema name.
  • version (str) – The schema version.

Returns:

  • ImportResult – The schema and a warning for each field or description it dropped.

Raises:

  • SchemaLoadError – The classes do not form a valid schema, for example a class named Chunk. The message lists every problem.