Skip to content

Commit 023eb70

Browse files
PeriniMkatmayblnhsingh
authored
docs: pydantic ai integration example (#1633)
## Overview Added PydanticAI traceability page under integrations ## Type of change **Type:** New documentation page ## Checklist <!-- Put an 'x' in all boxes that apply --> - [x] I have read the [contributing guidelines](README.md) - [x] I have tested my changes locally using `docs dev` - [x] All code examples have been tested and work correctly - [x] I have used **root relative** paths for internal links - [x] I have updated navigation in `src/docs.json` if needed (Internal team members only / optional): Create a preview deployment as necessary using the [Create Preview Branch workflow](https://github.com/langchain-ai/docs/actions/workflows/create-preview-branch.yml) --------- Co-authored-by: Kathryn May <[email protected]> Co-authored-by: Lauren Hirata Singh <[email protected]> Co-authored-by: Kathryn May <[email protected]>
1 parent 9f401ec commit 023eb70

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

src/docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@
943943
"langsmith/trace-with-instructor",
944944
"langsmith/trace-with-openai-agents-sdk",
945945
"langsmith/trace-with-opentelemetry",
946+
"langsmith/trace-with-pydantic-ai",
946947
"langsmith/trace-with-semantic-kernel",
947948
"langsmith/trace-with-vercel-ai-sdk"
948949
]
2.06 KB
Loading

src/langsmith/integrations.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ mode: wide
8686
icon="/langsmith/images/opentelemetry-icon.svg"
8787
href="/langsmith/trace-with-opentelemetry" horizontal />
8888

89+
<Card
90+
title="PydanticAI"
91+
icon="/langsmith/images/pydantic-icon.png"
92+
href="/langsmith/trace-with-pydantic-ai" horizontal />
93+
8994
<Card
9095
title="Semantic Kernel"
9196
icon="/langsmith/images/microsoft-icon.svg"
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: Trace with PydanticAI
3+
sidebarTitle: PydanticAI
4+
---
5+
6+
LangSmith can capture traces generated by PydanticAI using its built-in OpenTelemetry instrumentation. This guide shows you how to automatically capture traces from your PydanticAI agents and send them to LangSmith for monitoring and analysis.
7+
8+
## Installation
9+
10+
Install the required packages:
11+
12+
<CodeGroup>
13+
14+
```bash pip
15+
pip install langsmith pydantic-ai opentelemetry-exporter-otlp
16+
```
17+
18+
```bash uv
19+
uv add langsmith pydantic-ai opentelemetry-exporter-otlp
20+
```
21+
22+
</CodeGroup>
23+
24+
<Info>
25+
Requires LangSmith Python SDK version `langsmith>=0.4.26` for optimal OpenTelemetry support.
26+
</Info>
27+
28+
## Setup
29+
30+
### 1. Configure environment variables
31+
32+
Set your [API keys](/langsmith/create-account-api-key) and project name:
33+
34+
```bash
35+
export LANGSMITH_API_KEY=<your_langsmith_api_key>
36+
export LANGSMITH_PROJECT=<your_project_name>
37+
export OPENAI_API_KEY=<your_openai_api_key>
38+
```
39+
40+
### 2. Configure OpenTelemetry integration
41+
42+
In your PydanticAI application, configure the LangSmith OpenTelemetry integration:
43+
44+
```python
45+
from langsmith.integrations.otel import configure
46+
from pydantic_ai import Agent
47+
48+
# Configure LangSmith tracing
49+
configure(project_name="pydantic-ai-demo")
50+
51+
# Instrument all PydanticAI agents
52+
Agent.instrument_all()
53+
```
54+
55+
<Note>
56+
You do not need to set any OpenTelemetry environment variables or configure exporters manually—`configure()` handles everything automatically.
57+
</Note>
58+
59+
### 3. Create and run your PydanticAI agent
60+
61+
Once configured, your PydanticAI agents will automatically send traces to LangSmith:
62+
63+
```python
64+
from langsmith.integrations.otel import configure
65+
from pydantic_ai import Agent
66+
67+
# Configure LangSmith tracing
68+
configure(project_name="pydantic-ai-demo")
69+
70+
# Instrument all PydanticAI agents
71+
Agent.instrument_all()
72+
73+
# Create and run an agent
74+
agent = Agent('openai:gpt-4o')
75+
result = agent.run_sync('What is the capital of France?')
76+
print(result.output)
77+
#> Paris
78+
```
79+
80+
## Advanced usage
81+
82+
### Custom metadata and tags
83+
84+
You can add custom metadata to your traces using OpenTelemetry span attributes:
85+
86+
```python
87+
from opentelemetry import trace
88+
from pydantic_ai import Agent
89+
from langsmith.integrations.otel import configure
90+
91+
configure(project_name="pydantic-ai-metadata")
92+
Agent.instrument_all()
93+
94+
tracer = trace.get_tracer(__name__)
95+
96+
agent = Agent('openai:gpt-4o')
97+
98+
with tracer.start_as_current_span("pydantic_ai_workflow") as span:
99+
span.set_attribute("langsmith.metadata.user_id", "user_123")
100+
span.set_attribute("langsmith.metadata.workflow_type", "question_answering")
101+
span.set_attribute("langsmith.span.tags", "pydantic-ai,production")
102+
103+
result = agent.run_sync('Explain quantum computing in simple terms')
104+
print(result.output)
105+
```
106+
107+
### Combine with other instrumentors
108+
109+
You can combine PydanticAI instrumentation with other OpenTelemetry instrumentors:
110+
111+
```python
112+
from langsmith.integrations.otel import configure
113+
from pydantic_ai import Agent
114+
from openinference.instrumentation.openai import OpenAIInstrumentor
115+
116+
# Configure LangSmith tracing
117+
configure(project_name="multi-framework-app")
118+
119+
# Initialize multiple instrumentors
120+
Agent.instrument_all()
121+
OpenAIInstrumentor().instrument()
122+
123+
# Your application code using multiple frameworks
124+
```
125+

0 commit comments

Comments
 (0)