Skip to content

Commit 7d9eb5b

Browse files
committed
fix: test - hopefully
1 parent f0cfbab commit 7d9eb5b

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

posthog/ai/openai/openai.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from posthog.ai.utils import call_llm_and_track_usage, get_model_params, with_privacy_mode
1212
from posthog.client import Client as PostHogClient
13+
from openai.resources.responses import Responses
1314

1415

1516
class OpenAI(openai.OpenAI):

posthog/ai/utils.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,50 @@ def format_response_openai(response):
123123
)
124124
# Handle Responses API format
125125
if hasattr(response, "output"):
126-
for message in response.output:
127-
128-
output.append(
129-
{
130-
"content": message.content,
131-
"role": message.role,
132-
}
133-
)
126+
for item in response.output:
127+
if item.type == "message":
128+
# Extract text content from the content list
129+
if hasattr(item, "content") and isinstance(item.content, list):
130+
for content_item in item.content:
131+
if (
132+
hasattr(content_item, "type")
133+
and content_item.type == "input_text"
134+
and hasattr(content_item, "text")
135+
):
136+
output.append(
137+
{
138+
"content": content_item.text,
139+
"role": item.role,
140+
}
141+
)
142+
elif hasattr(content_item, "text"):
143+
output.append(
144+
{
145+
"content": content_item.text,
146+
"role": item.role,
147+
}
148+
)
149+
elif (
150+
hasattr(content_item, "type")
151+
and content_item.type == "input_image"
152+
and hasattr(content_item, "image_url")
153+
):
154+
output.append(
155+
{
156+
"content": {
157+
"type": "image",
158+
"image": content_item.image_url,
159+
},
160+
"role": item.role,
161+
}
162+
)
163+
else:
164+
output.append(
165+
{
166+
"content": item.content,
167+
"role": item.role,
168+
}
169+
)
134170
return output
135171

136172

posthog/test/ai/openai/test_openai.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def mock_openai_response_with_responses_api():
6464
role="assistant",
6565
content=[
6666
ResponseOutputMessage(
67-
type="text",
67+
type="input_text",
6868
text="Test response",
6969
)
7070
],
@@ -532,21 +532,29 @@ def test_streaming_with_tool_calls(mock_client):
532532

533533

534534
# test responses api
535-
def test_responses_api(mock_client):
536-
with patch("openai.resources.responses.Responses.create") as mock_create:
537-
mock_create.return_value = mock_openai_response_with_responses_api
535+
def test_responses_api(mock_client, mock_openai_response_with_responses_api):
536+
with patch("openai.resources.responses.Responses.create", return_value=mock_openai_response_with_responses_api):
538537
client = OpenAI(api_key="test-key", posthog_client=mock_client)
539-
response = client.responses.create(model="gpt-4o-mini", input=[{"role": "user", "content": "Hello"}])
538+
response = client.responses.create(
539+
model="gpt-4o-mini",
540+
input=[{"role": "user", "content": "Hello"}],
541+
posthog_distinct_id="test-id",
542+
posthog_properties={"foo": "bar"},
543+
)
540544
assert response == mock_openai_response_with_responses_api
541545
assert mock_client.capture.call_count == 1
542546

543547
call_args = mock_client.capture.call_args[1]
544548
props = call_args["properties"]
549+
550+
assert call_args["distinct_id"] == "test-id"
551+
assert call_args["event"] == "$ai_generation"
545552
assert props["$ai_provider"] == "openai"
546553
assert props["$ai_model"] == "gpt-4o-mini"
547554
assert props["$ai_input"] == [{"role": "user", "content": "Hello"}]
548555
assert props["$ai_output_choices"] == [{"role": "assistant", "content": "Test response"}]
549556
assert props["$ai_input_tokens"] == 10
550557
assert props["$ai_output_tokens"] == 10
551558
assert props["$ai_http_status"] == 200
559+
assert props["foo"] == "bar"
552560
assert isinstance(props["$ai_latency"], float)

0 commit comments

Comments
 (0)