> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 追踪 PydanticAI 应用

LangSmith 可以通过其内置的 OpenTelemetry 插桩来捕获 PydanticAI 生成的追踪数据。本指南将向您展示如何自动捕获来自 PydanticAI 代理的追踪数据，并将其发送到 LangSmith 进行监控和分析。

## 安装

安装所需的软件包：

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install langsmith pydantic-ai opentelemetry-exporter-otlp
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langsmith pydantic-ai opentelemetry-exporter-otlp
  ```
</CodeGroup>

<Info>
  需要 LangSmith Python SDK 版本 `langsmith>=0.4.26` 以获得最佳的 OpenTelemetry 支持。
</Info>

## 设置

### 1. 配置环境变量

设置您的 [API 密钥](/langsmith/create-account-api-key) 和项目名称：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export LANGSMITH_API_KEY=<your_langsmith_api_key>
export LANGSMITH_PROJECT=<your_project_name>
export OPENAI_API_KEY=<your_openai_api_key>
```

### 2. 配置 OpenTelemetry 集成

在您的 PydanticAI 应用程序中，配置 LangSmith OpenTelemetry 集成：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langsmith.integrations.otel import configure
from pydantic_ai import Agent

# 配置 LangSmith 追踪
configure(project_name="pydantic-ai-demo")

# 插桩所有 PydanticAI 代理
Agent.instrument_all()
```

<Note>
  您无需手动设置任何 OpenTelemetry 环境变量或配置导出器——`configure()` 会自动处理一切。
</Note>

### 3. 创建并运行您的 PydanticAI 代理

配置完成后，您的 PydanticAI 代理将自动向 LangSmith 发送追踪数据：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langsmith.integrations.otel import configure
from pydantic_ai import Agent

# 配置 LangSmith 追踪
configure(project_name="pydantic-ai-demo")

# 插桩所有 PydanticAI 代理
Agent.instrument_all()

# 创建并运行一个代理
agent = Agent('openai:gpt-5.4')
result = agent.run_sync('法国的首都是哪里？')
print(result.output)
#> 巴黎
```

## 高级用法

### 自定义元数据和标签

您可以使用 OpenTelemetry span 属性为追踪数据添加自定义元数据：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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-5.4')

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('用简单的话解释量子计算')
    print(result.output)
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等工具，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/trace-with-pydantic-ai.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
