@@ -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 ):
0 commit comments