Skip to content

Commit dd22595

Browse files
authored
Add thinking_config to GeminiModel (#1609)
1 parent 807c46d commit dd22595

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

pydantic_ai_slim/pydantic_ai/models/anthropic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"""
9191

9292

93-
class AnthropicModelSettings(ModelSettings):
93+
class AnthropicModelSettings(ModelSettings, total=False):
9494
"""Settings used for an Anthropic model request.
9595
9696
ALL FIELDS MUST BE `anthropic_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.

pydantic_ai_slim/pydantic_ai/models/cohere.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"""
7979

8080

81-
class CohereModelSettings(ModelSettings):
81+
class CohereModelSettings(ModelSettings, total=False):
8282
"""Settings used for a Cohere model request.
8383
8484
ALL FIELDS MUST BE `cohere_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.

pydantic_ai_slim/pydantic_ai/models/gemini.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,26 @@
7373
"""
7474

7575

76-
class GeminiModelSettings(ModelSettings):
76+
class GeminiModelSettings(ModelSettings, total=False):
7777
"""Settings used for a Gemini model request.
7878
7979
ALL FIELDS MUST BE `gemini_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.
8080
"""
8181

8282
gemini_safety_settings: list[GeminiSafetySettings]
8383

84+
gemini_thinking_config: ThinkingConfig
85+
"""Thinking is "on" by default in both the API and AI Studio.
86+
87+
Being on by default doesn't mean the model will send back thoughts. For that, you would need to set `include_thoughts`
88+
to `True`, but since end of January 2025, `thoughts` are not returned anymore, and are only displayed in the Google
89+
AI Studio. See https://discuss.ai.google.dev/t/thoughts-are-missing-cot-not-included-anymore/63653 for more details.
90+
91+
If you want to avoid the model spending any tokens on thinking, you can set `thinking_budget` to `0`.
92+
93+
See more about it on <https://ai.google.dev/gemini-api/docs/thinking>.
94+
"""
95+
8496

8597
@dataclass(init=False)
8698
class GeminiModel(Model):
@@ -223,7 +235,9 @@ async def _make_request(
223235
generation_config['presence_penalty'] = presence_penalty
224236
if (frequency_penalty := model_settings.get('frequency_penalty')) is not None:
225237
generation_config['frequency_penalty'] = frequency_penalty
226-
if (gemini_safety_settings := model_settings.get('gemini_safety_settings')) != []:
238+
if (thinkingConfig := model_settings.get('gemini_thinking_config')) is not None:
239+
generation_config['thinking_config'] = thinkingConfig # pragma: no cover
240+
if (gemini_safety_settings := model_settings.get('gemini_safety_settings')) is not None:
227241
request_data['safetySettings'] = gemini_safety_settings
228242
if generation_config:
229243
request_data['generationConfig'] = generation_config
@@ -497,6 +511,16 @@ class GeminiSafetySettings(TypedDict):
497511
"""
498512

499513

514+
class ThinkingConfig(TypedDict, total=False):
515+
"""The thinking features configuration."""
516+
517+
include_thoughts: Annotated[bool, pydantic.Field(alias='includeThoughts')]
518+
"""Indicates whether to include thoughts in the response. If true, thoughts are returned only if the model supports thought and thoughts are available."""
519+
520+
thinking_budget: Annotated[int, pydantic.Field(alias='thinkingBudget')]
521+
"""Indicates the thinking budget in tokens."""
522+
523+
500524
class _GeminiGenerationConfig(TypedDict, total=False):
501525
"""Schema for an API request to the Gemini API.
502526
@@ -511,6 +535,7 @@ class _GeminiGenerationConfig(TypedDict, total=False):
511535
presence_penalty: float
512536
frequency_penalty: float
513537
stop_sequences: list[str]
538+
thinking_config: ThinkingConfig
514539

515540

516541
class _GeminiContent(TypedDict):

pydantic_ai_slim/pydantic_ai/models/groq.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"""
8383

8484

85-
class GroqModelSettings(ModelSettings):
85+
class GroqModelSettings(ModelSettings, total=False):
8686
"""Settings used for a Groq model request.
8787
8888
ALL FIELDS MUST BE `groq_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.

pydantic_ai_slim/pydantic_ai/models/mistral.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"""
9292

9393

94-
class MistralModelSettings(ModelSettings):
94+
class MistralModelSettings(ModelSettings, total=False):
9595
"""Settings used for a Mistral model request.
9696
9797
ALL FIELDS MUST BE `mistral_` PREFIXED SO YOU CAN MERGE THEM WITH OTHER MODELS.

0 commit comments

Comments
 (0)