What is Instructor?
Instructor is a Python framework for extracting structured outputs from Large Language Models (LLMs). It uses Pydantic models to define the expected output structure and ensures type-safe, validated responses.Key Features of Instructor
- Type-Safe Structured Outputs: Instructor leverages Pydantic models to define expected output schemas, ensuring type safety and automatic validation of LLM responses. This eliminates the need for manual parsing and reduces errors in production applications.
- Automatic Validation and Error Handling: Built-in validation mechanisms automatically retry failed requests and provide detailed error messages when LLM outputs don’t match the expected schema. This makes applications more robust and reliable in production environments.
- Support for Complex Data Structures: Instructor supports nested models, lists, enums, and other complex Pydantic types, enabling extraction of sophisticated data structures from natural language. It also supports streaming responses for real-time applications and partial extraction scenarios.
Prerequisites
Before integrating Instructor with TrueFoundry, ensure you have:- TrueFoundry Account: Create a TrueFoundry account with at least one model provider and generate a Personal Access Token by following the instructions in Generating Tokens. For a quick setup guide, see our Gateway Quick Start
- Instructor Installation: Install Instructor using pip:
pip install instructor
- OpenAI Library: Install the OpenAI Python library:
pip install openai
- Pydantic: Install Pydantic for data validation:
pip install pydantic
Setup Process
Step 1: Install Dependencies
Step 2: Configure Instructor with TrueFoundry Gateway
Get your TrueFoundry Gateway API key, base URL, and model name from the unified code snippet in your TrueFoundry playground: