Skip to content

feat(llmobs): [MLOB-2989] add pydantic_ai instrumentation #13621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Jul 1, 2025
Merged

Conversation

Kyle-Verhoog
Copy link
Member

@Kyle-Verhoog Kyle-Verhoog commented Jun 6, 2025

This PR implements APM tracing for the PydanticAI library. Currently, the following methods are traced:

  • agent.Agent.iter: captures all ways of running the agent, including agent.run, agent.run_sync, agent.run_stream, and agent.iter
  • tools.Tool.run: captures tool invocations

The only llmobs related tags set on these spans are pydantic_ai.request.model and pydantic_ai.request.provider (for Pydantic Agent spans only).

Below is an example script using PydanticAI for a simple math problem. Running this script with DD_SERVICE="nicole-test" DD_ENV="nicole-test" ddtrace-run python3 agent_iter_example.py produces the following APM trace with a span representing the agent invocation, a span representing the tool invocation, and two Open AI LLM calls that occurred during the execution.

import asyncio
from pydantic_ai import Agent

def calculate_square(x: int) -> int:
    return x * x

agent = Agent(  
    'openai:gpt-4o',
    system_prompt='Use one of the tools provided to calculate the mathematical operation.',  
    tools=[calculate_square],
)

async def main():
    async with agent.iter('What is the square of 5?') as response:
        async for _ in response:
            pass

if __name__ == '__main__':
    asyncio.run(main())

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

Copy link
Contributor

github-actions bot commented Jun 6, 2025

CODEOWNERS have been resolved as:

.riot/requirements/111353f.txt                                          @DataDog/apm-python
.riot/requirements/14191ba.txt                                          @DataDog/apm-python
.riot/requirements/16cf7dd.txt                                          @DataDog/apm-python
.riot/requirements/1721127.txt                                          @DataDog/apm-python
.riot/requirements/187928c.txt                                          @DataDog/apm-python
.riot/requirements/1a98a60.txt                                          @DataDog/apm-python
.riot/requirements/2c6ccbc.txt                                          @DataDog/apm-python
.riot/requirements/3459dc4.txt                                          @DataDog/apm-python
.riot/requirements/d7a340e.txt                                          @DataDog/apm-python
.riot/requirements/f346262.txt                                          @DataDog/apm-python
ddtrace/contrib/_pydantic_ai.py                                         @DataDog/ml-observability
ddtrace/contrib/internal/pydantic_ai/patch.py                           @DataDog/ml-observability
ddtrace/contrib/internal/pydantic_ai/utils.py                           @DataDog/ml-observability
ddtrace/llmobs/_integrations/pydantic_ai.py                             @DataDog/ml-observability
releasenotes/notes/pydantic-ai-apm-integration-279845b06ab611dc.yaml    @DataDog/apm-python
tests/contrib/pydantic_ai/__init__.py                                   @DataDog/ml-observability
tests/contrib/pydantic_ai/cassettes/agent_iter.yaml                     @DataDog/ml-observability
tests/contrib/pydantic_ai/cassettes/agent_run_stream.yaml               @DataDog/ml-observability
tests/contrib/pydantic_ai/cassettes/agent_with_tools.yaml               @DataDog/ml-observability
tests/contrib/pydantic_ai/conftest.py                                   @DataDog/ml-observability
tests/contrib/pydantic_ai/test_pydantic_ai.py                           @DataDog/ml-observability
tests/contrib/pydantic_ai/test_pydantic_ai_patch.py                     @DataDog/ml-observability
tests/snapshots/tests.contrib.pydantic_ai.test_pydantic_ai.test_agent_run.json  @DataDog/apm-python
tests/snapshots/tests.contrib.pydantic_ai.test_pydantic_ai.test_agent_run_error.json  @DataDog/apm-python
tests/snapshots/tests.contrib.pydantic_ai.test_pydantic_ai.test_agent_with_tools.json  @DataDog/apm-python
.github/CODEOWNERS                                                      @DataDog/python-guild @DataDog/apm-core-python
ddtrace/_monkey.py                                                      @DataDog/apm-core-python
ddtrace/contrib/integration_registry/registry.yaml                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/llmobs/_llmobs.py                                               @DataDog/ml-observability
ddtrace/settings/_config.py                                             @DataDog/apm-core-python
riotfile.py                                                             @DataDog/apm-python
supported_versions_output.json                                          @DataDog/apm-core-python
supported_versions_table.csv                                            @DataDog/apm-core-python
tests/contrib/openai/test_openai_v1.py                                  @DataDog/ml-observability
tests/llmobs/suitespec.yml                                              @DataDog/ml-observability

Copy link
Contributor

github-actions bot commented Jun 6, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 274 ± 2 ms.

The average import time from base is: 276 ± 2 ms.

The import time difference between this PR and base is: -1.16 ± 0.09 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 1.923 ms (0.70%)
ddtrace.bootstrap.sitecustomize 1.247 ms (0.45%)
ddtrace.bootstrap.preload 1.247 ms (0.45%)
ddtrace.internal.remoteconfig.client 0.642 ms (0.23%)
ddtrace 0.676 ms (0.25%)
ddtrace.internal._unpatched 0.031 ms (0.01%)
json 0.031 ms (0.01%)
json.decoder 0.031 ms (0.01%)
re 0.031 ms (0.01%)
enum 0.031 ms (0.01%)
types 0.031 ms (0.01%)

@pr-commenter
Copy link

pr-commenter bot commented Jun 25, 2025

Benchmarks

Benchmark execution time: 2025-06-30 16:18:14

Comparing candidate commit 5120620 in PR branch kylev/pydantic with baseline commit 4b87ee8 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 569 metrics, 3 unstable metrics.

@ncybul ncybul changed the title feat(llmobs): add pydantic_ai instrumentation feat(llmobs): [MLOB-2989] add pydantic_ai instrumentation Jun 25, 2025
Copy link
Member Author

@Kyle-Verhoog Kyle-Verhoog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one thought about the span type and kind but otherwise so happy with how clean this was! nice work 😄

@ncybul ncybul marked this pull request as ready for review June 26, 2025 17:50
@ncybul ncybul requested review from a team as code owners June 26, 2025 17:50
Copy link
Member

@brettlangdon brettlangdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a thought, otherwise the files owned by core lgtm

@ncybul ncybul closed this Jul 1, 2025
@ncybul ncybul reopened this Jul 1, 2025
@ncybul ncybul merged commit f4ac9fd into main Jul 1, 2025
905 checks passed
@ncybul ncybul deleted the kylev/pydantic branch July 1, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants