Skip to main content
Instructor is a Python library that makes it easy to get structured, validated outputs from LLMs. Instead of parsing raw text responses, you define Pydantic models and Instructor handles the extraction, validation, and type conversion automatically. This guide shows you how to use Instructor with Cerebras’s ultra-fast inference.

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 .env file in your project directory to store your API key securely. This keeps your credentials out of your code and version control.
Then load it in your Python code:
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

If you’re seeing validation errors, check:
  1. Model capability - Smaller models may struggle with complex structures. Try gpt-oss-120b for better accuracy
  2. Field descriptions - Add clear descriptions to your Pydantic fields to guide extraction
  3. Retry limit - Increase max_retries to give the model more attempts
  4. Input clarity - Ensure your prompt clearly describes what to extract
Example with better guidance:
Use Python’s union syntax with None to make fields optional:
Instructor will extract these fields if present, or set them to None if not found in the input text.
Use async clients when you need to:
  • Handle multiple requests concurrently
  • Integrate with async frameworks (FastAPI, aiohttp, etc.)
  • Maximize throughput in high-performance applications
Use sync clients for:
  • Simple scripts and notebooks
  • Sequential processing
  • Easier debugging and testing
Both provide the same functionality, just different execution models. Initialize async clients with AsyncCerebras instead of Cerebras.
Instructor automatically retries requests when validation fails. You can control this behavior with the max_retries parameter:
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.

Available Models

Instructor works with all Cerebras models for structured data extraction: Simply change the model parameter in your API calls to switch between models.

Next Steps