Pydantic - Pydantic Logfire Documentation (original) (raw)

Logfire has a Pydantic plugin to instrument Pydantic models. The plugin provides logs and metrics about model validation.

To enable the plugin, do one of the following:

[tool.logfire] pydantic_plugin_record = "all"

`import logfire

logfire.instrument_pydantic() # Defaults to record='all' `

Note that if you only use the last option then only model classes defined and imported after calling logfire.instrument_pydanticwill be instrumented.

Note

Remember to call logfire.configure() at some point, whether before or after calling logfire.instrument_pydantic and defining model classes. Model validations will only start being logged after calling logfire.configure().

Third party modules

By default, third party modules are not instrumented by the plugin to avoid noise. You can enable instrumentation for those using the include configuration.

logfire.instrument_pydantic(include={'openai'})

You can also disable instrumentation for your own modules using theexclude configuration.

logfire.instrument_pydantic(exclude={'app.api.v1'})

Model configuration

If you want more granular control over the plugin, you can use theplugin_settings class parameter in your Pydantic models.

`from logfire.integrations.pydantic import PluginSettings from pydantic import BaseModel

class Foo(BaseModel, plugin_settings=PluginSettings(logfire={'record': 'failure'})): ... `

Record

The record argument is used to configure what to record. It can be one of the following values:

Tags

Tags are used to add additional information to the traces, and metrics. They can be included by adding the tags key inplugin_settings.

`from pydantic import BaseModel

class Foo( BaseModel, plugin_settings={'logfire': {'record': 'all', 'tags': ('tag1', 'tag2')}} ): `