Prerequisites
Before you begin, ensure you have:- Cerebras API Key - Get a free API key here
- Python 3.10 or higher - Instructor requires Python 3.10+ for modern typing syntax
- Basic familiarity with Pydantic - Instructor uses Pydantic models for validation
Why Use Instructor with Cerebras?
Combining Instructor with Cerebras gives you:- Type Safety - Define your expected output structure with Pydantic models
- Automatic Validation - Instructor validates responses and retries if needed
- Ultra-Fast Inference - Cerebras’s hardware acceleration for rapid structured outputs
- Developer Experience - Clean, Pythonic API with full IDE support
Configure Instructor
1
Install Instructor with Cerebras support
Install Instructor with the Cerebras Cloud SDK integration. This installs both Instructor and the Cerebras Cloud SDK with all required dependencies.
2
Set up your environment variables
Create a Then load it in your Python code:
.env file in your project directory to store your API key securely. This keeps your credentials out of your code and version control.Never commit your
.env file to version control. Add it to your .gitignore file.3
Initialize the Instructor client
4
Define your data model
Create a Pydantic model that describes the structure you want to extract. Instructor will ensure the LLM’s response matches this schema.Pydantic’s
Field allows you to add descriptions that help guide the LLM’s extraction.5
Extract structured data
Now you can make requests and get back validated Pydantic objects instead of raw text. The response is automatically validated and converted to your Pydantic model.The
response_model parameter tells Instructor what structure to extract.Working with Complex Structures
Instructor excels at extracting nested and complex data structures. Here’s an example with nested models and lists:Async Support
Instructor fully supports async/await for high-performance applications. This is ideal when you need to handle multiple requests concurrently or integrate with async frameworks.Validation and Retry Logic
Instructor automatically validates responses and can retry if validation fails. This ensures you always get data that matches your schema.Using Different Cerebras Models
Cerebras offers several models optimized for different use cases. Choose the model that best fits your needs:FAQ
Why am I getting validation errors?
Why am I getting validation errors?
If you’re seeing validation errors, check:
- Model capability - Smaller models may struggle with complex structures. Try
llama-3.3-70bfor better accuracy - Field descriptions - Add clear descriptions to your Pydantic fields to guide extraction
- Retry limit - Increase
max_retriesto give the model more attempts - Input clarity - Ensure your prompt clearly describes what to extract
How do I handle optional fields?
How do I handle optional fields?
Use Python’s union syntax with Instructor will extract these fields if present, or set them to
None to make fields optional:None if not found in the input text.What's the difference between sync and async clients?
What's the difference between sync and async clients?
Use async clients when you need to:
- Handle multiple requests concurrently
- Integrate with async frameworks (FastAPI, aiohttp, etc.)
- Maximize throughput in high-performance applications
- Simple scripts and notebooks
- Sequential processing
- Easier debugging and testing
AsyncCerebras instead of Cerebras.How does Instructor handle retries?
How does Instructor handle retries?
Instructor automatically retries requests when validation fails. You can control this behavior with the On each retry, Instructor sends the validation error back to the model, allowing it to correct its response. This significantly improves accuracy for complex extractions.
max_retries parameter:Next Steps
- Explore the Instructor documentation for advanced features like hooks, custom validators, and more
- Migrate to GLM4.6: Ready to upgrade? Follow our migration guide to start using our latest model
- Try different Cerebras models to find the best balance of speed and capability for your use case
- Learn about Pydantic validation to create robust data models

