Instructor: A Free Library That Forces LLMs to Return Structured Data
Instructor is a Python library that patches OpenAI's client to return validated, typed data structures using Pydantic models, eliminating the need to parse JSON from LLM outputs.
Why it matters
Instructor simplifies the integration of LLMs into applications by providing a consistent, validated data structure, reducing the need for custom parsing and error handling.
Key Points
- 1Structured extraction: LLM outputs are validated against Pydantic/Zod schemas
- 2Automatic retries: If validation fails, Instructor re-prompts with the error
- 3Streaming: Partial objects stream as they're generated
- 4Supports multiple providers: OpenAI, Anthropic, Google, Mistral, Ollama, LiteLLM
- 5First-class support for Python and TypeScript
Details
Instructor is a lightweight library that patches existing LLM clients to return structured data instead of just strings. It uses Pydantic models to define the expected data structure, and automatically validates the LLM output against these models. If validation fails, Instructor will re-prompt the LLM and return the corrected, validated data. Instructor also supports streaming, allowing partial objects to be returned as they're generated. The library supports multiple LLM providers, including OpenAI, Anthropic, Google, and others. Instructor is designed to be a simple, non-intrusive solution that integrates with existing LLM-powered applications.
No comments yet
Be the first to comment