How to beginner · 3 min read

How to use Gemini API with Flask

Quick answer
Use the OpenAI SDK v1 in Python to call the gemini-1.5-pro model within a Flask app by importing OpenAI, setting your API key via os.environ, and creating chat completions inside Flask routes. This enables you to build AI-powered web endpoints easily.

PREREQUISITES

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

Setup

Install the required packages and set your OpenAI API key as an environment variable before running your Flask app.

bash
pip install openai flask

Step by step

Create a Flask app that uses the OpenAI SDK v1 to call the gemini-1.5-pro model for chat completions. The example below shows a simple endpoint that accepts a user message and returns the AI's response.

python
import os
from flask import Flask, request, jsonify
from openai import OpenAI

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

@app.route('/chat', methods=['POST'])
def chat():
    data = request.json
    user_message = data.get('message', '')
    if not user_message:
        return jsonify({'error': 'No message provided'}), 400

    response = client.chat.completions.create(
        model="gemini-1.5-pro",
        messages=[{"role": "user", "content": user_message}]
    )
    answer = response.choices[0].message.content
    return jsonify({'response': answer})

if __name__ == '__main__':
    app.run(debug=True, port=5000)
output
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

# Example POST request JSON:
# {"message": "Hello, Gemini!"}

# Response JSON:
# {"response": "Hello! How can I assist you today?"}

Common variations

You can adapt the Flask app to use different Gemini models like gemini-1.5-flash or add streaming responses. For async support, use Quart or FastAPI with the same OpenAI client pattern.

python
from openai import OpenAI
import asyncio

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

async def async_chat(message: str):
    response = await client.chat.completions.acreate(
        model="gemini-1.5-flash",
        messages=[{"role": "user", "content": message}]
    )
    return response.choices[0].message.content

Troubleshooting

  • If you get an authentication error, verify your OPENAI_API_KEY environment variable is set correctly.
  • For network timeouts, check your internet connection and retry.
  • If the model name is invalid, confirm you are using a current Gemini model like gemini-1.5-pro.

Key Takeaways

  • Use the OpenAI SDK v1 with gemini-1.5-pro for best Gemini API integration.
  • Set your API key securely via environment variables before running Flask apps.
  • Flask routes can easily wrap Gemini chat completions for AI-powered endpoints.
  • Async and streaming variants require different frameworks or SDK async methods.
  • Always verify model names and API keys to avoid common errors.
Verified 2026-04 · gemini-1.5-pro, gemini-1.5-flash
Verify ↗