Skip to content

The agent engineering platform.

fix(langchain,langchain-classic): more descriptive error msg when dep is not installed (#34679)

Mason Daugherty2e8744559d7e
diff --git a/.github/workflows/pr_lint.yml b/.github/workflows/pr_lint.yml
index 10c11a09b0..6387d69f9f 100644
--- a/.github/workflows/pr_lint.yml
+++ b/.github/workflows/pr_lint.yml
@@ -27,7 +27,7 @@
# * release — prepare a new release
#
# Allowed Scope(s) (optional):
-# core, cli, langchain, langchain_v1, langchain-classic, model-profiles,
+# core, cli, langchain, langchain-classic, model-profiles,
# standard-tests, text-splitters, docs, anthropic, chroma, deepseek, exa,
# fireworks, groq, huggingface, mistralai, nomic, ollama, openai,
# perplexity, prompty, qdrant, xai, infra, deps
diff --git a/libs/langchain/langchain_classic/chat_models/base.py b/libs/langchain/langchain_classic/chat_models/base.py
index e648d069ce..a32b7a073b 100644
--- a/libs/langchain/langchain_classic/chat_models/base.py
+++ b/libs/langchain/langchain_classic/chat_models/base.py
@@ -377,125 +377,125 @@ def _init_chat_model_helper(
) -> BaseChatModel:
model, model_provider = _parse_model(model, model_provider)
if model_provider == "openai":
- _check_pkg("langchain_openai")
+ _check_pkg("langchain_openai", "ChatOpenAI")
from langchain_openai import ChatOpenAI
return ChatOpenAI(model=model, **kwargs)
if model_provider == "anthropic":
- _check_pkg("langchain_anthropic")
+ _check_pkg("langchain_anthropic", "ChatAnthropic")
from langchain_anthropic import ChatAnthropic
return ChatAnthropic(model=model, **kwargs) # type: ignore[call-arg,unused-ignore]
if model_provider == "azure_openai":
- _check_pkg("langchain_openai")
+ _check_pkg("langchain_openai", "AzureChatOpenAI")
from langchain_openai import AzureChatOpenAI
return AzureChatOpenAI(model=model, **kwargs)
if model_provider == "azure_ai":
- _check_pkg("langchain_azure_ai")
+ _check_pkg("langchain_azure_ai", "AzureAIChatCompletionsModel")
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
return AzureAIChatCompletionsModel(model=model, **kwargs)
if model_provider == "cohere":
- _check_pkg("langchain_cohere")
+ _check_pkg("langchain_cohere", "ChatCohere")
from langchain_cohere import ChatCohere
return ChatCohere(model=model, **kwargs)
if model_provider == "google_vertexai":
- _check_pkg("langchain_google_vertexai")
+ _check_pkg("langchain_google_vertexai", "ChatVertexAI")
from langchain_google_vertexai import ChatVertexAI
return ChatVertexAI(model=model, **kwargs)
if model_provider == "google_genai":
- _check_pkg("langchain_google_genai")
+ _check_pkg("langchain_google_genai", "ChatGoogleGenerativeAI")
from langchain_google_genai import ChatGoogleGenerativeAI
return ChatGoogleGenerativeAI(model=model, **kwargs)
if model_provider == "fireworks":
- _check_pkg("langchain_fireworks")
+ _check_pkg("langchain_fireworks", "ChatFireworks")
from langchain_fireworks import ChatFireworks
return ChatFireworks(model=model, **kwargs)
if model_provider == "ollama":
try:
- _check_pkg("langchain_ollama")
+ _check_pkg("langchain_ollama", "ChatOllama")
from langchain_ollama import ChatOllama
except ImportError:
# For backwards compatibility
try:
- _check_pkg("langchain_community")
+ _check_pkg("langchain_community", "ChatOllama")
from langchain_community.chat_models import ChatOllama
except ImportError:
# If both langchain-ollama and langchain-community aren't available,
# raise an error related to langchain-ollama
- _check_pkg("langchain_ollama")
+ _check_pkg("langchain_ollama", "ChatOllama")
return ChatOllama(model=model, **kwargs)
if model_provider == "together":
- _check_pkg("langchain_together")
+ _check_pkg("langchain_together", "ChatTogether")
from langchain_together import ChatTogether
return ChatTogether(model=model, **kwargs)
if model_provider == "mistralai":
- _check_pkg("langchain_mistralai")
+ _check_pkg("langchain_mistralai", "ChatMistralAI")
from langchain_mistralai import ChatMistralAI
return ChatMistralAI(model=model, **kwargs) # type: ignore[call-arg,unused-ignore]
if model_provider == "huggingface":
- _check_pkg("langchain_huggingface")
+ _check_pkg("langchain_huggingface", "ChatHuggingFace")
from langchain_huggingface import ChatHuggingFace
return ChatHuggingFace.from_model_id(model_id=model, **kwargs)
if model_provider == "groq":
- _check_pkg("langchain_groq")
+ _check_pkg("langchain_groq", "ChatGroq")
from langchain_groq import ChatGroq
return ChatGroq(model=model, **kwargs)
if model_provider == "bedrock":
- _check_pkg("langchain_aws")
+ _check_pkg("langchain_aws", "ChatBedrock")
from langchain_aws import ChatBedrock
# TODO: update to use model= once ChatBedrock supports
return ChatBedrock(model_id=model, **kwargs)
if model_provider == "bedrock_converse":
- _check_pkg("langchain_aws")
+ _check_pkg("langchain_aws", "ChatBedrockConverse")
from langchain_aws import ChatBedrockConverse
return ChatBedrockConverse(model=model, **kwargs)
if model_provider == "google_anthropic_vertex":
- _check_pkg("langchain_google_vertexai")
+ _check_pkg("langchain_google_vertexai", "ChatAnthropicVertex")
from langchain_google_vertexai.model_garden import ChatAnthropicVertex
return ChatAnthropicVertex(model=model, **kwargs)
if model_provider == "deepseek":
- _check_pkg("langchain_deepseek", pkg_kebab="langchain-deepseek")
+ _check_pkg("langchain_deepseek", "ChatDeepSeek", pkg_kebab="langchain-deepseek")
from langchain_deepseek import ChatDeepSeek
return ChatDeepSeek(model=model, **kwargs)
if model_provider == "nvidia":
- _check_pkg("langchain_nvidia_ai_endpoints")
+ _check_pkg("langchain_nvidia_ai_endpoints", "ChatNVIDIA")
from langchain_nvidia_ai_endpoints import ChatNVIDIA
return ChatNVIDIA(model=model, **kwargs)
if model_provider == "ibm":
- _check_pkg("langchain_ibm")
+ _check_pkg("langchain_ibm", "ChatWatsonx")
from langchain_ibm import ChatWatsonx
return ChatWatsonx(model_id=model, **kwargs)
if model_provider == "xai":
- _check_pkg("langchain_xai")
+ _check_pkg("langchain_xai", "ChatXAI")
from langchain_xai import ChatXAI
return ChatXAI(model=model, **kwargs)
if model_provider == "perplexity":
- _check_pkg("langchain_perplexity")
+ _check_pkg("langchain_perplexity", "ChatPerplexity")
from langchain_perplexity import ChatPerplexity
return ChatPerplexity(model=model, **kwargs)
if model_provider == "upstage":
- _check_pkg("langchain_upstage")
+ _check_pkg("langchain_upstage", "ChatUpstage")
from langchain_upstage import ChatUpstage
return ChatUpstage(model=model, **kwargs)
@@ -630,11 +630,12 @@ def _parse_model(model: str, model_provider: str | None) -> tuple[str, str]:
return model, model_provider
-def _check_pkg(pkg: str, *, pkg_kebab: str | None = None) -> None:
+def _check_pkg(pkg: str, class_name: str, *, pkg_kebab: str | None = None) -> None:
if not util.find_spec(pkg):
pkg_kebab = pkg_kebab if pkg_kebab is not None else pkg.replace("_", "-")
msg = (
- f"Unable to import {pkg}. Please install with `pip install -U {pkg_kebab}`"
+ f"Initializing {class_name} requires the {pkg_kebab} package. "
+ f"Please install it with `pip install {pkg_kebab}`"
)
raise ImportError(msg)
diff --git a/libs/langchain_v1/langchain/chat_models/base.py b/libs/langchain_v1/langchain/chat_models/base.py
index 89a8b00e53..7bd12911c6 100644
--- a/libs/langchain_v1/langchain/chat_models/base.py
+++ b/libs/langchain_v1/langchain/chat_models/base.py
@@ -84,11 +84,12 @@ Each entry maps a provider key to a tuple of:
"""
-def _import_module(module: str) -> ModuleType:
+def _import_module(module: str, class_name: str) -> ModuleType:
"""Import a module by name.
Args:
module: The fully qualified module name to import (e.g., `'langchain_openai'`).
+ class_name: The name of the class being imported, used for error messages.
Returns:
The imported module.
@@ -103,7 +104,10 @@ def _import_module(module: str) -> ModuleType:
# Extract package name from module path (e.g., "langchain_azure_ai.chat_models"
# becomes "langchain-azure-ai")
pkg = module.split(".", maxsplit=1)[0].replace("_", "-")
- msg = f"Could not import {pkg} python package. Please install it with `pip install {pkg}`"
+ msg = (
+ f"Initializing {class_name} requires the {pkg} package. Please install it "
+ f"with `pip install {pkg}`"
+ )
raise ImportError(msg) from e
@@ -135,13 +139,13 @@ def _get_chat_model_creator(
pkg, class_name, creator_func = _SUPPORTED_PROVIDERS[provider]
try:
- module = _import_module(pkg)
+ module = _import_module(pkg, class_name)
except ImportError as e:
if provider != "ollama":
raise
# For backwards compatibility
try:
- module = _import_module("langchain_community.chat_models")
+ module = _import_module("langchain_community.chat_models", class_name)
except ImportError:
# If both langchain-ollama and langchain-community aren't available,
# raise an error related to langchain-ollama