11import time
22import uuid
3- from typing import Any , Callable , Dict , Optional
3+ from typing import Any , Callable , Dict , List , Optional , cast
44
55from posthog .client import Client as PostHogClient
6- from posthog .ai .types import StreamingEventData , TokenUsage
6+ from posthog .ai .types import FormattedMessage , StreamingEventData , TokenUsage
77from posthog .ai .sanitization import (
88 sanitize_openai ,
99 sanitize_anthropic ,
@@ -158,7 +158,9 @@ def extract_available_tool_calls(provider: str, kwargs: Dict[str, Any]):
158158 return None
159159
160160
161- def merge_system_prompt (kwargs : Dict [str , Any ], provider : str ):
161+ def merge_system_prompt (
162+ kwargs : Dict [str , Any ], provider : str
163+ ) -> List [FormattedMessage ]:
162164 """
163165 Merge system prompts and format messages for the given provider.
164166 """
@@ -169,10 +171,11 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
169171 system = kwargs .get ("system" )
170172 return format_anthropic_input (messages , system )
171173 elif provider == "gemini" :
172- from posthog .ai .gemini .gemini_converter import format_gemini_input
174+ from posthog .ai .gemini .gemini_converter import format_gemini_input_with_system
173175
174176 contents = kwargs .get ("contents" , [])
175- return format_gemini_input (contents )
177+ config = kwargs .get ("config" )
178+ return format_gemini_input_with_system (contents , config )
176179 elif provider == "openai" :
177180 from posthog .ai .openai .openai_converter import format_openai_input
178181
@@ -187,9 +190,11 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
187190 if kwargs .get ("system" ) is not None :
188191 has_system = any (msg .get ("role" ) == "system" for msg in messages )
189192 if not has_system :
190- messages = [
191- {"role" : "system" , "content" : kwargs .get ("system" )}
192- ] + messages
193+ system_msg = cast (
194+ FormattedMessage ,
195+ {"role" : "system" , "content" : kwargs .get ("system" )},
196+ )
197+ messages = [system_msg ] + messages
193198
194199 # For Responses API, add instructions to the system prompt if provided
195200 if kwargs .get ("instructions" ) is not None :
@@ -207,9 +212,11 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str):
207212 )
208213 else :
209214 # Create a new system message with instructions
210- messages = [
211- {"role" : "system" , "content" : kwargs .get ("instructions" )}
212- ] + messages
215+ instruction_msg = cast (
216+ FormattedMessage ,
217+ {"role" : "system" , "content" : kwargs .get ("instructions" )},
218+ )
219+ messages = [instruction_msg ] + messages
213220
214221 return messages
215222
0 commit comments