From 3e7ce90b6d3013613df9e83f4b1ba41d8c350245 Mon Sep 17 00:00:00 2001 From: perinim Date: Wed, 26 Nov 2025 13:35:30 +0100 Subject: [PATCH 1/3] feat: pydantic ai integration --- src/docs.json | 1 + src/langsmith/integrations.mdx | 5 + src/langsmith/trace-with-pydantic-ai.mdx | 125 +++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 src/langsmith/trace-with-pydantic-ai.mdx 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/integrations.mdx b/src/langsmith/integrations.mdx index d0ca2a4059..995b53fec3 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 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) +``` + +### Combining 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 +``` + From 59ae2305390d3c1249ec9f77f23ce4dd8429e612 Mon Sep 17 00:00:00 2001 From: Kathryn May Date: Mon, 1 Dec 2025 08:56:55 -0500 Subject: [PATCH 2/3] Add icon image --- src/langsmith/images/pydantic-icon.png | Bin 0 -> 2114 bytes src/langsmith/integrations.mdx | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 src/langsmith/images/pydantic-icon.png diff --git a/src/langsmith/images/pydantic-icon.png b/src/langsmith/images/pydantic-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..95e41b5d7a318609822079990d2623c240938095 GIT binary patch literal 2114 zcmV-I2)*}-P)O6;N-YF&O10!qDHM^m5N3s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oWMGeS!8=K)e44<2W2<@Lp0}qdih6@$yY{(CeSX*&G<#=LQBL z2m_&hLKorVpXM(g$2cxK+e!;b(}{9N^SNHunkLjSqqnmc*_i493;Og#6hO)sjFn zfnYe!Q$x33D5DxbEOj&$S$xhuGr@{ELG)aSYL1ae$>-}J1&oFK<+{^HHL#t6WEFHH>X2>%VDQnZPgv9_DynbRh&Rs*dpD!XCPL@)Q#bx@{b%KFk zSg)+F_uA|EF;)_W?1DOA2L#?^9`1za!jtkAyx|MHtlZn`^f4f`<{z;!Qjlz!p sKaa70vyL{#42(9$42(AZ9RL9T{~f9n=)o^}hX4Qo07*qoM6N<$g1_kx(EtDd literal 0 HcmV?d00001 diff --git a/src/langsmith/integrations.mdx b/src/langsmith/integrations.mdx index 995b53fec3..10f7fdf913 100644 --- a/src/langsmith/integrations.mdx +++ b/src/langsmith/integrations.mdx @@ -88,7 +88,7 @@ mode: wide Date: Mon, 1 Dec 2025 09:17:32 -0500 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Kathryn May <44557882+katmayb@users.noreply.github.com> --- src/langsmith/trace-with-pydantic-ai.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/langsmith/trace-with-pydantic-ai.mdx b/src/langsmith/trace-with-pydantic-ai.mdx index 2142b21667..291e4e1ee4 100644 --- a/src/langsmith/trace-with-pydantic-ai.mdx +++ b/src/langsmith/trace-with-pydantic-ai.mdx @@ -29,7 +29,7 @@ Requires LangSmith Python SDK version `langsmith>=0.4.26` for optimal OpenTeleme ### 1. Configure environment variables -Set your API keys and project name: +Set your [API keys](/langsmith/create-account-api-key) and project name: ```bash export LANGSMITH_API_KEY= @@ -104,7 +104,7 @@ with tracer.start_as_current_span("pydantic_ai_workflow") as span: print(result.output) ``` -### Combining with other instrumentors +### Combine with other instrumentors You can combine PydanticAI instrumentation with other OpenTelemetry instrumentors: