Skip to content

Disable thinking mode in Gemini 2.5 flash #1555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tcrapts opened this issue Apr 20, 2025 · 6 comments · Fixed by #1609 or #1779 · May be fixed by #1142
Closed

Disable thinking mode in Gemini 2.5 flash #1555

tcrapts opened this issue Apr 20, 2025 · 6 comments · Fixed by #1609 or #1779 · May be fixed by #1142
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@tcrapts
Copy link

tcrapts commented Apr 20, 2025

Description

Gemini 2.5 flash has thinking mode enabled by default. This is is expensive. It can be turned off, but Pydantic AI doesn't support this.

AISDK from Vercel does have this. We need something similar:

Image

References

No response

@Kludex
Copy link
Member

Kludex commented Apr 20, 2025

I'm working on this already. Should be ready at some point this week.

@Kludex Kludex mentioned this issue Apr 20, 2025
3 tasks
@bigs
Copy link

bigs commented Apr 22, 2025

Hey @Kludex excited to see this coming! Was wondering if, while you were working on thinking options, if #840 might also be relevant as a model-specific option? Just throwing it into your consciousness!

@amiyapatanaik
Copy link

A small example of how to achieve this will be very helpful.

@Kludex Kludex reopened this May 6, 2025
@Kludex Kludex added the documentation Improvements or additions to documentation label May 6, 2025
@tcrapts
Copy link
Author

tcrapts commented May 7, 2025

@amiyapatanaik refer to the docs; https://ai.pydantic.dev/api/models/gemini/#pydantic_ai.models.gemini.GeminiModelSettings

Something like this should work:

from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel, ThinkingConfig
from pydantic_ai.providers.google_gla import GoogleGLAProvider

api_key = "your_key"

gemini_thinking_config: ThinkingConfig = {
    "include_thoughts": False,
    "thinking_budget": 0,
}


agent = Agent(
    model=GeminiModel(
        model_name="gemini-2.5-flash-preview-04-17",
        provider=GoogleGLAProvider(
            api_key=api_key,
        ),
    ),
    model_settings={"gemini_thinking_config": gemini_thinking_config},  # type: ignore
)

result = agent.run_sync("Who are you?")
print(result.output)

I need to ignore type issues, because the default ModelSettings class somehow doesn't seem to recognize gemini_thinking_config. But it works.

@d-zimmermann
Copy link

d-zimmermann commented May 8, 2025

I expected to give do something along the line of:

# Disable thinking (budget 0) and output; only works for GEMINI
thinkingDisabledConfig = ThinkingConfig(
    include_thoughts=False,
    thinking_budget=0
)

model_settings=ModelSettings(    
    thinking_config=thinkingDisabledConfig,
    temperature=1.0, 
    top_p=0.95,
    top_k=64,
)

agent = Agent(  
            'google-gla:gemini-2.5-flash-preview-04-17',
            model_settings=model_settings
)

... but this is completely ignored and for Usage(), I can see that it's thinking along anyway.

The SOLUTION is using gemini_thinking_config in ModelSettings.

model_settings=ModelSettings(    
    gemini_thinking_config=thinkingDisabledConfig,
    temperature=1.0, 
    top_p=0.95,
    top_k=64,
)

Not sure if this is intended or a bug though. It makes it unnecessarily complicated to use a different model than Gemini with this behaviour. I had hoped that it would work with generic ModelSettings and model-specific settings, and that these would just be ignored if not supported. ('gemini' prefix)

EDIT: Thanks @tcrapts for pointing this out!

@tcrapts
Copy link
Author

tcrapts commented May 8, 2025

@d-zimmermann ModelSettings and GeminiModelSettings are both TypedDict, in runtime it doesn't matter which you use. I think both of your examples will not work, the correct key is gemini_thinking_config, with the gemini_ prefix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
5 participants