new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Pydantic · Integrations · all subjects

logfire integration

15 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Logfire MCP server provides runtime data to AI tools

The Logfire MCP server gives AI tools access to an application's runtime data, including traces, metrics, and recorded validations. An agent can query this data from your service to, for example, retrieve the input behind a ValidationError while assisting with debugging.

Logfire records failed Pydantic validations with structured errors

Logfire records failed Pydantic validations with their structured errors and can keep them inside the surrounding request or job trace, so you can see what failed, where the input came from, and whether the same problem keeps happening.

Install and authenticate Logfire

Install Logfire with 'pip install logfire' and then run 'logfire auth' from your project directory to set up authentication.

Call instrument_pydantic() before defining or importing models

Call logfire.instrument_pydantic() before defining or importing the models you want to monitor. This must be done after logfire.configure().

instrument_pydantic() record parameter settings

The 'record' argument to logfire.instrument_pydantic() controls what gets recorded. Setting 'failure' records only failed validations as individual records while keeping all validations as metrics. Setting 'all' (default) records every successful and failed validation as individual records while keeping all validations as metrics. Setting 'metrics' records no individual records, only all validations as metrics. Setting 'off' records nothing.

Use record='failure' for production, record='all' for development

Use 'failure' for production troubleshooting without creating an individual record for every successful validation. Use 'all' while developing when you want to inspect successful inputs and validated results too.

Logfire scrubs sensitive values before export

Logfire scrubs common sensitive values before export, but stores every rejected value under the key 'input' inside the serialized 'errors' attribute, separately from its field path. If those values can contain secrets or personal data, pass 'scrubbing=logfire.ScrubbingOptions(extra_patterns=[r"(?:^input$|\"input\"\s*:)"])' to logfire.configure() to scrub validation error input values.

Pydantic validation data privacy consideration with Logfire

Failed-validation records contain the rejected values from Pydantic's structured errors. When exporting these records, review the validation data carefully as it may contain secrets or personal data.

Instrument web frameworks to see request context for failed validations

Instrument your web framework, database client, or task queue to see where invalid values came from and what happened around them. Logfire provides integrations for FastAPI, Django, Celery, SQLAlchemy, HTTPX, and more.

Log a validated Pydantic model explicitly with Logfire

You can attach a Pydantic model to your own structured log or span using logfire.info(). Logfire preserves the model's fields so you can inspect and query them.

Example: instrument_pydantic with record='failure'

from datetime import date import logfire from pydantic import BaseModel logfire.configure() logfire.instrument_pydantic(record='failure') class User(BaseModel): name: str country_code: str dob: date User(name='Anne', country_code='USA', dob='not-a-date')

Example: log a validated Pydantic model explicitly

from datetime import date import logfire from pydantic import BaseModel logfire.configure() class User(BaseModel): name: str country_code: str dob: date user = User(name='Anne', country_code='USA', dob='2000-01-01') logfire.info('user processed: {user!r}', user=user)

Troubleshooting: no validation records appear

If no validation records appear, make sure logfire.configure() runs and that instrument_pydantic() runs before the model class is defined or imported.

Troubleshooting: successful validations do not appear

If successful validations do not appear, record='failure' keeps them as metrics only. Use record='all' when you need an individual span for each success.

Troubleshooting: inspecting successful validations requires record='all'

To inspect successful validations, use record='all'. This creates an individual span for every validation, so review its data-volume and privacy implications before using it in production.

Give your agent this brain