Core Provider Tests
This directory contains unified tests that run across all core providers: OpenAI, Anthropic, Google (Gemini), Cohere, xAI, Mistral, Cerebras, Fireworks, Writer, and Perplexity.
Philosophy
Instead of duplicating the same tests for each provider, we use instructor.from_provider() with parameterization to run the same test suite against all providers simultaneously.
Test Organization
Core Tests (Run on All Providers)
These tests verify that core instructor functionality works consistently across providers:
- test_basic_extraction.py - Simple extraction, lists, nested models, field descriptions
- test_streaming.py - Partial streaming, Iterable streaming, union types
- test_validation.py - Validators, field constraints, custom validation
- test_retries.py - Retry logic and max_retries parameter
- test_response_modes.py - Different client methods (create, messages.create, etc.)
- test_simple_types.py - Simple types (int, bool, str, Literal, Union, Enum)
Configuration
shared_config.py
Located in tests/llm/shared_config.py, this file:
- Defines
PROVIDER_CONFIGSwith model names, modes, and required API keys - Implements
get_available_providers()to detect which providers are available - Provides
pytest_generate_tests()hook for automatic parameterization - Handles skipping when API keys or packages are missing
Usage in Tests
Tests use the provider_config fixture which is automatically parametrized:
def test_something(provider_config):
model, mode = provider_config
client = instructor.from_provider(model, mode=mode)
result = client.create(
response_model=MyModel,
messages=[{"role": "user", "content": "..."}],
)
assert isinstance(result, MyModel)
The test will automatically run for each available provider:
- OpenAI (if OPENAI_API_KEY is set)
- Anthropic (if ANTHROPIC_API_KEY is set)
- Google (if GOOGLE_API_KEY is set)
- Cohere (if COHERE_API_KEY is set)
- xAI (if XAI_API_KEY is set)
- Mistral (if MISTRAL_API_KEY is set)
- Cerebras (if CEREBRAS_API_KEY is set)
- Fireworks (if FIREWORKS_API_KEY is set)
- Writer (if WRITER_API_KEY is set)
- Perplexity (if PERPLEXITY_API_KEY is set)
Tests automatically skip if the API key or package is not available.
Running Tests
uv is Astral's fast Python package manager. Install it by following the official guide if it is not already on your PATH.
Run all core provider tests:
uv run pytest tests/llm/test_core_providers/ -v
Run specific test file:
uv run pytest tests/llm/test_core_providers/test_basic_extraction.py -v
Run specific test:
uv run pytest tests/llm/test_core_providers/test_basic_extraction.py::test_simple_extraction -v
Run tests for specific provider only:
# Only OpenAI
uv run pytest tests/llm/test_core_providers/ -k "openai" -v
# Only Anthropic
uv run pytest tests/llm/test_core_providers/ -k "anthropic" -v
# Only Google
uv run pytest tests/llm/test_core_providers/ -k "google" -v
Skip tests when API keys are missing:
Tests automatically skip if the required API key or package is not available.
Required API keys (set only what you have):
OPENAI_API_KEY- for OpenAIANTHROPIC_API_KEY- for AnthropicGOOGLE_API_KEY- for Google (Gemini)GOOGLE_GENAI_MODEL- model string for Google GenAI tests (e.g.,google/gemini-3-flash)COHERE_API_KEY- for CohereXAI_API_KEY- for xAI (Grok)MISTRAL_API_KEY- for MistralCEREBRAS_API_KEY- for CerebrasFIREWORKS_API_KEY- for FireworksWRITER_API_KEY- for WriterPERPLEXITY_API_KEY- for Perplexity
Current Models
All providers automatically skip if API keys are missing.
- OpenAI:
gpt-4o-miniwithMode.TOOLS - Anthropic:
claude-haiku-4-5-20251001withMode.ANTHROPIC_TOOLS - Google:
gemini-prowithMode.GENAI_STRUCTURED_OUTPUTS - Cohere:
command-a-03-2025withMode.COHERE_TOOLS - xAI:
grok-3-miniwithMode.XAI_TOOLS - Mistral:
ministral-8b-latestwithMode.MISTRAL_TOOLS - Cerebras:
llama3.1-70bwithMode.CEREBRAS_TOOLS - Fireworks:
llama-v3p1-70b-instructwithMode.FIREWORKS_TOOLS - Writer:
palmyra-x-004withMode.WRITER_TOOLS - Perplexity:
llama-3.1-sonar-large-128k-onlinewithMode.PERPLEXITY_JSON
To change models, edit tests/llm/shared_config.py.
Benefits
✅ Less code: ~3,500+ lines of duplicate code eliminated ✅ Easier maintenance: Update test logic once, applies to all providers ✅ Better coverage: Ensures all providers support core features ✅ Faster development: Add new providers by updating one config file ✅ Consistent behavior: Catches provider-specific quirks early
Migration Status
- ✅ Shared configuration created
- ✅ Core test files created (basic_extraction, streaming, validation, retries, response_modes, simple_types)
- ✅ util.py files updated to use
provider/modelformat - ✅ Provider-specific tests cleaned up (removed all duplicates)
- ✅ Deleted 6 entire provider directories (cerebras, fireworks, perplexity, cohere, xai, mistral)
- ✅ Deleted 35+ duplicate test files across remaining providers
Adding New Core Tests
- Create test file in
tests/llm/test_core_providers/ - Use
provider_configparameter in test functions - Extract
model, mode = provider_config - Create client with
instructor.from_provider(model, mode=mode) - Write provider-agnostic assertions
Adding New Providers
To add a new provider to core tests:
- Update
PROVIDER_CONFIGSintests/llm/shared_config.py - Add tuple:
("provider/model-name", instructor.Mode.PROVIDER_SPECIFIC_MODE, "API_KEY_ENV_VAR", "package.name") - Pick the mode that matches the provider's client (see
instructor.Modeor the provider guide). - Tests will automatically run against the new provider!