Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Comments

Close side panel

feat(llma): Add Pydantic AI instrumentation via OpenTelemetry#379

Draft
andrewm4894 wants to merge 14 commits intomasterPostHog/posthog-python:masterfrom
feature/pydantic-ai-otel-exporterPostHog/posthog-python:feature/pydantic-ai-otel-exporterCopy head branch name to clipboard
Draft

feat(llma): Add Pydantic AI instrumentation via OpenTelemetry#379
andrewm4894 wants to merge 14 commits intomasterPostHog/posthog-python:masterfrom
feature/pydantic-ai-otel-exporterPostHog/posthog-python:feature/pydantic-ai-otel-exporterCopy head branch name to clipboard

Conversation

@andrewm4894
Copy link
Member

@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 <noreply@anthropic.com>
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

posthog/ai/otel/exporter.py Outdated Show resolved Hide resolved
posthog/ai/otel/exporter.py Outdated Show resolved Hide resolved
posthog/ai/otel/exporter.py Outdated Show resolved Hide resolved
posthog/ai/otel/exporter.py Outdated Show resolved Hide resolved
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".

posthog/ai/pydantic_ai/__init__.py Outdated Show resolved Hide resolved
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
@andrewm4894 andrewm4894 marked this pull request as draft January 7, 2026 17:05
@rafaeelaudibert
Copy link
Member

We've updated our release process. We require sampo now. Please rebase on master and check README to understand what should be done.

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.