How to beginner · 3 min read

How to use Gemini API with FastAPI

Quick answer
Use the OpenAI SDK v1 to call the Gemini API within a FastAPI app by importing OpenAI, initializing the client with your API key from os.environ, and creating chat completions with client.chat.completions.create(model="gemini-1.5-pro", messages=[...]). This enables building AI-powered endpoints efficiently.

PREREQUISITES

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

Setup

Install the required packages and set your OpenAI API key as an environment variable.

  • Install FastAPI, Uvicorn, and OpenAI SDK:
bash
pip install fastapi uvicorn openai>=1.0

Step by step

Create a FastAPI app that uses the OpenAI SDK v1 to call the Gemini API for chat completions. The example below defines a POST endpoint /chat that accepts user messages and returns Gemini's response.

python
import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import OpenAI

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

class ChatRequest(BaseModel):
    messages: list[dict]

@app.post("/chat")
async def chat_completion(request: ChatRequest):
    try:
        response = client.chat.completions.create(
            model="gemini-1.5-pro",
            messages=request.messages
        )
        return {"response": response.choices[0].message.content}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# To run: uvicorn filename:app --reload

Common variations

You can use async calls with FastAPI naturally. To switch models, change model="gemini-1.5-pro" to another Gemini or OpenAI model like gemini-2.0-flash. For streaming responses, integrate FastAPI's StreamingResponse with the SDK's streaming interface.

python
from fastapi.responses import StreamingResponse

@app.post("/chat_stream")
async def chat_stream(request: ChatRequest):
    def event_stream():
        response = client.chat.completions.create(
            model="gemini-1.5-pro",
            messages=request.messages,
            stream=True
        )
        for chunk in response:
            yield chunk.choices[0].delta.get('content', '')

    return StreamingResponse(event_stream(), media_type="text/event-stream")

Troubleshooting

  • If you get KeyError for the API key, ensure OPENAI_API_KEY is set in your environment.
  • For Model not found errors, verify the model name is correct and available in your account.
  • Timeouts or network errors may require retry logic or checking your internet connection.

Key Takeaways

  • Use the OpenAI SDK v1 with client.chat.completions.create to call Gemini models in FastAPI.
  • Always load your API key securely from os.environ to avoid exposing secrets.
  • Switch Gemini models by changing the model parameter for different capabilities.
  • Implement streaming with FastAPI's StreamingResponse and the SDK's stream=True option.
  • Handle common errors by validating environment variables and model names before requests.
Verified 2026-04 · gemini-1.5-pro, gemini-2.0-flash
Verify ↗