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