Structured outputs with LiteLLM, a complete guide w/ instructor¶
LiteLLM provides a unified interface for multiple LLM providers, making it easy to switch between different models and providers. This guide shows you how to use Instructor with LiteLLM for type-safe, validated responses across various LLM providers.
Quick Start¶
Install Instructor with LiteLLM support:
Simple User Example (Sync)¶
from litellm import completion
import instructor
from pydantic import BaseModel
# Enable instructor patches
client = instructor.from_litellm(completion)
class User(BaseModel):
name: str
age: int
# Create structured output
user = client.completion(
model="gpt-3.5-turbo", # Can use any supported model
messages=[
{"role": "user", "content": "Extract: Jason is 25 years old"},
],
response_model=User,
)
print(user) # User(name='Jason', age=25)
Simple User Example (Async)¶
from litellm import acompletion
import instructor
from pydantic import BaseModel
import asyncio
# Enable instructor patches for async
client = instructor.from_litellm(acompletion)
class User(BaseModel):
name: str
age: int
async def extract_user():
user = await client.acompletion(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Extract: Jason is 25 years old"},
],
response_model=User,
)
return user
# Run async function
user = asyncio.run(extract_user())
print(user) # User(name='Jason', age=25)
Related Resources¶
Updates and Compatibility¶
Instructor maintains compatibility with LiteLLM's latest releases. Check the changelog for updates.
Note: Always verify provider-specific features and limitations in their respective documentation before implementation.