Skip to content

Commit f0cfbab

Browse files
committed
fix: black
1 parent b9e9e9c commit f0cfbab

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

posthog/ai/openai/openai.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def __init__(self, posthog_client: PostHogClient, **kwargs):
3737

3838
class WrappedResponses(openai.resources.responses.Responses):
3939
_client: OpenAI
40-
40+
4141
def create(
42-
self,
43-
posthog_distinct_id: Optional[str] = None,
44-
posthog_trace_id: Optional[str] = None,
45-
posthog_properties: Optional[Dict[str, Any]] = None,
46-
posthog_privacy_mode: bool = False,
47-
posthog_groups: Optional[Dict[str, Any]] = None,
48-
**kwargs: Any,
42+
self,
43+
posthog_distinct_id: Optional[str] = None,
44+
posthog_trace_id: Optional[str] = None,
45+
posthog_properties: Optional[Dict[str, Any]] = None,
46+
posthog_privacy_mode: bool = False,
47+
posthog_groups: Optional[Dict[str, Any]] = None,
48+
**kwargs: Any,
4949
):
5050
if posthog_trace_id is None:
5151
posthog_trace_id = uuid.uuid4()
@@ -72,7 +72,7 @@ def create(
7272
super().create,
7373
**kwargs,
7474
)
75-
75+
7676
def _create_streaming(
7777
self,
7878
posthog_distinct_id: Optional[str],

posthog/ai/openai/openai_async.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ def __init__(self, posthog_client: PostHogClient, **kwargs):
3636

3737
class WrappedResponses(openai.resources.responses.Responses):
3838
_client: AsyncOpenAI
39-
39+
4040
async def create(
41-
self,
42-
posthog_distinct_id: Optional[str] = None,
43-
posthog_trace_id: Optional[str] = None,
44-
posthog_properties: Optional[Dict[str, Any]] = None,
45-
posthog_privacy_mode: bool = False,
46-
posthog_groups: Optional[Dict[str, Any]] = None,
47-
**kwargs: Any,
41+
self,
42+
posthog_distinct_id: Optional[str] = None,
43+
posthog_trace_id: Optional[str] = None,
44+
posthog_properties: Optional[Dict[str, Any]] = None,
45+
posthog_privacy_mode: bool = False,
46+
posthog_groups: Optional[Dict[str, Any]] = None,
47+
**kwargs: Any,
4848
):
4949
if posthog_trace_id is None:
5050
posthog_trace_id = uuid.uuid4()
@@ -71,7 +71,7 @@ async def create(
7171
super().create,
7272
**kwargs,
7373
)
74-
74+
7575
async def _create_streaming(
7676
self,
7777
posthog_distinct_id: Optional[str],
@@ -194,8 +194,6 @@ async def _capture_streaming_event(
194194
)
195195

196196

197-
198-
199197
class WrappedChat(openai.resources.chat.AsyncChat):
200198
_client: AsyncOpenAI
201199

posthog/ai/utils.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get_usage(response, provider: str) -> Dict[str, Any]:
5252
response.usage.input_tokens_details, "cached_tokens"
5353
):
5454
cached_tokens = response.usage.input_tokens_details.cached_tokens
55-
if hasattr(response.usage, "output_tokens_details" ) and hasattr(
55+
if hasattr(response.usage, "output_tokens_details") and hasattr(
5656
response.usage.output_tokens_details, "reasoning_tokens"
5757
):
5858
reasoning_tokens = response.usage.output_tokens_details.reasoning_tokens
@@ -142,14 +142,15 @@ def format_tool_calls(response, provider: str):
142142
# Handle both Chat Completions and Responses API
143143
if hasattr(response, "choices") and response.choices:
144144
# Check for tool_calls in message (Chat Completions format)
145-
if (hasattr(response.choices[0], "message") and
146-
hasattr(response.choices[0].message, "tool_calls") and
147-
response.choices[0].message.tool_calls):
145+
if (
146+
hasattr(response.choices[0], "message")
147+
and hasattr(response.choices[0].message, "tool_calls")
148+
and response.choices[0].message.tool_calls
149+
):
148150
return response.choices[0].message.tool_calls
149-
151+
150152
# Check for tool_calls directly in response (Responses API format)
151-
if (hasattr(response.choices[0], "tool_calls") and
152-
response.choices[0].tool_calls):
153+
if hasattr(response.choices[0], "tool_calls") and response.choices[0].tool_calls:
153154
return response.choices[0].tool_calls
154155
return None
155156

@@ -160,10 +161,10 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
160161
if kwargs.get("system") is None:
161162
return messages
162163
return [{"role": "system", "content": kwargs.get("system")}] + messages
163-
164+
164165
# For OpenAI, handle both Chat Completions and Responses API
165166
messages = []
166-
167+
167168
if kwargs.get("messages"):
168169
messages = kwargs.get("messages")
169170

@@ -172,26 +173,26 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
172173
messages.extend(kwargs.get("input"))
173174
else:
174175
messages.append({"role": "user", "content": kwargs.get("input")})
175-
176+
176177
# Check if system prompt is provided as a separate parameter
177178
if kwargs.get("system") is not None:
178179
has_system = any(msg.get("role") == "system" for msg in messages)
179180
if not has_system:
180181
messages = [{"role": "system", "content": kwargs.get("system")}] + messages
181-
182+
182183
# For Responses API, add instructions to the system prompt if provided
183184
if kwargs.get("instructions") is not None:
184185
# Find the system message if it exists
185186
system_idx = next((i for i, msg in enumerate(messages) if msg.get("role") == "system"), None)
186-
187+
187188
if system_idx is not None:
188189
# Append instructions to existing system message
189190
system_content = messages[system_idx].get("content", "")
190191
messages[system_idx]["content"] = f"{system_content}\n\n{kwargs.get('instructions')}"
191192
else:
192193
# Create a new system message with instructions
193194
messages = [{"role": "system", "content": kwargs.get("instructions")}] + messages
194-
195+
195196
return messages
196197

197198

@@ -275,7 +276,9 @@ def call_llm_and_track_usage(
275276

276277
# Process instructions for Responses API
277278
if provider == "openai" and kwargs.get("instructions") is not None:
278-
event_properties["$ai_instructions"] = with_privacy_mode(ph_client, posthog_privacy_mode, kwargs.get("instructions"))
279+
event_properties["$ai_instructions"] = with_privacy_mode(
280+
ph_client, posthog_privacy_mode, kwargs.get("instructions")
281+
)
279282

280283
# send the event to posthog
281284
if hasattr(ph_client, "capture") and callable(ph_client.capture):
@@ -365,7 +368,9 @@ async def call_llm_and_track_usage_async(
365368

366369
# Process instructions for Responses API
367370
if provider == "openai" and kwargs.get("instructions") is not None:
368-
event_properties["$ai_instructions"] = with_privacy_mode(ph_client, posthog_privacy_mode, kwargs.get("instructions"))
371+
event_properties["$ai_instructions"] = with_privacy_mode(
372+
ph_client, posthog_privacy_mode, kwargs.get("instructions")
373+
)
369374

370375
# send the event to posthog
371376
if hasattr(ph_client, "capture") and callable(ph_client.capture):

posthog/test/ai/openai/test_openai.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from openai.types.completion_usage import CompletionUsage
1313
from openai.types.create_embedding_response import CreateEmbeddingResponse, Usage
1414
from openai.types.embedding import Embedding
15-
from openai.types.responses import Response, ResponseOutputItem, ResponseUsage, ResponseOutputMessage
15+
from openai.types.responses import Response, ResponseOutputItem, ResponseOutputMessage, ResponseUsage
1616

1717
from posthog.ai.openai import OpenAI
1818

@@ -77,6 +77,7 @@ def mock_openai_response_with_responses_api():
7777
),
7878
)
7979

80+
8081
@pytest.fixture
8182
def mock_embedding_response():
8283
return CreateEmbeddingResponse(
@@ -548,4 +549,4 @@ def test_responses_api(mock_client):
548549
assert props["$ai_input_tokens"] == 10
549550
assert props["$ai_output_tokens"] == 10
550551
assert props["$ai_http_status"] == 200
551-
assert isinstance(props["$ai_latency"], float)
552+
assert isinstance(props["$ai_latency"], float)

0 commit comments

Comments
 (0)