@@ -173,14 +173,28 @@ def merge_system_prompt(kwargs: Dict[str, Any], provider: str) -> List[Formatted
173173
174174 contents = kwargs .get ("contents" , [])
175175 formatted_messages = format_gemini_input (contents )
176-
177- if kwargs .get ("system_instruction" ) is not None :
178- system_instruction = kwargs .get ("system_instruction" )
179- system_message = cast (FormattedMessage , {
180- "role" : "system" ,
181- "content" : system_instruction
182- })
183- formatted_messages = [system_message ] + list (formatted_messages )
176+
177+ # Check if system instruction is provided in config parameter
178+ config = kwargs .get ("config" )
179+ system_instruction = None
180+
181+ if config is not None :
182+ # Handle different config formats
183+ if hasattr (config , "system_instruction" ):
184+ system_instruction = config .system_instruction
185+ elif isinstance (config , dict ) and "system_instruction" in config :
186+ system_instruction = config ["system_instruction" ]
187+ elif isinstance (config , dict ) and "systemInstruction" in config :
188+ system_instruction = config ["systemInstruction" ]
189+
190+ if system_instruction is not None :
191+ has_system = any (msg .get ("role" ) == "system" for msg in formatted_messages )
192+ if not has_system :
193+ system_message = cast (FormattedMessage , {
194+ "role" : "system" ,
195+ "content" : system_instruction
196+ })
197+ formatted_messages = [system_message ] + list (formatted_messages )
184198
185199 return formatted_messages
186200 elif provider == "openai" :
0 commit comments