1- import time
21import json
3- from unittest .mock import patch , MagicMock
2+ import time
3+ from unittest .mock import MagicMock , patch
44
55import pytest
66from openai .types .chat import ChatCompletion , ChatCompletionMessage , ToolCall , ToolCallFunction
77from openai .types .chat .chat_completion import Choice
8- from openai .types .completion_usage import CompletionUsage
9- from openai .types .create_embedding_response import CreateEmbeddingResponse , Usage
10- from openai .types .embedding import Embedding
118from openai .types .chat .chat_completion_chunk import ChatCompletionChunk , ChoiceDelta
129from openai .types .chat .chat_completion_chunk import ToolCall as ChunkToolCall
1310from openai .types .chat .chat_completion_chunk import ToolCallFunction as ChunkToolCallFunction
11+ from openai .types .completion_usage import CompletionUsage
12+ from openai .types .create_embedding_response import CreateEmbeddingResponse , Usage
13+ from openai .types .embedding import Embedding
1414
1515from posthog .ai .openai import OpenAI
1616
@@ -255,7 +255,9 @@ def test_error(mock_client, mock_openai_response):
255255
256256
257257def test_cached_tokens (mock_client , mock_openai_response_with_cached_tokens ):
258- with patch ("openai.resources.chat.completions.Completions.create" , return_value = mock_openai_response_with_cached_tokens ):
258+ with patch (
259+ "openai.resources.chat.completions.Completions.create" , return_value = mock_openai_response_with_cached_tokens
260+ ):
259261 client = OpenAI (api_key = "test-key" , posthog_client = mock_client )
260262 response = client .chat .completions .create (
261263 model = "gpt-4" ,
@@ -285,12 +287,19 @@ def test_cached_tokens(mock_client, mock_openai_response_with_cached_tokens):
285287
286288
287289def test_tool_calls (mock_client , mock_openai_response_with_tool_calls ):
288- with patch ("openai.resources.chat.completions.Completions.create" , return_value = mock_openai_response_with_tool_calls ):
290+ with patch (
291+ "openai.resources.chat.completions.Completions.create" , return_value = mock_openai_response_with_tool_calls
292+ ):
289293 client = OpenAI (api_key = "test-key" , posthog_client = mock_client )
290294 response = client .chat .completions .create (
291295 model = "gpt-4" ,
292296 messages = [{"role" : "user" , "content" : "What's the weather in San Francisco?" }],
293- tools = [{"type" : "function" , "function" : {"name" : "get_weather" , "description" : "Get weather" , "parameters" : {}}}],
297+ tools = [
298+ {
299+ "type" : "function" ,
300+ "function" : {"name" : "get_weather" , "description" : "Get weather" , "parameters" : {}},
301+ }
302+ ],
294303 posthog_distinct_id = "test-id" ,
295304 )
296305
@@ -306,23 +315,23 @@ def test_tool_calls(mock_client, mock_openai_response_with_tool_calls):
306315 assert props ["$ai_model" ] == "gpt-4"
307316 assert props ["$ai_input" ] == [{"role" : "user" , "content" : "What's the weather in San Francisco?" }]
308317 assert props ["$ai_output_choices" ] == [{"role" : "assistant" , "content" : "I'll check the weather for you." }]
309-
318+
310319 # Check that tool calls are properly captured
311320 assert "$ai_tools" in props
312321 tool_calls = props ["$ai_tools" ]
313322 assert len (tool_calls ) == 1
314-
323+
315324 # Verify the tool call details
316325 tool_call = tool_calls [0 ]
317326 assert tool_call .id == "call_abc123"
318327 assert tool_call .type == "function"
319328 assert tool_call .function .name == "get_weather"
320-
329+
321330 # Verify the arguments
322331 arguments = tool_call .function .arguments
323332 parsed_args = json .loads (arguments )
324333 assert parsed_args == {"location" : "San Francisco" , "unit" : "celsius" }
325-
334+
326335 # Check token usage
327336 assert props ["$ai_input_tokens" ] == 20
328337 assert props ["$ai_output_tokens" ] == 15
@@ -434,53 +443,58 @@ def test_streaming_with_tool_calls(mock_client):
434443 mock_create .return_value = tool_call_chunks
435444
436445 client = OpenAI (api_key = "test-key" , posthog_client = mock_client )
437-
446+
438447 # Call the streaming method
439448 response_generator = client .chat .completions .create (
440449 model = "gpt-4" ,
441450 messages = [{"role" : "user" , "content" : "What's the weather in San Francisco?" }],
442- tools = [{"type" : "function" , "function" : {"name" : "get_weather" , "description" : "Get weather" , "parameters" : {}}}],
451+ tools = [
452+ {
453+ "type" : "function" ,
454+ "function" : {"name" : "get_weather" , "description" : "Get weather" , "parameters" : {}},
455+ }
456+ ],
443457 stream = True ,
444458 posthog_distinct_id = "test-id" ,
445459 )
446-
460+
447461 # Consume the generator to trigger the event capture
448462 chunks = list (response_generator )
449-
463+
450464 # Verify the chunks were returned correctly
451465 assert len (chunks ) == 4
452466 assert chunks == tool_call_chunks
453-
467+
454468 # Verify the capture was called with the right arguments
455469 assert mock_client .capture .call_count == 1
456-
470+
457471 call_args = mock_client .capture .call_args [1 ]
458472 props = call_args ["properties" ]
459-
473+
460474 assert call_args ["distinct_id" ] == "test-id"
461475 assert call_args ["event" ] == "$ai_generation"
462476 assert props ["$ai_provider" ] == "openai"
463477 assert props ["$ai_model" ] == "gpt-4"
464-
478+
465479 # Check that the tool calls were properly accumulated
466480 assert "$ai_tools" in props
467481 tool_calls = props ["$ai_tools" ]
468482 assert len (tool_calls ) == 1
469-
483+
470484 # Verify the complete tool call was properly assembled
471485 tool_call = tool_calls [0 ]
472486 assert tool_call .id == "call_abc123"
473487 assert tool_call .type == "function"
474488 assert tool_call .function .name == "get_weather"
475-
489+
476490 # Verify the arguments were concatenated correctly
477491 arguments = tool_call .function .arguments
478492 parsed_args = json .loads (arguments )
479493 assert parsed_args == {"location" : "San Francisco" , "unit" : "celsius" }
480-
494+
481495 # Check that the content was also accumulated
482496 assert props ["$ai_output_choices" ][0 ]["content" ] == "The weather in San Francisco is 15°C."
483-
497+
484498 # Check token usage
485499 assert props ["$ai_input_tokens" ] == 20
486- assert props ["$ai_output_tokens" ] == 15
500+ assert props ["$ai_output_tokens" ] == 15
0 commit comments