diff --git a/src/docs.json b/src/docs.json index 2b61a4e4af..8349910511 100644 --- a/src/docs.json +++ b/src/docs.json @@ -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" ] diff --git a/src/langsmith/images/pydantic-icon.png b/src/langsmith/images/pydantic-icon.png new file mode 100644 index 0000000000..95e41b5d7a Binary files /dev/null and b/src/langsmith/images/pydantic-icon.png differ diff --git a/src/langsmith/integrations.mdx b/src/langsmith/integrations.mdx index d0ca2a4059..10f7fdf913 100644 --- a/src/langsmith/integrations.mdx +++ b/src/langsmith/integrations.mdx @@ -86,6 +86,11 @@ mode: wide icon="/langsmith/images/opentelemetry-icon.svg" href="/langsmith/trace-with-opentelemetry" horizontal /> + + + +```bash pip +pip install langsmith pydantic-ai opentelemetry-exporter-otlp +``` + +```bash uv +uv add langsmith pydantic-ai opentelemetry-exporter-otlp +``` + + + + +Requires LangSmith Python SDK version `langsmith>=0.4.26` for optimal OpenTelemetry support. + + +## Setup + +### 1. Configure environment variables + +Set your [API keys](/langsmith/create-account-api-key) and project name: + +```bash +export LANGSMITH_API_KEY= +export LANGSMITH_PROJECT= +export 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() +``` + + +You do not need to set any OpenTelemetry environment variables or configure exporters manually—`configure()` handles everything automatically. + + +### 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 +``` +