Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2895fcd
feat(ai): add OpenAI Agents SDK integration
andrewm4894 Jan 14, 2026
3006664
feat(openai-agents): add $ai_group_id support for linking conversatio…
andrewm4894 Jan 14, 2026
945134a
feat(openai-agents): add enhanced span properties
andrewm4894 Jan 15, 2026
2445b40
Add $ai_framework property and standardize $ai_provider for OpenAI Ag…
andrewm4894 Jan 15, 2026
6193698
chore: bump version to 7.7.0 for OpenAI Agents SDK integration
andrewm4894 Jan 15, 2026
df0fcc0
fix: add openai_agents package to setuptools config
andrewm4894 Jan 27, 2026
6bf341a
fix: correct indentation in on_trace_start properties dict
andrewm4894 Jan 27, 2026
71069ed
fix: prevent unbounded growth of span/trace tracking dicts
andrewm4894 Jan 27, 2026
2f49c73
fix: resolve distinct_id from trace metadata in on_span_end
andrewm4894 Jan 27, 2026
8d7a68d
refactor: extract _base_properties helper to reduce duplication
andrewm4894 Jan 27, 2026
27bd98c
test: add missing edge case tests for openai agents processor
andrewm4894 Jan 27, 2026
143ff91
fix: handle non-dict error_info in span error parsing
andrewm4894 Jan 27, 2026
ae519ef
style: apply ruff formatting
andrewm4894 Jan 27, 2026
789be8d
style: replace lambda assignments with def (ruff E731)
andrewm4894 Jan 27, 2026
b3c631c
fix: restore full CHANGELOG.md history
andrewm4894 Jan 27, 2026
fea1420
fix: preserve personless mode for trace-id fallback distinct IDs
andrewm4894 Jan 27, 2026
fb4f1da
fix: restore changelog history and fix personless mode edge cases
andrewm4894 Jan 27, 2026
61d43e3
fix: handle None token counts in generation span
andrewm4894 Jan 27, 2026
b626a16
fix: check error_type_raw for all error categories
andrewm4894 Jan 27, 2026
b4a2d8b
fix: add type hints to instrument() function
andrewm4894 Jan 27, 2026
d4f4a3a
refactor: rename _safe_json to _ensure_serializable for clarity
andrewm4894 Jan 27, 2026
7a534de
refactor: emit $ai_trace at trace end instead of start
andrewm4894 Jan 27, 2026
ea6cba3
style: fix ruff formatting
andrewm4894 Jan 27, 2026
f239609
fix: add TYPE_CHECKING imports for type hints in instrument()
andrewm4894 Jan 27, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 7.7.0 - 2026-01-15

feat(ai): Add OpenAI Agents SDK integration

Automatic tracing for agent workflows, handoffs, tool calls, guardrails, and custom spans. Includes `$ai_total_tokens`, `$ai_error_type` categorization, and `$ai_framework` property.

# 7.6.0 - 2026-01-12

feat: add device_id to flags request payload
Expand Down
76 changes: 76 additions & 0 deletions posthog/ai/openai_agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union

if TYPE_CHECKING:
from agents.tracing import Trace

from posthog.client import Client

try:
import agents # noqa: F401
except ImportError:
raise ModuleNotFoundError(
"Please install the OpenAI Agents SDK to use this feature: 'pip install openai-agents'"
)

from posthog.ai.openai_agents.processor import PostHogTracingProcessor

__all__ = ["PostHogTracingProcessor", "instrument"]


def instrument(
Copy link
Member

Choose a reason for hiding this comment

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

Let's add type hints here too

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in b4a2d8b — added full type hints to instrument() matching the PostHogTracingProcessor.__init__ signature.

client: Optional[Client] = None,
distinct_id: Optional[Union[str, Callable[[Trace], Optional[str]]]] = None,
privacy_mode: bool = False,
groups: Optional[Dict[str, Any]] = None,
properties: Optional[Dict[str, Any]] = None,
) -> PostHogTracingProcessor:
"""
One-liner to instrument OpenAI Agents SDK with PostHog tracing.

This registers a PostHogTracingProcessor with the OpenAI Agents SDK,
automatically capturing traces, spans, and LLM generations.

Args:
client: Optional PostHog client instance. If not provided, uses the default client.
distinct_id: Optional distinct ID to associate with all traces.
Can also be a callable that takes a trace and returns a distinct ID.
privacy_mode: If True, redacts input/output content from events.
groups: Optional PostHog groups to associate with events.
properties: Optional additional properties to include with all events.

Returns:
PostHogTracingProcessor: The registered processor instance.

Example:
```python
from posthog.ai.openai_agents import instrument

# Simple setup
instrument(distinct_id="user@example.com")

# With custom properties
instrument(
distinct_id="user@example.com",
privacy_mode=True,
properties={"environment": "production"}
)

# Now run agents as normal - traces automatically sent to PostHog
from agents import Agent, Runner
agent = Agent(name="Assistant", instructions="You are helpful.")
result = Runner.run_sync(agent, "Hello!")
```
"""
from agents.tracing import add_trace_processor

processor = PostHogTracingProcessor(
client=client,
distinct_id=distinct_id,
privacy_mode=privacy_mode,
groups=groups,
properties=properties,
)
add_trace_processor(processor)
return processor
Loading
Loading