@@ -293,6 +293,43 @@ def check_grounding_ran(response: types.GenerateContentResponse) -> bool:
293293 return bool (n_searches and n_chunks and n_supports )
294294
295295
296+ def get_thinking_config (
297+ model_name : str , do_thinking : bool
298+ ) -> types .ThinkingConfig | None :
299+ """
300+ Gets the thinking cofig required for the current model.
301+ Thinking is set differently before and after Gemini 3.0.
302+ Certain models like the 2.5 and 3.0 pro models, do not allow grounding to be disabled.
303+ """
304+ if "gemini-2.5-pro" in model_name :
305+ if not do_thinking :
306+ _logger .warning (
307+ "It is not possible to turn off thinking with this model. Setting to minimum."
308+ )
309+ return types .ThinkingConfig (thinking_budget = 128 ) # minimum thinking
310+ return types .ThinkingConfig (thinking_budget = - 1 ) # dynamic budget
311+
312+ if (
313+ model_name < "gemini-2.6"
314+ ): # there is no 2.6, but this means it will catch all 2.5 variants
315+ if do_thinking :
316+ return types .ThinkingConfig (thinking_budget = - 1 ) # dynamic budget
317+ return types .ThinkingConfig (thinking_budget = 0 ) # disable thinking
318+
319+ if model_name >= "gemini-3" :
320+ if not do_thinking :
321+ if "pro" in model_name :
322+ _logger .warning (
323+ "Cannot disable thinking in this model. Setting thinking to low."
324+ )
325+ return types .ThinkingConfig (thinking_level = types .ThinkingLevel .LOW )
326+ return types .ThinkingConfig (thinking_level = types .ThinkingLevel .MINIMAL )
327+ return None
328+
329+ _logger .warning ("Did not recognise the model provided, defaulting to None" )
330+ return None
331+
332+
296333def run_prompt (
297334 prompt : str ,
298335 video_uri : str | None = None ,
@@ -302,6 +339,7 @@ def run_prompt(
302339 safety_settings : list [types .SafetySetting ] = DEFAULT_SAFETY_SETTINGS ,
303340 model_config : ModelConfig | None = None ,
304341 use_grounding : bool = False ,
342+ do_thinking : bool = False ,
305343 inline_citations : bool = False ,
306344 labels : dict [str , str ] = {},
307345) -> str :
@@ -352,6 +390,10 @@ class Movie(BaseModel):
352390 and makes the output more likely to be factual.
353391 Does not work with structured output.
354392 See the docs (`grounding`_).
393+ do_thinking: bool
394+ Whether Gemini should use a thought process.
395+ This is more expensive but may yield better results.
396+ Do not use for bulk tasks that don't require complex thoughts.
355397 inline_citations: bool
356398 Whether output should include citations inline with the text.
357399 These citations will be links to be used as evidence.
@@ -379,6 +421,7 @@ class Movie(BaseModel):
379421 safety_settings = safety_settings ,
380422 model_config = model_config ,
381423 use_grounding = use_grounding ,
424+ do_thinking = do_thinking ,
382425 inline_citations = inline_citations ,
383426 labels = labels ,
384427 )
@@ -394,6 +437,7 @@ async def run_prompt_async(
394437 safety_settings : list [types .SafetySetting ] = DEFAULT_SAFETY_SETTINGS ,
395438 model_config : ModelConfig | None = None ,
396439 use_grounding : bool = False ,
440+ do_thinking : bool = False ,
397441 inline_citations : bool = False ,
398442 labels : dict [str , str ] = {},
399443) -> str :
@@ -444,6 +488,10 @@ class Movie(BaseModel):
444488 and makes the output more likely to be factual.
445489 Does not work with structured output.
446490 See the docs (`grounding`_).
491+ do_thinking: bool
492+ Whether Gemini should use a thought process.
493+ This is more expensive but may yield better results.
494+ Do not use for bulk tasks that don't require complex thoughts.
447495 inline_citations: bool
448496 Whether output should include citations inline with the text.
449497 These citations will be links to be used as evidence.
@@ -506,6 +554,7 @@ class Movie(BaseModel):
506554 safety_settings = safety_settings ,
507555 ** built_gen_config ,
508556 labels = merged_labels ,
557+ thinking_config = get_thinking_config (model_config .model_name , do_thinking ),
509558 ),
510559 )
511560
0 commit comments