How to beginner · 3 min read

How to use Gemini thinking model

Quick answer
Use the gemini-2.5-pro model via the OpenAI-compatible API to perform advanced reasoning tasks by sending your prompt in a chat completion request. The model supports multi-turn conversations and complex thought processes, making it ideal for tasks requiring deep reasoning and planning.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your OpenAI API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Export your API key in your shell: export OPENAI_API_KEY='your_api_key_here'.
bash
pip install openai

Step by step

Use the gemini-2.5-pro model with the OpenAI Python SDK to send a prompt that requires reasoning. The example below shows a simple reasoning task with a multi-turn chat.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

messages = [
    {"role": "user", "content": "You are a reasoning assistant. Explain step-by-step how to solve: If a train travels 60 miles in 1.5 hours, what is its average speed?"}
]

response = client.chat.completions.create(
    model="gemini-2.5-pro",
    messages=messages
)

print(response.choices[0].message.content)
output
The average speed of the train is 40 miles per hour. Here's how to calculate it step-by-step:
1. Distance traveled = 60 miles
2. Time taken = 1.5 hours
3. Average speed = Distance / Time = 60 / 1.5 = 40 mph

Common variations

You can use gemini-2.5-pro for more complex reasoning by extending the conversation or chaining prompts. The model also supports streaming responses and can be used asynchronously with the OpenAI SDK.

python
import asyncio
import os
from openai import OpenAI

async def async_reasoning():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [
        {"role": "user", "content": "Explain the reasoning behind the Pythagorean theorem."}
    ]
    response = await client.chat.completions.acreate(
        model="gemini-2.5-pro",
        messages=messages
    )
    print(response.choices[0].message.content)

asyncio.run(async_reasoning())
output
The Pythagorean theorem states that in a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. This can be reasoned by constructing squares on each side and comparing their areas...

Troubleshooting

If you receive an authentication error, verify your OPENAI_API_KEY environment variable is set correctly. For rate limit errors, reduce request frequency or upgrade your quota. If the model returns incomplete answers, try increasing max_tokens or using multi-turn prompts for clarity.

Key Takeaways

  • Use gemini-2.5-pro for advanced reasoning tasks via chat completions.
  • Set up the OpenAI SDK with your API key from environment variables.
  • Leverage multi-turn conversations to improve reasoning depth.
  • Use async calls for scalable or streaming applications.
  • Adjust max_tokens and prompt clarity to optimize responses.
Verified 2026-04 · gemini-2.5-pro
Verify ↗