Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@
"langsmith/trace-with-instructor",
"langsmith/trace-with-openai-agents-sdk",
"langsmith/trace-with-opentelemetry",
"langsmith/trace-with-pydantic-ai",
"langsmith/trace-with-semantic-kernel",
"langsmith/trace-with-vercel-ai-sdk"
]
Expand Down
Binary file added src/langsmith/images/pydantic-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/langsmith/integrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ mode: wide
icon="/langsmith/images/opentelemetry-icon.svg"
href="/langsmith/trace-with-opentelemetry" horizontal />

<Card
title="PydanticAI"
icon="/langsmith/images/pydantic-icon.png"
href="/langsmith/trace-with-pydantic-ai" horizontal />

<Card
title="Semantic Kernel"
icon="/langsmith/images/microsoft-icon.svg"
Expand Down
125 changes: 125 additions & 0 deletions src/langsmith/trace-with-pydantic-ai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: Trace with PydanticAI
sidebarTitle: PydanticAI
---

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.

## Installation

Install the required packages:

<CodeGroup>

```bash pip
pip install langsmith pydantic-ai opentelemetry-exporter-otlp
```

```bash uv
uv add langsmith pydantic-ai opentelemetry-exporter-otlp
```

</CodeGroup>

<Info>
Requires LangSmith Python SDK version `langsmith>=0.4.26` for optimal OpenTelemetry support.
</Info>

## Setup

### 1. Configure environment variables

Set your [API keys](/langsmith/create-account-api-key) and project name:

```bash
export LANGSMITH_API_KEY=<your_langsmith_api_key>
export LANGSMITH_PROJECT=<your_project_name>
export OPENAI_API_KEY=<your_openai_api_key>
```

### 2. Configure OpenTelemetry integration

In your PydanticAI application, configure the LangSmith OpenTelemetry integration:

```python
from langsmith.integrations.otel import configure
from pydantic_ai import Agent

# Configure LangSmith tracing
configure(project_name="pydantic-ai-demo")

# Instrument all PydanticAI agents
Agent.instrument_all()
```

<Note>
You do not need to set any OpenTelemetry environment variables or configure exporters manually—`configure()` handles everything automatically.
</Note>

### 3. Create and run your PydanticAI agent

Once configured, your PydanticAI agents will automatically send traces to LangSmith:

```python
from langsmith.integrations.otel import configure
from pydantic_ai import Agent

# Configure LangSmith tracing
configure(project_name="pydantic-ai-demo")

# Instrument all PydanticAI agents
Agent.instrument_all()

# Create and run an agent
agent = Agent('openai:gpt-4o')
result = agent.run_sync('What is the capital of France?')
print(result.output)
#> Paris
```

## Advanced usage

### Custom metadata and tags

You can add custom metadata to your traces using OpenTelemetry span attributes:

```python
from opentelemetry import trace
from pydantic_ai import Agent
from langsmith.integrations.otel import configure

configure(project_name="pydantic-ai-metadata")
Agent.instrument_all()

tracer = trace.get_tracer(__name__)

agent = Agent('openai:gpt-4o')

with tracer.start_as_current_span("pydantic_ai_workflow") as span:
span.set_attribute("langsmith.metadata.user_id", "user_123")
span.set_attribute("langsmith.metadata.workflow_type", "question_answering")
span.set_attribute("langsmith.span.tags", "pydantic-ai,production")

result = agent.run_sync('Explain quantum computing in simple terms')
print(result.output)
```

### Combine with other instrumentors

You can combine PydanticAI instrumentation with other OpenTelemetry instrumentors:

```python
from langsmith.integrations.otel import configure
from pydantic_ai import Agent
from openinference.instrumentation.openai import OpenAIInstrumentor

# Configure LangSmith tracing
configure(project_name="multi-framework-app")

# Initialize multiple instrumentors
Agent.instrument_all()
OpenAIInstrumentor().instrument()

# Your application code using multiple frameworks
```

Loading