2.2 KiB
2.2 KiB
Providers Directory Structure
This directory contains implementations for all supported LLM providers in the instructor library.
Provider Organization
Each provider is organized in its own subdirectory with the following structure:
providers/
├── provider_name/
│ ├── __init__.py
│ ├── client.py # Provider-specific client factory (optional)
│ └── utils.py # Provider-specific utilities (optional)
File Structure Patterns
Providers with both client.py and utils.py
- anthropic, bedrock, cerebras, cohere, fireworks, gemini, mistral, perplexity, writer, xai
- These providers require custom response handling logic and utility functions
client.py: Contains thefrom_<provider>()factory functionutils.py: Contains provider-specific response handlers, reask functions, and message formatting
Providers with only client.py
- genai, groq, vertexai
- These are simpler providers that use standard response handling from the core
- They don't require custom utility functions
Special Case: OpenAI (only utils.py)
- OpenAI doesn't have a
client.pybecausefrom_openai()is defined incore/client.py - This is because OpenAI is the reference implementation that other providers are based on
- OpenAI utilities are still needed by the core processing logic for standard handling
Adding a New Provider
When adding a new provider:
- Create a new subdirectory under
providers/ - Add an
__init__.pyfile (can be minimal) - Create
client.pywith afrom_<provider>()function if needed - Create
utils.pyonly if you need custom:- Response handlers (e.g.,
handle_<provider>_json()) - Reask functions (e.g.,
reask_<provider>_tools()) - Message formatting (e.g.,
convert_to_<provider>_messages())
- Response handlers (e.g.,
- Update
providers/__init__.pyto conditionally import your provider - Update the main
instructor/__init__.pyto export the factory function
Import Structure
- Provider modules use relative imports with
...to access parent modules - Example:
from ...core.exceptions import ProviderError - This maintains clean separation between provider implementations and core functionality