Skip to content

Commit 9350f9b

Browse files
committed
fix: update comments from pr
1 parent 4cf2b9b commit 9350f9b

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

llm_observability_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
posthog_client=posthog,
2222
)
2323

24-
2524
def main_sync():
2625
trace_id = str(uuid.uuid4())
2726
print("Trace ID:", trace_id)
@@ -90,6 +89,7 @@ async def basic_async_openai_call(distinct_id, trace_id, properties):
9089

9190

9291
def streaming_openai_call(distinct_id, trace_id, properties):
92+
9393
response = openai_client.chat.completions.create(
9494
model="gpt-4o-mini",
9595
messages=[
@@ -132,7 +132,7 @@ async def streaming_async_openai_call(distinct_id, trace_id, properties):
132132

133133

134134
def non_instrumented_openai_call():
135-
response = openai_client.images.generate(model="dall-e-3", prompt="A cute baby sea otter", n=1, size="1024x1024")
135+
response = openai_client.images.generate(model="dall-e-3", prompt="A cute baby hedgehog", n=1, size="1024x1024")
136136
print(response)
137137
return response
138138

posthog/ai/providers/openai/openai.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import uuid
23
from typing import Any, Dict, Optional, Union
34

45
try:
@@ -66,7 +67,7 @@ def create(
6667
posthog_properties: Optional[Dict[str, Any]] = None,
6768
**kwargs: Any,
6869
):
69-
distinct_id = posthog_distinct_id or "anonymous_ai_user"
70+
distinct_id = posthog_distinct_id or uuid.uuid4()
7071

7172
if kwargs.get("stream", False):
7273
return self._create_streaming(
@@ -134,6 +135,8 @@ def _capture_streaming_event(
134135
latency: float,
135136
output: str,
136137
):
138+
if posthog_trace_id is None:
139+
posthog_trace_id = uuid.uuid4()
137140

138141
event_properties = {
139142
"$ai_provider": "openai",

posthog/ai/providers/openai/openai_async.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import uuid
23
from typing import Any, Dict, Optional, Union
34

45
try:
@@ -65,7 +66,7 @@ async def create(
6566
posthog_properties: Optional[Dict[str, Any]] = None,
6667
**kwargs: Any,
6768
):
68-
distinct_id = posthog_distinct_id or "anonymous_ai_user"
69+
distinct_id = posthog_distinct_id or uuid.uuid4()
6970

7071
# If streaming, handle streaming specifically
7172
if kwargs.get("stream", False):
@@ -130,6 +131,9 @@ def _capture_streaming_event(
130131
latency: float,
131132
output: str,
132133
):
134+
135+
if posthog_trace_id is None:
136+
posthog_trace_id = uuid.uuid4()
133137

134138
event_properties = {
135139
"$ai_provider": "openai",

posthog/ai/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import uuid
23
from typing import Any, Callable, Dict, Optional
34

45
from posthog.client import Client as PostHogClient
@@ -62,11 +63,14 @@ def call_llm_and_track_usage(
6263
response = call_method(**kwargs)
6364
except Exception as exc:
6465
error = exc
65-
http_status = getattr(exc, "status_code", 500)
66+
http_status = getattr(exc, "status_code", 0) # default to 0 becuase its liekly an SDK error
6667
finally:
6768
end_time = time.time()
6869
latency = end_time - start_time
6970

71+
if posthog_trace_id is None:
72+
posthog_trace_id = uuid.uuid4()
73+
7074
if response and hasattr(response, "usage"):
7175
usage = response.usage.model_dump()
7276

@@ -118,11 +122,14 @@ async def call_llm_and_track_usage_async(
118122
response = await call_async_method(**kwargs)
119123
except Exception as exc:
120124
error = exc
121-
http_status = getattr(exc, "status_code", 500)
125+
http_status = getattr(exc, "status_code", 0) # default to 0 because its likely an SDK error
122126
finally:
123127
end_time = time.time()
124128
latency = end_time - start_time
125129

130+
if posthog_trace_id is None:
131+
posthog_trace_id = uuid.uuid4()
132+
126133
if response and hasattr(response, "usage"):
127134
usage = response.usage.model_dump()
128135

0 commit comments

Comments
 (0)