@@ -163,8 +163,30 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
163163 ? ( this . options . modelTemperature ?? info . defaultTemperature ?? 1 )
164164 : info . defaultTemperature
165165
166+ // Check if the model is a Gemma 3 model
167+ const isGemma3 = model . includes ( "gemma-3" )
168+
169+ // Prepend system instruction to the first user message if it's a Gemma 3 model,
170+ // as they don't support the system instruction parameter.
171+ if ( isGemma3 && systemInstruction ) {
172+ if ( contents . length > 0 && contents [ 0 ] . role === "user" ) {
173+ const firstMessage = contents [ 0 ]
174+ // Create a new text part for the system instruction
175+ const systemPart = { text : systemInstruction }
176+ // Prepend it to the existing parts
177+ firstMessage . parts = [ systemPart , ...firstMessage . parts ]
178+ } else {
179+ // If no messages or first message is not user (e.g. starts with model),
180+ // prepend a new user message with the system instruction.
181+ contents . unshift ( {
182+ role : "user" ,
183+ parts : [ { text : systemInstruction } ] ,
184+ } )
185+ }
186+ }
187+
166188 const config : GenerateContentConfig = {
167- systemInstruction,
189+ ... ( isGemma3 ? { } : { systemInstruction } ) ,
168190 httpOptions : this . options . googleGeminiBaseUrl ? { baseUrl : this . options . googleGeminiBaseUrl } : undefined ,
169191 thinkingConfig,
170192 maxOutputTokens,
@@ -337,6 +359,19 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
337359 TelemetryService . instance . captureException ( apiError )
338360
339361 if ( error instanceof Error ) {
362+ // Parse "limit: 15000, model: gemma-3-27b\nPlease retry in 31.714908887s"
363+ // Use a more flexible regex to handle potential variations in spacing or text
364+ const match = error . message . match ( / l i m i t : \s * ( \d + ) \s * , \s * m o d e l : \s * ( [ ^ , \n ] + ) .* ?r e t r y i n \s * ( [ \d . ] + ) / s)
365+ if ( match ) {
366+ const [ , limit , model , retry ] = match
367+ throw new Error (
368+ t ( "common:errors.gemini.resource_exhausted" , {
369+ limit,
370+ model : model . trim ( ) ,
371+ retry : Math . round ( Number ( retry ) ) ,
372+ } ) ,
373+ )
374+ }
340375 throw new Error ( t ( "common:errors.gemini.generate_stream" , { error : error . message } ) )
341376 }
342377
0 commit comments