Architecture
The provider system uses three core components:- Registry - Maps model ID patterns to provider classes
- Factory - Creates provider instances based on model IDs
- Providers - Implement the
BaseLanguageModelinterface
How Provider Selection Works
When you calllx.extract(model_id="gemini-2.5-flash", ...), here’s what happens:
- Factory receives model_id: “gemini-2.5-flash”
- Registry searches patterns: Each provider registers regex patterns
- First match wins: Returns the matching provider class
- Provider instantiated: With model_id and any kwargs
- Inference runs: Using the selected provider
Provider Types
Core Providers (Always Available)
Shipped with LangExtract, dependencies included:- Gemini - Google’s Gemini models (API key or Vertex AI)
- Ollama - Local models via Ollama (no API key required)
Built-in with Optional Dependencies
Shipped with LangExtract, but requires extra installation:- OpenAI - OpenAI’s GPT models
- Code included in package
- Requires:
pip install langextract[openai]
External Plugins (Third-party)
Separate packages that extend LangExtract:- Installed separately:
pip install langextract-yourprovider - Auto-discovered: Uses Python entry points for automatic registration
- Zero configuration: Import langextract and the provider is available
- Independent updates: Update providers without touching core
Usage Examples
Auto-Detection (Recommended)
The simplest approach - let LangExtract choose the provider:Explicit Provider Selection
When multiple providers might support the same model ID:- Full class name:
"GeminiLanguageModel","OpenAILanguageModel" - Partial match:
"gemini","openai","ollama"(case-insensitive)
Passing Parameters to Providers
Parameters flow fromlx.extract() to providers:
Direct Provider Usage
Plugin Discovery
External plugins are automatically discovered via Python entry points:Plugin loading is lazy - plugins are discovered when first needed. To manually trigger plugin loading:
lx.providers.load_plugins_once()Environment Variables
The factory automatically resolves API keys from environment:| Provider | Environment Variables (in priority order) |
|---|---|
| Gemini | GEMINI_API_KEY, LANGEXTRACT_API_KEY |
| OpenAI | OPENAI_API_KEY, LANGEXTRACT_API_KEY |
| Ollama | OLLAMA_BASE_URL (default: http://localhost:11434) |
Common Issues
Provider Not Found
registry.list_entries()
Missing Dependencies
Plugin Not Loading
Solutions:- Manually trigger loading:
lx.providers.load_plugins_once() - Check entry points are installed:
pip show -f your-package - Verify no typos in
pyproject.tomlentry point - Ensure package is installed:
pip list | grep your-package
Next Steps
Gemini Provider
Use Google’s Gemini models with API key or Vertex AI
OpenAI Provider
Use OpenAI’s GPT models
Ollama Provider
Run local models without API keys
Custom Providers
Create your own provider plugins