Skip to content

Conversation

@andrewm4894
Copy link
Contributor

@andrewm4894 andrewm4894 commented Dec 2, 2025

Summary

Add OpenTelemetry-based instrumentation for Pydantic AI agents. This provides a foundation for OTel-instrumented AI frameworks.

New modules:

  • posthog.ai.otel - Generic OTel SpanExporter that translates spans to PostHog events
  • posthog.ai.pydantic_ai - Pydantic AI-specific wrapper and setup function

Usage

from posthog import Posthog
from posthog.ai.pydantic_ai import instrument_pydantic_ai
from pydantic_ai import Agent

posthog = Posthog(project_api_key="phc_xxx", host="https://us.i.posthog.com")

# One-liner setup
instrument_pydantic_ai(posthog, distinct_id="user_123")

# Use Pydantic AI normally - automatically traced
agent = Agent("openai:gpt-4")
result = await agent.run("Hello")

Architecture

Pydantic AI Agent -> OTel Spans -> PydanticAISpanExporter -> PostHogSpanExporter -> PostHog Events
  • PostHogSpanExporter: Generic exporter following GenAI semantic conventions
  • PydanticAISpanExporter: Normalizes Pydantic AI message formats and tool attribute names

Changes

  • Add posthog/ai/otel/ with generic OTel exporter
  • Add posthog/ai/pydantic_ai/ with Pydantic AI wrapper and instrument_pydantic_ai()
  • Add optional dependencies: pip install posthog[pydantic-ai] or pip install posthog[otel]
  • Add 60 tests covering both modules

Test plan

Add OpenTelemetry-based instrumentation for Pydantic AI agents:

- PostHogSpanExporter: Generic OTel exporter that translates spans to PostHog events
- PydanticAISpanExporter: Wrapper that normalizes Pydantic AI message formats and tool attributes
- instrument_pydantic_ai(): One-liner setup function for easy integration

Usage:
    from posthog.ai.pydantic_ai import instrument_pydantic_ai
    instrument_pydantic_ai(posthog_client, distinct_id="user_123")

    agent = Agent("openai:gpt-4")
    result = await agent.run("Hello")  # Automatically traced

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

13 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

andrewm4894 and others added 10 commits December 2, 2025 16:29
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Agent spans now create $ai_trace events so that child spans (tools, etc.)
have a valid parent reference. Previously agent spans were skipped, which
caused orphaned $ai_parent_id references in tool spans.
@andrewm4894 andrewm4894 changed the title feat: Add Pydantic AI instrumentation via OpenTelemetry feat(llma): Add Pydantic AI instrumentation via OpenTelemetry Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants