-
Notifications
You must be signed in to change notification settings - Fork 52
feat(ai): add OpenAI Agents SDK integration #408
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
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 3006664
feat(openai-agents): add $ai_group_id support for linking conversatio…
andrewm4894 945134a
feat(openai-agents): add enhanced span properties
andrewm4894 2445b40
Add $ai_framework property and standardize $ai_provider for OpenAI Ag…
andrewm4894 6193698
chore: bump version to 7.7.0 for OpenAI Agents SDK integration
andrewm4894 df0fcc0
fix: add openai_agents package to setuptools config
andrewm4894 6bf341a
fix: correct indentation in on_trace_start properties dict
andrewm4894 71069ed
fix: prevent unbounded growth of span/trace tracking dicts
andrewm4894 2f49c73
fix: resolve distinct_id from trace metadata in on_span_end
andrewm4894 8d7a68d
refactor: extract _base_properties helper to reduce duplication
andrewm4894 27bd98c
test: add missing edge case tests for openai agents processor
andrewm4894 143ff91
fix: handle non-dict error_info in span error parsing
andrewm4894 ae519ef
style: apply ruff formatting
andrewm4894 789be8d
style: replace lambda assignments with def (ruff E731)
andrewm4894 b3c631c
fix: restore full CHANGELOG.md history
andrewm4894 fea1420
fix: preserve personless mode for trace-id fallback distinct IDs
andrewm4894 fb4f1da
fix: restore changelog history and fix personless mode edge cases
andrewm4894 61d43e3
fix: handle None token counts in generation span
andrewm4894 b626a16
fix: check error_type_raw for all error categories
andrewm4894 b4a2d8b
fix: add type hints to instrument() function
andrewm4894 d4f4a3a
refactor: rename _safe_json to _ensure_serializable for clarity
andrewm4894 7a534de
refactor: emit $ai_trace at trace end instead of start
andrewm4894 ea6cba3
style: fix ruff formatting
andrewm4894 f239609
fix: add TYPE_CHECKING imports for type hints in instrument()
andrewm4894 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 thePostHogTracingProcessor.__init__signature.