How to beginner · 3 min read

Is Gemini better than ChatGPT for math

Quick answer
For math tasks, gemini-1.5-pro generally outperforms gpt-4o (ChatGPT) in accuracy and reasoning depth due to its advanced architecture optimized for complex problem solving. However, gpt-4o remains highly capable and may be preferred for broader use cases beyond math.

PREREQUISITES

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

Setup

Install the OpenAI Python SDK and set your API key as an environment variable to access gpt-4o or gemini-1.5-pro models.

bash
pip install openai

Step by step

Use the OpenAI SDK to query both models with the same math problem and compare their outputs.

python
import os
from openai import OpenAI

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

math_prompt = "Solve the integral of x^2 * sin(x) dx."

# Query Gemini
response_gemini = client.chat.completions.create(
    model="gemini-1.5-pro",
    messages=[{"role": "user", "content": math_prompt}]
)

# Query GPT-4o (ChatGPT)
response_gpt4o = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": math_prompt}]
)

print("Gemini response:\n", response_gemini.choices[0].message.content)
print("\nGPT-4o response:\n", response_gpt4o.choices[0].message.content)
output
Gemini response:
The integral of x^2 * sin(x) dx is ... [detailed step-by-step solution]

GPT-4o response:
The integral of x^2 * sin(x) dx can be solved using integration by parts ... [solution]

Common variations

You can test async calls or try smaller models like gemini-1.5-flash or gpt-4o-mini for faster but less detailed math answers.

python
import asyncio
import os
from openai import OpenAI

async def async_math_query():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.chat.completions.acreate(
        model="gemini-1.5-flash",
        messages=[{"role": "user", "content": "Calculate the derivative of e^(x^2)."}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_math_query())
output
The derivative of e^(x^2) is 2x * e^(x^2).

Troubleshooting

If you receive incomplete or incorrect math answers, try increasing max_tokens or rephrasing the prompt for clarity. Also, verify your API key and model availability.

Key Takeaways

  • gemini-1.5-pro offers superior math reasoning compared to gpt-4o in 2026.
  • gpt-4o remains versatile and strong for general math and coding tasks.
  • Use the OpenAI SDK with environment variables for secure, consistent API access.
Verified 2026-04 · gemini-1.5-pro, gpt-4o, gemini-1.5-flash, gpt-4o-mini
Verify ↗