Skip to content

Logfire Guide for Instrumenting Pydantic Models

Integration for instrumenting Pydantic models.

PluginSettings

Bases: TypedDict

A typed dict for the Pydantic plugin settings.

This is how you can use the PluginSettings with a Pydantic model:

from pydantic import BaseModel

from logfire.integrations.pydantic import PluginSettings


class Model(BaseModel, plugin_settings=PluginSettings(logfire={'record': 'all'})):
    a: int

Attributes

logfire

Settings for the logfire integration.

Type: LogfireSettings

LogfireSettings

Bases: TypedDict

Settings for the logfire integration.

Attributes

trace_sample_rate

The sample rate to use for tracing.

Type: float

tags

Tags to add to the spans.

Type: list[str]

record

What to record.

The following values are supported:

  • all: Record all validation events.
  • failure: Record only validation failures.
  • metrics: Record only validation metrics.

Type: Literal[‘all’, ‘failure’, ‘metrics’]

LogfirePydanticPlugin

Implements a new API for pydantic plugins.

Patches Pydantic to accept this new API shape.

Set the LOGFIRE_PYDANTIC_RECORD environment variable to "off" to disable the plugin, or PYDANTIC_DISABLE_PLUGINS to true to disable all Pydantic plugins.

Methods

new_schema_validator

def new_schema_validator(
    schema: CoreSchema,
    schema_type: Any,
    schema_type_path: SchemaTypePath,
    schema_kind: SchemaKind,
    config: CoreConfig | None,
    plugin_settings: dict[str, Any],
) -> tuple[_ValidateWrapper, ...] | tuple[None, ...]

This method is called every time a new SchemaValidator is created.

Returns

tuple[_ValidateWrapper, …] | tuple[None, …] — A tuple of decorator factories for each of the three validation methods - validate_python, validate_json, validate_strings or a tuple of three None if recording is off.

Parameters

schema : CoreSchema

The schema to validate against.

schema_type : Any

The original type which the schema was created from, e.g. the model class.

schema_type_path : SchemaTypePath

Path defining where schema_type was defined, or where TypeAdapter was called.

schema_kind : SchemaKind

The kind of schema to validate against.

config : CoreConfig | None

The config to use for validation.

plugin_settings : dict[str, Any]

The plugin settings.

get_schema_name

def get_schema_name(schema: CoreSchema) -> str

Find the best name to use for a schema.

The follow rules are used:

  • If the schema represents a model or dataclass, use the name of the class.
  • If the root schema is a wrap/before/after validator, look at its schema property.
  • Otherwise use the schema’s type property.

Returns

str — The name of the schema.

Parameters

schema : CoreSchema

The schema to get the name for.

get_pydantic_plugin_config

def get_pydantic_plugin_config() -> PydanticPlugin

Get the Pydantic plugin config.

Returns

PydanticPlugin

set_pydantic_plugin_config

def set_pydantic_plugin_config(plugin_config: PydanticPlugin | None) -> None

Set the pydantic plugin config.

Returns

None