Skip to content

Fix: Correctly load built-in providers when using explicit 'provider' in ModelConfig#349

Open
siddikisahil47 wants to merge 4 commits intogoogle:mainfrom
siddikisahil47:fix-issue-320-provider-loading
Open

Fix: Correctly load built-in providers when using explicit 'provider' in ModelConfig#349
siddikisahil47 wants to merge 4 commits intogoogle:mainfrom
siddikisahil47:fix-issue-320-provider-loading

Conversation

@siddikisahil47
Copy link

@siddikisahil47 siddikisahil47 commented Feb 10, 2026

Description

Fixes a crash in _create_model_with_schema where built-in providers were not loaded when ModelConfig was initialized with an explicit provider argument.

Previously, providers.load_builtins_once() and providers.load_plugins_once() were only called inside the else block — meaning they were skipped when config.provider was set. This caused provider resolution to fail with an InferenceConfigError.

Root cause (in langextract/factory.py, _create_model_with_schema):

# BEFORE (buggy):
if config.provider:
    provider_class = router.resolve_provider(config.provider)  # builtins NOT loaded!
else:
    providers.load_builtins_once()   # only called here
    providers.load_plugins_once()
    provider_class = router.resolve(config.model_id)

Fix:

# AFTER (fixed):
providers.load_builtins_once()   # always load first
providers.load_plugins_once()

if config.provider:
    provider_class = router.resolve_provider(config.provider)
else:
    provider_class = router.resolve(config.model_id)

This aligns _create_model_with_schema with create_model, which already handles this correctly (lines 142-149).

Reproduction:

from langextract import factory
config = factory.ModelConfig(model_id='gpt-4o', provider='openai')
factory._create_model_with_schema(config)

Error before fix:

Traceback (most recent call last):
  File "langextract/factory.py", line 224, in _create_model_with_schema
    provider_class = router.resolve_provider(config.provider)
  File "langextract/providers/router.py", line 211, in resolve_provider
    raise exceptions.InferenceConfigError(
langextract.core.exceptions.InferenceConfigError: No provider found matching: 'openai'. Available providers can be listed with list_providers()

After fix: Provider resolves successfully ✅

Fixes #320

Bug fix

How Has This Been Tested?

Added a new unit test file tests/factory_provider_loading_test.py:

$ python -m unittest tests/factory_provider_loading_test.py
.
----------------------------------------------------------------------
Ran 1 test in 2.316s

OK

Test details:

  • Clears the provider registry to simulate a fresh state.
  • Creates a ModelConfig with explicit provider="openai".
  • Calls _create_model_with_schema and asserts that we do not get a "No provider found matching" error.
  • Accepts other errors (e.g., missing API key) as success — they prove the provider class was found and loaded.

Code style verified with ./autoformat.sh (all pre-commit hooks passed).

Checklist:

  • I have read and acknowledged Google's Open Source Code of conduct.
  • I have read the Contributing page, and I either signed the Google Individual CLA or am covered by my company's Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed Google's Python Style Guide and ran pylint over the affected code.

@github-actions github-actions bot added the size/S Pull request with 50-150 lines changed label Feb 10, 2026
@telackey
Copy link

I think this is essentially the same as #331 .

@github-actions
Copy link

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

1 similar comment
@github-actions
Copy link

github-actions bot commented Mar 5, 2026

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Pull request with 50-150 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

config.provider doesn't load builtin providers

2 participants