Pydantic — linkml documentation

# Pydantic

## Overview

The Pydantic Generator produces Pydantic flavored python dataclasses from a linkml model, with optional support for user-supplied jinja2 templates to generate alternate classes.

## Docs

### Command Line

#### gen-pydantic

Generate pydantic classes to represent a LinkML model

    gen-pydantic [OPTIONS] YAMLFILE

Options

--template_file <template_file>

  Optional jinja2 template to use for class generation

-f, --format <format>

  Output format (default=pydantic)

  Options

    pydantic

--metadata, --no-metadata

  Include metadata in output (default=–metadata)

--useuris, --metauris

  Include metadata in output (default=–useuris)

-im, --importmap <importmap>

  Import mapping file

--log_level <log_level>

  Logging level (default=WARNING)

  Options

    CRITICAL | ERROR | WARNING | INFO | DEBUG

--mergeimports, --no-mergeimports

  Merge imports into source file (default=mergeimports)

Arguments

YAMLFILE

  Required argument

### Code

class linkml.generators.pydanticgen.PydanticGenerator(schema: Union[str, TextIO, linkml_runtime.linkml_model.meta.SchemaDefinition], template_file: Optional[str] = None, format: str = 'pydantic', genmeta: bool = False, gen_classvars: bool = True, gen_slots: bool = True, **kwargs)
  serialize() → None

    Generate output in the required format

    Parameters

      kwargs – Generater specific parameters

    Returns

      Generated output

## Additional Notes

LinkML contains two Python generators. The Pydantic dataclass generator is specifically useful for FastAPI, but is newer and less full featured than the standard Python generator.

## Biolink Example

Begin by downloading the Biolink Model YAML and adding a virtual environment and installing linkml.

    curl -OJ https://raw.githubusercontent.com/biolink/biolink-model/master/biolink-model.yaml
    python3 -m venv venv
    source venv/bin/activate
    pip install linkml

Now generate the classes using the gen-pydantic command

    gen-pydantic biolink-model.yaml > biolink-model.py