Logfire
Observability platform from Pydantic — structured logging, tracing, and metrics built on OpenTelemetry. Current version is 4.30.0. Must call logfire.configure() before any instrumentation or spans. Requires LOGFIRE_TOKEN env var for cloud backend. The old pydantic-logfire package is a deprecated stub — use logfire directly. Pricing changed January 2026.
Common errors
-
ModuleNotFoundError: No module named 'opentelemetry'
cause The 'logfire' library relies on OpenTelemetry components, and this error indicates that the necessary 'opentelemetry-sdk' package or its dependencies are not installed in the environment.fixInstall the OpenTelemetry SDK: `pip install opentelemetry-sdk` or ensure a fresh `logfire` installation handles all its dependencies correctly. -
ValueError: LOGFIRE_TOKEN environment variable must be provided.
cause The Logfire SDK requires an authentication token to connect to the Logfire platform, which can be provided via the `LOGFIRE_TOKEN` environment variable or directly in `logfire.configure()`.fixSet the `LOGFIRE_TOKEN` environment variable (e.g., `export LOGFIRE_TOKEN='your_token_here'`) or pass the token explicitly when configuring Logfire: `logfire.configure(token='your_token_here')`. -
ModuleNotFoundError: No module named 'pydantic_logfire'
cause Developers might mistakenly try to import from the deprecated `pydantic-logfire` package instead of the current `logfire` package.fixEnsure you have installed the `logfire` package (not `pydantic-logfire`) and update your import statements to `import logfire`. -
ModuleNotFoundError: No module named 'pytest'
cause In some older versions of `logfire` (e.g., 0.50), `pytest` was an unexpected transitive dependency, particularly if `logfire.testing` was imported indirectly.fixInstall `pytest` (`pip install pytest`) or upgrade `logfire` to a newer version where this dependency issue may have been resolved, especially if you are not directly using `logfire.testing`.
Warnings
- breaking logfire.configure() must be called before any spans, logs, or instrument_*() calls. Omitting it causes all telemetry to be silently dropped — no error is raised.
- breaking instrument_httpx() parameters changed: capture_request_json_body, capture_request_text_body, capture_request_form_data, and capture_response_json_body were removed and replaced by capture_request_body and capture_response_body.
- breaking install_auto_tracing() requires explicit modules parameter — there is no default. Calling it without modules raises TypeError.
- gotcha pydantic-logfire on PyPI is a deprecated stub package that just depends on logfire. Use pip install logfire directly.
- gotcha Logfire pricing changed January 2026. Free Personal plan now limited to 1 seat and 3 projects. Teams exceeding these limits needed to upgrade by February 1, 2026 or be auto-upgraded.
- breaking logfire.configure() raises LogfireConfigError if LOGFIRE_TOKEN is missing or invalid, or `logfire auth` has not been run, preventing cloud export and application startup. It does not silently default to local console output.
Install
-
pip install logfire -
pip install logfire[fastapi] -
pip install logfire[httpx,requests,sqlalchemy,asyncpg] -
pip install pydantic-logfire
Imports
- logfire.configure
import logfire # Missing configure() before use — spans not exported: logfire.info('This message is not exported') # silently dropped # Old pydantic-logfire import (deprecated): from pydantic_logfire import configure # ImportError — use import logfireimport logfire # Must call configure() FIRST — before any spans or instrumentation # Reads LOGFIRE_TOKEN env var automatically if set logfire.configure() # Then instrument libraries logfire.instrument_fastapi(app) logfire.instrument_httpx() logfire.instrument_sqlalchemy(engine) # Structured logging logfire.info('User {user_id} logged in', user_id=123) # Manual spans with logfire.span('Processing order {order_id}', order_id=order.id): result = process(order)
Quickstart
import logfire
import os
# Set env var: export LOGFIRE_TOKEN='your-token'
# Or pass directly (not recommended for production):
logfire.configure(
token=os.environ.get('LOGFIRE_TOKEN'),
service_name='my-service',
environment='production'
)
# Structured log with attributes
logfire.info('Processing request', user_id=42, endpoint='/api/data')
logfire.warning('Rate limit approaching', limit=100, current=95)
logfire.error('Database error', error_type='ConnectionTimeout', db='postgres')
# Span (traces an operation)
with logfire.span('Fetch user data {user_id}', user_id=42):
# Code here is traced
data = fetch_user(42)
logfire.info('Fetched {record_count} records', record_count=len(data))
# FastAPI integration
from fastapi import FastAPI
app = FastAPI()
logfire.instrument_fastapi(app) # auto-traces all routes