Skip to content

Commit 53ed803

Browse files
authored
fix failing ai test (#216)
1 parent 18729e3 commit 53ed803

File tree

5 files changed

+38
-37
lines changed

5 files changed

+38
-37
lines changed

posthog/ai/anthropic/anthropic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _create_streaming(
116116

117117
def generator():
118118
nonlocal usage_stats
119-
nonlocal accumulated_content
119+
nonlocal accumulated_content # noqa: F824
120120
try:
121121
for event in response:
122122
if hasattr(event, "usage") and event.usage:

posthog/ai/anthropic/anthropic_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async def _create_streaming(
116116

117117
async def generator():
118118
nonlocal usage_stats
119-
nonlocal accumulated_content
119+
nonlocal accumulated_content # noqa: F824
120120
try:
121121
async for event in response:
122122
if hasattr(event, "usage") and event.usage:

posthog/ai/openai/openai.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _create_streaming(
8989

9090
def generator():
9191
nonlocal usage_stats
92-
nonlocal final_content
92+
nonlocal final_content # noqa: F824
9393

9494
try:
9595
for chunk in response:
@@ -261,8 +261,8 @@ def _create_streaming(
261261

262262
def generator():
263263
nonlocal usage_stats
264-
nonlocal accumulated_content
265-
nonlocal accumulated_tools
264+
nonlocal accumulated_content # noqa: F824
265+
nonlocal accumulated_tools # noqa: F824
266266

267267
try:
268268
for chunk in response:

posthog/ai/openai/openai_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def _create_streaming(
8888

8989
async def async_generator():
9090
nonlocal usage_stats
91-
nonlocal final_content
91+
nonlocal final_content # noqa: F824
9292

9393
try:
9494
async for chunk in response:
@@ -261,7 +261,7 @@ async def _create_streaming(
261261
response = await super().create(**kwargs)
262262

263263
async def async_generator():
264-
nonlocal usage_stats, accumulated_content, accumulated_tools
264+
nonlocal usage_stats, accumulated_content, accumulated_tools # noqa: F824
265265
try:
266266
async for chunk in response:
267267
if hasattr(chunk, "usage") and chunk.usage:

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,11 @@ def test_graph_state(mock_client):
618618
assert isinstance(result["messages"][2], AIMessage)
619619
assert result["messages"][2].content == "It's a type of greeble."
620620

621-
assert mock_client.capture.call_count == 11
621+
assert mock_client.capture.call_count == 12
622622
calls = [call[1] for call in mock_client.capture.call_args_list]
623623

624-
trace_args = calls[10]
625-
trace_props = calls[10]["properties"]
624+
trace_args = calls[11]
625+
trace_props = calls[11]["properties"]
626626

627627
# Events are captured in the reverse order.
628628
# Check all trace_ids
@@ -656,48 +656,49 @@ def test_graph_state(mock_client):
656656
"messages": [HumanMessage(content="What's a bar?"), AIMessage(content="Let's explore bar.")],
657657
"xyz": "abc",
658658
}
659-
assert calls[3]["event"] == "$ai_span"
660-
assert calls[3]["properties"]["$ai_parent_id"] == calls[4]["properties"]["$ai_span_id"]
661-
assert "$ai_span_id" in calls[3]["properties"]
662-
assert calls[3]["properties"]["$ai_input_state"] == second_state
663-
assert calls[3]["properties"]["$ai_output_state"] == second_state
664659

665-
# Fifth span, run the fake_plain node
666660
assert calls[4]["event"] == "$ai_span"
667-
assert "$ai_span_id" in calls[4]["properties"]
668-
assert calls[4]["properties"]["$ai_span_name"] == "fake_plain"
669-
assert calls[4]["properties"]["$ai_parent_id"] == trace_props["$ai_trace_id"]
670-
assert calls[4]["properties"]["$ai_input_state"] == initial_state
661+
assert calls[4]["properties"]["$ai_parent_id"] == calls[5]["properties"]["$ai_span_id"]
662+
assert "$ai_span_id" in calls[3]["properties"]
663+
assert calls[4]["properties"]["$ai_input_state"] == second_state
671664
assert calls[4]["properties"]["$ai_output_state"] == second_state
672665

673-
# Sixth span, chat prompt template
666+
# Fifth span, run the fake_plain node
674667
assert calls[5]["event"] == "$ai_span"
675-
assert calls[5]["properties"]["$ai_parent_id"] == calls[7]["properties"]["$ai_span_id"]
676-
assert "$ai_span_id" in calls[5]["properties"]
677-
assert calls[5]["properties"]["$ai_span_name"] == "ChatPromptTemplate"
668+
assert "$ai_span_id" in calls[4]["properties"]
669+
assert calls[5]["properties"]["$ai_span_name"] == "fake_plain"
670+
assert calls[5]["properties"]["$ai_parent_id"] == trace_props["$ai_trace_id"]
671+
assert calls[5]["properties"]["$ai_input_state"] == initial_state
672+
assert calls[5]["properties"]["$ai_output_state"] == second_state
678673

679-
# 7. Generation, fake_llm
680-
assert calls[6]["event"] == "$ai_generation"
681-
assert calls[6]["properties"]["$ai_parent_id"] == calls[7]["properties"]["$ai_span_id"]
674+
# Sixth span, chat prompt template
675+
assert calls[6]["event"] == "$ai_span"
676+
assert calls[6]["properties"]["$ai_parent_id"] == calls[8]["properties"]["$ai_span_id"]
682677
assert "$ai_span_id" in calls[6]["properties"]
683-
assert calls[6]["properties"]["$ai_span_name"] == "FakeMessagesListChatModel"
678+
assert calls[6]["properties"]["$ai_span_name"] == "ChatPromptTemplate"
684679

685-
# 8. Span, RunnableSequence
686-
assert calls[7]["event"] == "$ai_span"
687-
assert calls[7]["properties"]["$ai_parent_id"] == calls[9]["properties"]["$ai_span_id"]
680+
# 7. Generation, fake_llm
681+
assert calls[7]["event"] == "$ai_generation"
682+
assert calls[7]["properties"]["$ai_parent_id"] == calls[8]["properties"]["$ai_span_id"]
688683
assert "$ai_span_id" in calls[7]["properties"]
689-
assert calls[7]["properties"]["$ai_span_name"] == "RunnableSequence"
684+
assert calls[7]["properties"]["$ai_span_name"] == "FakeMessagesListChatModel"
690685

691-
# 9. Span, fake_llm write
686+
# 8. Span, RunnableSequence
692687
assert calls[8]["event"] == "$ai_span"
693-
assert calls[8]["properties"]["$ai_parent_id"] == calls[9]["properties"]["$ai_span_id"]
688+
assert calls[8]["properties"]["$ai_parent_id"] == calls[10]["properties"]["$ai_span_id"]
694689
assert "$ai_span_id" in calls[8]["properties"]
690+
assert calls[8]["properties"]["$ai_span_name"] == "RunnableSequence"
695691

696-
# 10. Span, fake_llm node
692+
# 9. Span, fake_llm write
697693
assert calls[9]["event"] == "$ai_span"
698-
assert calls[9]["properties"]["$ai_parent_id"] == trace_props["$ai_trace_id"]
694+
assert calls[9]["properties"]["$ai_parent_id"] == calls[10]["properties"]["$ai_span_id"]
699695
assert "$ai_span_id" in calls[9]["properties"]
700-
assert calls[9]["properties"]["$ai_span_name"] == "fake_llm"
696+
697+
# 10. Span, fake_llm node
698+
assert calls[10]["event"] == "$ai_span"
699+
assert calls[10]["properties"]["$ai_parent_id"] == trace_props["$ai_trace_id"]
700+
assert "$ai_span_id" in calls[10]["properties"]
701+
assert calls[10]["properties"]["$ai_span_name"] == "fake_llm"
701702

702703
# 11. Trace
703704
assert trace_args["event"] == "$ai_trace"

0 commit comments

Comments
 (0)