@@ -195,7 +195,29 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
195195 // Create a new text part for the system instruction
196196 const systemPart = { text : systemInstruction }
197197 // Prepend it to the existing parts
198- firstMessage . parts = [ systemPart , ...firstMessage . parts ]
198+ firstMessage . parts = [ systemPart , ...( firstMessage . parts || [ ] ) ]
199+ } else {
200+ // If no messages or first message is not user (e.g. starts with model),
201+ // prepend a new user message with the system instruction.
202+ contents . unshift ( {
203+ role : "user" ,
204+ parts : [ { text : systemInstruction } ] ,
205+ } )
206+ }
207+ }
208+
209+ // Check if the model is a Gemma 3 model
210+ const isGemma3 = model . includes ( "gemma-3" )
211+
212+ // Prepend system instruction to the first user message if it's a Gemma 3 model,
213+ // as they don't support the system instruction parameter.
214+ if ( isGemma3 && systemInstruction ) {
215+ if ( contents . length > 0 && contents [ 0 ] . role === "user" ) {
216+ const firstMessage = contents [ 0 ]
217+ // Create a new text part for the system instruction
218+ const systemPart = { text : systemInstruction }
219+ // Prepend it to the existing parts
220+ firstMessage . parts = [ systemPart , ...( firstMessage . parts || [ ] ) ]
199221 } else {
200222 // If no messages or first message is not user (e.g. starts with model),
201223 // prepend a new user message with the system instruction.
0 commit comments