Overview
The provider registry (router) system allows LangExtract to automatically resolve model identifiers to the appropriate provider class. It supports lazy registration to avoid importing provider dependencies until they’re actually needed.Module
Registration Functions
register_lazy()
Registers a provider lazily using a string import path.One or more regex patterns to match model IDs. Each pattern can be a string (compiled to regex) or a compiled regex pattern.
Import path in format
"module.path:ClassName". The module is imported only when the provider is needed.Priority for resolution. Higher values win on pattern conflicts.
register()
Decorator to register a provider class directly.One or more regex patterns to match model IDs.
Priority for resolution. Higher values win on pattern conflicts.
Decorator function that registers the class and returns it unchanged.
Resolution Functions
resolve()
Resolves a model ID to its provider class.The model identifier to resolve (e.g.,
"gemini-pro", "gpt-4").The provider class that handles this model ID.
InferenceConfigError if no provider is registered for the model ID.
Example:
resolve_provider()
Resolves a provider name to its provider class.The provider name (e.g.,
"gemini", "openai") or class name (e.g., "GeminiLanguageModel").The provider class.
InferenceConfigError if no provider matches the name.
Example:
Utility Functions
list_providers()
Lists all registered providers with their patterns and priorities.List of (patterns, priority) tuples for debugging.
clear()
Clears all registered providers. Mainly for testing.Plugin System
LangExtract automatically discovers and loads provider plugins via entry points.load_plugins_once()
Loads provider plugins from installed packages.- Discovers plugins using the
langextract.providersentry point group - Loads each plugin’s provider class
- Registers patterns from the provider’s
get_model_patterns()method - Is idempotent (multiple calls have no effect)
- Can be disabled by setting
LANGEXTRACT_DISABLE_PLUGINS=1
load_builtins_once()
Loads built-in providers (Gemini, OpenAI, Ollama).- Registers built-in provider patterns
- Is idempotent (multiple calls have no effect)
- Uses lazy registration to avoid importing dependencies
Pattern Matching
The registry uses regex patterns to match model IDs:- Patterns are checked in priority order (highest first)
- First matching pattern wins
- Patterns can be strings (auto-compiled) or compiled regex objects
- Use
^and$anchors for exact matches
Priority System
When multiple patterns match the same model ID, priority determines which provider is used:- Higher priority wins (e.g., 20 beats 10)
- Built-in providers use priority 10
- Plugins default to priority 20
- Custom registrations can use any priority
- Same-priority conflicts are resolved by registration order
Usage Example
Notes
- Resolution results are cached (LRU cache, maxsize=128)
- Use
clear()to reset the cache in tests - Lazy registration avoids importing dependencies until needed
- Plugins are auto-discovered via entry points
- Set
LANGEXTRACT_DISABLE_PLUGINS=1to disable plugin loading