@@ -154,8 +154,30 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
154154 ? ( this . options . modelTemperature ?? info . defaultTemperature ?? 1 )
155155 : info . defaultTemperature
156156
157+ // Check if the model is a Gemma 3 model
158+ const isGemma3 = model . includes ( "gemma-3" )
159+
160+ // Prepend system instruction to the first user message if it's a Gemma 3 model,
161+ // as they don't support the system instruction parameter.
162+ if ( isGemma3 && systemInstruction ) {
163+ if ( contents . length > 0 && contents [ 0 ] . role === "user" ) {
164+ const firstMessage = contents [ 0 ]
165+ // Create a new text part for the system instruction
166+ const systemPart = { text : systemInstruction }
167+ // Prepend it to the existing parts
168+ firstMessage . parts = [ systemPart , ...firstMessage . parts ]
169+ } else {
170+ // If no messages or first message is not user (e.g. starts with model),
171+ // prepend a new user message with the system instruction.
172+ contents . unshift ( {
173+ role : "user" ,
174+ parts : [ { text : systemInstruction } ] ,
175+ } )
176+ }
177+ }
178+
157179 const config : GenerateContentConfig = {
158- systemInstruction,
180+ ... ( isGemma3 ? { } : { systemInstruction } ) ,
159181 httpOptions : this . options . googleGeminiBaseUrl ? { baseUrl : this . options . googleGeminiBaseUrl } : undefined ,
160182 thinkingConfig,
161183 maxOutputTokens,
@@ -339,6 +361,19 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
339361 }
340362 } catch ( error ) {
341363 if ( error instanceof Error ) {
364+ // Parse "limit: 15000, model: gemma-3-27b\nPlease retry in 31.714908887s"
365+ // Use a more flexible regex to handle potential variations in spacing or text
366+ 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)
367+ if ( match ) {
368+ const [ , limit , model , retry ] = match
369+ throw new Error (
370+ t ( "common:errors.gemini.resource_exhausted" , {
371+ limit,
372+ model : model . trim ( ) ,
373+ retry : Math . round ( Number ( retry ) ) ,
374+ } ) ,
375+ )
376+ }
342377 throw new Error ( t ( "common:errors.gemini.generate_stream" , { error : error . message } ) )
343378 }
344379
0 commit comments