How to use Instructor with Gemini
Quick answer
Use the
instructor Python library with the OpenAI client configured for Gemini models by calling instructor.from_openai() and specifying the Gemini model like gemini-2.5-pro. Pass your Pydantic BaseModel as response_model to parse structured outputs from the chat completion.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0 instructor pydantic
Setup
Install the required packages and set your OpenAI API key in the environment.
- Install packages:
pip install openai instructor pydantic - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install openai instructor pydantic Step by step
This example shows how to use instructor with the Gemini model gemini-2.5-pro to extract structured data from a user prompt using a Pydantic model.
import os
from openai import OpenAI
import instructor
from pydantic import BaseModel
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Wrap OpenAI client with Instructor
instructor_client = instructor.from_openai(client)
# Define a Pydantic model for structured extraction
class UserInfo(BaseModel):
name: str
age: int
# Prepare messages
messages = [{"role": "user", "content": "Extract: Alice is 28 years old."}]
# Call chat completion with response_model and Gemini model
response = instructor_client.chat.completions.create(
model="gemini-2.5-pro",
response_model=UserInfo,
messages=messages
)
# Access structured data
user_info = response
print(f"Name: {user_info.name}, Age: {user_info.age}") output
Name: Alice, Age: 28
Common variations
- Async usage: Use
await instructor_client.chat.completions.acreate(...)in an async function. - Different Gemini models: Replace
gemini-2.5-prowith other Gemini models likegemini-2.0-flashas needed. - Custom system prompt: Add a
systemparameter to guide the model's behavior.
import asyncio
async def async_example():
response = await instructor_client.chat.completions.acreate(
model="gemini-2.5-pro",
response_model=UserInfo,
messages=[{"role": "user", "content": "Extract: Bob is 35 years old."}]
)
print(f"Name: {response.name}, Age: {response.age}")
asyncio.run(async_example()) output
Name: Bob, Age: 35
Troubleshooting
- If you get a
ValidationError, ensure the model output matches the Pydantic schema exactly. - If the response is empty or incorrect, verify your
messagesformat and that themodelsupports structured extraction. - Check your
OPENAI_API_KEYenvironment variable is set correctly.
Key Takeaways
- Use
instructor.from_openai()to wrap the OpenAI client for Gemini integration. - Pass a Pydantic
BaseModelasresponse_modelto parse structured outputs. - Specify the Gemini model like
gemini-2.5-proin themodelparameter. - Async calls are supported with
acreate()for non-blocking usage. - Validate your schema and environment variables to avoid common errors.