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