Skip to content

The agent engineering platform.

refactor(langchain): rename `_SUPPORTED_PROVIDERS` -> `_BUILTIN_PROVIDERS` (#35100)

Mason Daughertyce5f73e07c98
diff --git a/libs/langchain_v1/langchain/chat_models/base.py b/libs/langchain_v1/langchain/chat_models/base.py
index 7148bd44b5..b5f7cec785 100644
--- a/libs/langchain_v1/langchain/chat_models/base.py
+++ b/libs/langchain_v1/langchain/chat_models/base.py
@@ -35,7 +35,7 @@ def _call(cls: type[BaseChatModel], **kwargs: Any) -> BaseChatModel:
return cls(**kwargs)
-_SUPPORTED_PROVIDERS: dict[str, tuple[str, str, Callable[..., BaseChatModel]]] = {
+_BUILTIN_PROVIDERS: dict[str, tuple[str, str, Callable[..., BaseChatModel]]] = {
"anthropic": ("langchain_anthropic", "ChatAnthropic", _call),
"azure_ai": ("langchain_azure_ai.chat_models", "AzureAIChatCompletionsModel", _call),
"azure_openai": ("langchain_openai", "AzureChatOpenAI", _call),
@@ -81,6 +81,18 @@ Each entry maps a provider key to a tuple of:
not exported from the package root.
- `class_name`: The name of the chat model class to import.
- `creator_func`: A callable that instantiates the class with provided kwargs.
+
+!!! note
+
+ This dict is not exhaustive of all providers supported by LangChain, but is
+ meant to cover the most popular ones and serve as a template for adding more
+ providers in the future. If a provider is not in this dict, it can still be
+ used with `init_chat_model` as long as its integration package is installed,
+ but the provider key will not be inferred from the model name and must be
+ specified explicitly via the `model_provider` parameter.
+
+ Refer to the LangChain [integration documentation](https://docs.langchain.com/oss/python/integrations/providers/overview)
+ for a full list of supported providers and their corresponding packages.
"""
@@ -111,7 +123,7 @@ def _import_module(module: str, class_name: str) -> ModuleType:
raise ImportError(msg) from e
[email protected]_cache(maxsize=len(_SUPPORTED_PROVIDERS))
[email protected]_cache(maxsize=len(_BUILTIN_PROVIDERS))
def _get_chat_model_creator(
provider: str,
) -> Callable[..., BaseChatModel]:
@@ -122,22 +134,22 @@ def _get_chat_model_creator(
Args:
provider: The name of the model provider (e.g., `'openai'`, `'anthropic'`).
- Must be a key in `_SUPPORTED_PROVIDERS`.
+ Must be a key in `_BUILTIN_PROVIDERS`.
Returns:
A callable that accepts model kwargs and returns a `BaseChatModel` instance for
the specified provider.
Raises:
- ValueError: If the provider is not in `_SUPPORTED_PROVIDERS`.
+ ValueError: If the provider is not in `_BUILTIN_PROVIDERS`.
ImportError: If the provider's integration package is not installed.
"""
- if provider not in _SUPPORTED_PROVIDERS:
- supported = ", ".join(_SUPPORTED_PROVIDERS.keys())
+ if provider not in _BUILTIN_PROVIDERS:
+ supported = ", ".join(_BUILTIN_PROVIDERS.keys())
msg = f"Unsupported {provider=}.\n\nSupported model providers are: {supported}"
raise ValueError(msg)
- pkg, class_name, creator_func = _SUPPORTED_PROVIDERS[provider]
+ pkg, class_name, creator_func = _BUILTIN_PROVIDERS[provider]
try:
module = _import_module(pkg, class_name)
except ImportError as e:
@@ -549,7 +561,7 @@ def _parse_model(model: str, model_provider: str | None) -> tuple[str, str]:
if (
not model_provider
and ":" in model
- and model.split(":", maxsplit=1)[0] in _SUPPORTED_PROVIDERS
+ and model.split(":", maxsplit=1)[0] in _BUILTIN_PROVIDERS
):
model_provider = model.split(":", maxsplit=1)[0]
model = ":".join(model.split(":")[1:])
@@ -559,7 +571,7 @@ def _parse_model(model: str, model_provider: str | None) -> tuple[str, str]:
if not model_provider:
# Enhanced error message with suggestions
- supported_list = ", ".join(sorted(_SUPPORTED_PROVIDERS))
+ supported_list = ", ".join(sorted(_BUILTIN_PROVIDERS))
msg = (
f"Unable to infer model provider for {model=}. "
f"Please specify 'model_provider' directly.\n\n"
diff --git a/libs/langchain_v1/langchain/embeddings/base.py b/libs/langchain_v1/langchain/embeddings/base.py
index 6d8205c4d8..205481ddd1 100644
--- a/libs/langchain_v1/langchain/embeddings/base.py
+++ b/libs/langchain_v1/langchain/embeddings/base.py
@@ -12,7 +12,7 @@ def _call(cls: type[Embeddings], **kwargs: Any) -> Embeddings:
return cls(**kwargs)
-_SUPPORTED_PROVIDERS: dict[str, tuple[str, str, Callable[..., Embeddings]]] = {
+_BUILTIN_PROVIDERS: dict[str, tuple[str, str, Callable[..., Embeddings]]] = {
"azure_openai": ("langchain_openai", "AzureOpenAIEmbeddings", _call),
"bedrock": (
"langchain_aws",
@@ -38,10 +38,22 @@ Each entry maps a provider key to a tuple of:
- `module_path`: The Python module path containing the embeddings class.
- `class_name`: The name of the embeddings class to import.
- `creator_func`: A callable that instantiates the class with provided kwargs.
+
+!!! note
+
+ This dict is not exhaustive of all providers supported by LangChain, but is
+ meant to cover the most popular ones and serve as a template for adding more
+ providers in the future. If a provider is not in this dict, it can still be
+ used with `init_chat_model` as long as its integration package is installed,
+ but the provider key will not be inferred from the model name and must be
+ specified explicitly via the `model_provider` parameter.
+
+ Refer to the LangChain [integration documentation](https://docs.langchain.com/oss/python/integrations/providers/overview)
+ for a full list of supported providers and their corresponding packages.
"""
[email protected]_cache(maxsize=len(_SUPPORTED_PROVIDERS))
[email protected]_cache(maxsize=len(_BUILTIN_PROVIDERS))
def _get_embeddings_class_creator(provider: str) -> Callable[..., Embeddings]:
"""Return a factory function that creates an embeddings model for the given provider.
@@ -50,17 +62,17 @@ def _get_embeddings_class_creator(provider: str) -> Callable[..., Embeddings]:
Args:
provider: The name of the model provider (e.g., `'openai'`, `'cohere'`).
- Must be a key in `_SUPPORTED_PROVIDERS`.
+ Must be a key in `_BUILTIN_PROVIDERS`.
Returns:
A callable that accepts model kwargs and returns an `Embeddings` instance for
the specified provider.
Raises:
- ValueError: If the provider is not in `_SUPPORTED_PROVIDERS`.
+ ValueError: If the provider is not in `_BUILTIN_PROVIDERS`.
ImportError: If the provider's integration package is not installed.
"""
- if provider not in _SUPPORTED_PROVIDERS:
+ if provider not in _BUILTIN_PROVIDERS:
msg = (
f"Provider '{provider}' is not supported.\n"
f"Supported providers and their required packages:\n"
@@ -68,7 +80,7 @@ def _get_embeddings_class_creator(provider: str) -> Callable[..., Embeddings]:
)
raise ValueError(msg)
- module_name, class_name, creator_func = _SUPPORTED_PROVIDERS[provider]
+ module_name, class_name, creator_func = _BUILTIN_PROVIDERS[provider]
try:
module = importlib.import_module(module_name)
except ImportError as e:
@@ -83,7 +95,7 @@ def _get_embeddings_class_creator(provider: str) -> Callable[..., Embeddings]:
def _get_provider_list() -> str:
"""Get formatted list of providers and their packages."""
return "\n".join(
- f" - {p}: {pkg[0].replace('_', '-')}" for p, pkg in _SUPPORTED_PROVIDERS.items()
+ f" - {p}: {pkg[0].replace('_', '-')}" for p, pkg in _BUILTIN_PROVIDERS.items()
)
@@ -121,7 +133,7 @@ def _parse_model_string(model_name: str) -> tuple[str, str]:
f" - openai:text-embedding-3-small\n"
f" - bedrock:amazon.titan-embed-text-v1\n"
f" - cohere:embed-english-v3.0\n"
- f"Supported providers: {_SUPPORTED_PROVIDERS.keys()}"
+ f"Supported providers: {_BUILTIN_PROVIDERS.keys()}"
)
raise ValueError(msg)
@@ -129,7 +141,7 @@ def _parse_model_string(model_name: str) -> tuple[str, str]:
provider = provider.lower().strip()
model = model.strip()
- if provider not in _SUPPORTED_PROVIDERS:
+ if provider not in _BUILTIN_PROVIDERS:
msg = (
f"Provider '{provider}' is not supported.\n"
f"Supported providers and their required packages:\n"
@@ -161,11 +173,11 @@ def _infer_model_and_provider(
"1. A model string in format 'provider:model-name'\n"
" Example: 'openai:text-embedding-3-small'\n"
"2. Or explicitly set provider from: "
- f"{_SUPPORTED_PROVIDERS.keys()}"
+ f"{_BUILTIN_PROVIDERS.keys()}"
)
raise ValueError(msg)
- if provider not in _SUPPORTED_PROVIDERS:
+ if provider not in _BUILTIN_PROVIDERS:
msg = (
f"Provider '{provider}' is not supported.\n"
f"Supported providers and their required packages:\n"
@@ -247,7 +259,7 @@ def init_embeddings(
"""
if not model:
- providers = _SUPPORTED_PROVIDERS.keys()
+ providers = _BUILTIN_PROVIDERS.keys()
msg = f"Must specify model name. Supported providers are: {', '.join(providers)}"
raise ValueError(msg)
diff --git a/libs/langchain_v1/tests/integration_tests/embeddings/test_base.py b/libs/langchain_v1/tests/integration_tests/embeddings/test_base.py
index d5f17af8b6..7c3310a7af 100644
--- a/libs/langchain_v1/tests/integration_tests/embeddings/test_base.py
+++ b/libs/langchain_v1/tests/integration_tests/embeddings/test_base.py
@@ -5,7 +5,7 @@ import importlib
import pytest
from langchain_core.embeddings import Embeddings
-from langchain.embeddings.base import _SUPPORTED_PROVIDERS, init_embeddings
+from langchain.embeddings.base import _BUILTIN_PROVIDERS, init_embeddings
@pytest.mark.parametrize(
@@ -18,7 +18,7 @@ from langchain.embeddings.base import _SUPPORTED_PROVIDERS, init_embeddings
],
)
async def test_init_embedding_model(provider: str, model: str) -> None:
- package = _SUPPORTED_PROVIDERS[provider][0]
+ package = _BUILTIN_PROVIDERS[provider][0]
try:
importlib.import_module(package)
except ImportError:
diff --git a/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py b/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
index b2ac9345a8..33dd48b454 100644
--- a/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
+++ b/libs/langchain_v1/tests/unit_tests/chat_models/test_chat_models.py
@@ -8,7 +8,7 @@ from langchain_core.runnables import RunnableConfig, RunnableSequence
from pydantic import SecretStr
from langchain.chat_models import __all__, init_chat_model
-from langchain.chat_models.base import _SUPPORTED_PROVIDERS, _attempt_infer_model_provider
+from langchain.chat_models.base import _BUILTIN_PROVIDERS, _attempt_infer_model_provider
if TYPE_CHECKING:
from langchain_core.language_models import BaseChatModel
@@ -64,7 +64,7 @@ def test_init_unknown_provider() -> None:
def test_supported_providers_is_sorted() -> None:
"""Test that supported providers are sorted alphabetically."""
- assert list(_SUPPORTED_PROVIDERS) == sorted(_SUPPORTED_PROVIDERS.keys())
+ assert list(_BUILTIN_PROVIDERS) == sorted(_BUILTIN_PROVIDERS.keys())
@pytest.mark.parametrize(
diff --git a/libs/langchain_v1/tests/unit_tests/embeddings/test_base.py b/libs/langchain_v1/tests/unit_tests/embeddings/test_base.py
index 57efc4b6e6..901a1138a2 100644
--- a/libs/langchain_v1/tests/unit_tests/embeddings/test_base.py
+++ b/libs/langchain_v1/tests/unit_tests/embeddings/test_base.py
@@ -3,7 +3,7 @@
import pytest
from langchain.embeddings.base import (
- _SUPPORTED_PROVIDERS,
+ _BUILTIN_PROVIDERS,
_infer_model_and_provider,
_parse_model_string,
)
@@ -46,7 +46,7 @@ def test_parse_model_string_errors() -> None:
):
_parse_model_string("invalid-provider:model-name")
- for provider in _SUPPORTED_PROVIDERS:
+ for provider in _BUILTIN_PROVIDERS:
with pytest.raises(ValueError, match=f"{provider}"):
_parse_model_string("invalid-provider:model-name")
@@ -92,21 +92,21 @@ def test_infer_model_and_provider_errors() -> None:
with pytest.raises(ValueError, match="Provider 'invalid' is not supported") as exc:
_infer_model_and_provider("model", provider="invalid")
# Test provider list is in error
- for provider in _SUPPORTED_PROVIDERS:
+ for provider in _BUILTIN_PROVIDERS:
assert provider in str(exc.value)
@pytest.mark.parametrize(
"provider",
- sorted(_SUPPORTED_PROVIDERS.keys()),
+ sorted(_BUILTIN_PROVIDERS.keys()),
)
def test_supported_providers_package_names(provider: str) -> None:
"""Test that all supported providers have valid package names."""
- package = _SUPPORTED_PROVIDERS[provider][0]
+ package = _BUILTIN_PROVIDERS[provider][0]
assert "-" not in package
assert package.startswith("langchain_")
assert package.islower()
def test_is_sorted() -> None:
- assert list(_SUPPORTED_PROVIDERS) == sorted(_SUPPORTED_PROVIDERS.keys())
+ assert list(_BUILTIN_PROVIDERS) == sorted(_BUILTIN_PROVIDERS.keys())