Logfire Guide for Instrumenting Pydantic Models
Integration for instrumenting Pydantic models.
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
Settings for the logfire integration.
Type: LogfireSettings
Bases: TypedDict
Settings for the logfire integration.
The sample rate to use for tracing.
Type: float
Tags to add to the spans.
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’]
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.
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.
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.
The schema to validate against.
schema_type : Any
The original type which the schema was created from, e.g. the model class.
Path defining where schema_type was defined, or where TypeAdapter was called.
The kind of schema to validate against.
config : CoreConfig | None
The config to use for validation.
The plugin settings.
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
schemaproperty. - Otherwise use the schema’s
typeproperty.
str — The name of the schema.
The schema to get the name for.
def get_pydantic_plugin_config() -> PydanticPlugin
Get the Pydantic plugin config.
def set_pydantic_plugin_config(plugin_config: PydanticPlugin | None) -> None
Set the pydantic plugin config.