PydanticAI Instrumentation
Learn how to instrument PydanticAI applications with the LangWatch Python SDK.
PydanticAI is a library for building AI applications with Pydantic models. It features built-in, optional support for OpenTelemetry, allowing detailed tracing of agent runs and model interactions. LangWatch, being an OpenTelemetry-compatible observability platform, can seamlessly ingest these traces.
This guide explains how to configure PydanticAI and LangWatch to capture this observability data. For more background on PydanticAI’s observability features, refer to their debugging and monitoring documentation.
Prerequisites
-
Install LangWatch SDK:
-
Install PydanticAI:
Instrumenting PydanticAI with LangWatch
The primary way to integrate PydanticAI with LangWatch is by leveraging PydanticAI’s native OpenTelemetry emission in an environment configured by langwatch.setup()
.
Using PydanticAI’s Built-in OpenTelemetry with LangWatch Global Setup
When langwatch.setup()
is called, it initializes a global OpenTelemetry environment, including a trace exporter configured for LangWatch. If PydanticAI’s instrumentation is enabled (via Agent(instrument=True)
or Agent.instrument_all()
), it will emit OpenTelemetry traces that are automatically captured by LangWatch.
Key points for this approach:
langwatch.setup()
: Essential for initializing the OpenTelemetry environment that LangWatch uses.Agent(instrument=True)
orAgent.instrument_all()
: Activates PydanticAI’s OpenTelemetry signal emission.InstrumentationSettings(event_mode='logs')
: As per PydanticAI documentation, usingevent_mode='logs'
aligns message capture with OpenTelemetry Semantic Conventions for Generative AI, which might be better for generic OTel collectors. The default mode (json_array
) uses a custom attribute format for events.@langwatch.trace()
: Creates a parent trace in LangWatch, under which PydanticAI’s operation spans will be nested.
By following these steps, you can effectively monitor your PydanticAI applications using LangWatch, gaining insights into agent behavior, model performance, and overall application flow.