posthog 7.6.0__py3-none-any.whl → 7.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- posthog/ai/openai_agents/__init__.py +76 -0
- posthog/ai/openai_agents/processor.py +863 -0
- posthog/test/ai/__init__.py +0 -0
- posthog/test/ai/openai_agents/__init__.py +1 -0
- posthog/test/ai/openai_agents/test_processor.py +810 -0
- posthog/test/ai/test_sanitization.py +522 -0
- posthog/test/ai/test_system_prompts.py +363 -0
- posthog/version.py +1 -1
- {posthog-7.6.0.dist-info → posthog-7.7.0.dist-info}/METADATA +1 -1
- {posthog-7.6.0.dist-info → posthog-7.7.0.dist-info}/RECORD +13 -6
- {posthog-7.6.0.dist-info → posthog-7.7.0.dist-info}/WHEEL +0 -0
- {posthog-7.6.0.dist-info → posthog-7.7.0.dist-info}/licenses/LICENSE +0 -0
- {posthog-7.6.0.dist-info → posthog-7.7.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from agents.tracing import Trace
|
|
7
|
+
|
|
8
|
+
from posthog.client import Client
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
import agents # noqa: F401
|
|
12
|
+
except ImportError:
|
|
13
|
+
raise ModuleNotFoundError(
|
|
14
|
+
"Please install the OpenAI Agents SDK to use this feature: 'pip install openai-agents'"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
from posthog.ai.openai_agents.processor import PostHogTracingProcessor
|
|
18
|
+
|
|
19
|
+
__all__ = ["PostHogTracingProcessor", "instrument"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def instrument(
|
|
23
|
+
client: Optional[Client] = None,
|
|
24
|
+
distinct_id: Optional[Union[str, Callable[[Trace], Optional[str]]]] = None,
|
|
25
|
+
privacy_mode: bool = False,
|
|
26
|
+
groups: Optional[Dict[str, Any]] = None,
|
|
27
|
+
properties: Optional[Dict[str, Any]] = None,
|
|
28
|
+
) -> PostHogTracingProcessor:
|
|
29
|
+
"""
|
|
30
|
+
One-liner to instrument OpenAI Agents SDK with PostHog tracing.
|
|
31
|
+
|
|
32
|
+
This registers a PostHogTracingProcessor with the OpenAI Agents SDK,
|
|
33
|
+
automatically capturing traces, spans, and LLM generations.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
client: Optional PostHog client instance. If not provided, uses the default client.
|
|
37
|
+
distinct_id: Optional distinct ID to associate with all traces.
|
|
38
|
+
Can also be a callable that takes a trace and returns a distinct ID.
|
|
39
|
+
privacy_mode: If True, redacts input/output content from events.
|
|
40
|
+
groups: Optional PostHog groups to associate with events.
|
|
41
|
+
properties: Optional additional properties to include with all events.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
PostHogTracingProcessor: The registered processor instance.
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
```python
|
|
48
|
+
from posthog.ai.openai_agents import instrument
|
|
49
|
+
|
|
50
|
+
# Simple setup
|
|
51
|
+
instrument(distinct_id="user@example.com")
|
|
52
|
+
|
|
53
|
+
# With custom properties
|
|
54
|
+
instrument(
|
|
55
|
+
distinct_id="user@example.com",
|
|
56
|
+
privacy_mode=True,
|
|
57
|
+
properties={"environment": "production"}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# Now run agents as normal - traces automatically sent to PostHog
|
|
61
|
+
from agents import Agent, Runner
|
|
62
|
+
agent = Agent(name="Assistant", instructions="You are helpful.")
|
|
63
|
+
result = Runner.run_sync(agent, "Hello!")
|
|
64
|
+
```
|
|
65
|
+
"""
|
|
66
|
+
from agents.tracing import add_trace_processor
|
|
67
|
+
|
|
68
|
+
processor = PostHogTracingProcessor(
|
|
69
|
+
client=client,
|
|
70
|
+
distinct_id=distinct_id,
|
|
71
|
+
privacy_mode=privacy_mode,
|
|
72
|
+
groups=groups,
|
|
73
|
+
properties=properties,
|
|
74
|
+
)
|
|
75
|
+
add_trace_processor(processor)
|
|
76
|
+
return processor
|