Skip to content

Commit 491eb9d

Browse files
santiagxfmdrxy
andauthored
fix(langchain,langchain-classic): update model provider classes for Azure AI Foundry (#35812)
Fixes #35811 Updates support for the "azure_ai" provider in both chat model and embeddings initialization, ensuring consistency across the codebase and removing the use of deprecated classes `AzureAIChatCompletionsModel` and `AzureAIEmbeddingsModel`. The changes primarily involve updating provider mappings and instantiation logic for Azure AI integrations. **Changes:** * Updated the chat model initialization logic in `langchain_classic` and `langchain_v1` to use `AzureAIOpenAIApiChatModel` instead of the deprecated `AzureAIChatCompletionsModel` for the "azure_ai" provider. [[1]](diffhunk://#diff-969731928b9d70dac2677539a711478df103ce70024b4c61e95bd7b584bb841dL395-R398) [[2]](diffhunk://#diff-31b725e3fe9b2f92342af5cda9bf6e61c4bc0b69efd8c25743269f45c52571b2L41-R41) * Added support for the "azure_ai" provider in embeddings initialization, mapping it to the new `AzureAIOpenAIApiEmbeddingsModel` class in both `langchain_classic` and `langchain_v1`. [[1]](diffhunk://#diff-9490c8d3daa7c5b66d10ce2961f2bc1a6a35005150125c905316adb728bf80d6R9) [[2]](diffhunk://#diff-9490c8d3daa7c5b66d10ce2961f2bc1a6a35005150125c905316adb728bf80d6R209-R212) [[3]](diffhunk://#diff-d37aa14c07863cc184800713b88a9c25fdb43efd1a4daa31df3ecabb2f70f177R16) --------- Co-authored-by: Mason Daugherty <github@mdrxy.com> Co-authored-by: Mason Daugherty <mason@langchain.dev>
1 parent 3490470 commit 491eb9d

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

libs/langchain/langchain_classic/chat_models/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,10 @@ def _init_chat_model_helper(
392392

393393
return AzureChatOpenAI(model=model, **kwargs)
394394
if model_provider == "azure_ai":
395-
_check_pkg("langchain_azure_ai", "AzureAIChatCompletionsModel")
396-
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
395+
_check_pkg("langchain_azure_ai", "AzureAIOpenAIApiChatModel")
396+
from langchain_azure_ai.chat_models import AzureAIOpenAIApiChatModel
397397

398-
return AzureAIChatCompletionsModel(model=model, **kwargs)
398+
return AzureAIOpenAIApiChatModel(model=model, **kwargs)
399399
if model_provider == "cohere":
400400
_check_pkg("langchain_cohere", "ChatCohere")
401401
from langchain_cohere import ChatCohere

libs/langchain/langchain_classic/embeddings/base.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from langchain_core.runnables import Runnable
77

88
_SUPPORTED_PROVIDERS = {
9+
"azure_ai": "langchain_azure_ai",
910
"azure_openai": "langchain_openai",
1011
"bedrock": "langchain_aws",
1112
"cohere": "langchain_cohere",
@@ -118,9 +119,10 @@ def _infer_model_and_provider(
118119
def _check_pkg(pkg: str) -> None:
119120
"""Check if a package is installed."""
120121
if not util.find_spec(pkg):
122+
pip_name = pkg.replace("_", "-")
121123
msg = (
122124
f"Could not import {pkg} python package. "
123-
f"Please install it with `pip install {pkg}`"
125+
f"Please install it with `pip install {pip_name}`"
124126
)
125127
raise ImportError(msg)
126128

@@ -153,6 +155,7 @@ def init_embeddings(
153155
Supported providers:
154156
155157
- `openai` -> [`langchain-openai`](https://docs.langchain.com/oss/python/integrations/providers/openai)
158+
- `azure_ai` -> [`langchain-azure-ai`](https://docs.langchain.com/oss/python/integrations/providers/microsoft)
156159
- `azure_openai` -> [`langchain-openai`](https://docs.langchain.com/oss/python/integrations/providers/openai)
157160
- `bedrock` -> [`langchain-aws`](https://docs.langchain.com/oss/python/integrations/providers/aws)
158161
- `cohere` -> [`langchain-cohere`](https://docs.langchain.com/oss/python/integrations/providers/cohere)
@@ -201,14 +204,22 @@ def init_embeddings(
201204
pkg = _SUPPORTED_PROVIDERS[provider]
202205
_check_pkg(pkg)
203206

204-
if provider == "openai":
205-
from langchain_openai import OpenAIEmbeddings
207+
if provider == "azure_ai":
208+
from langchain_azure_ai.embeddings import AzureAIOpenAIApiEmbeddingsModel
206209

207-
return OpenAIEmbeddings(model=model_name, **kwargs)
210+
return AzureAIOpenAIApiEmbeddingsModel(model=model_name, **kwargs)
208211
if provider == "azure_openai":
209212
from langchain_openai import AzureOpenAIEmbeddings
210213

211214
return AzureOpenAIEmbeddings(model=model_name, **kwargs)
215+
if provider == "openai":
216+
from langchain_openai import OpenAIEmbeddings
217+
218+
return OpenAIEmbeddings(model=model_name, **kwargs)
219+
if provider == "bedrock":
220+
from langchain_aws import BedrockEmbeddings
221+
222+
return BedrockEmbeddings(model_id=model_name, **kwargs)
212223
if provider == "google_genai":
213224
from langchain_google_genai import GoogleGenerativeAIEmbeddings
214225

@@ -217,10 +228,6 @@ def init_embeddings(
217228
from langchain_google_vertexai import VertexAIEmbeddings
218229

219230
return VertexAIEmbeddings(model=model_name, **kwargs)
220-
if provider == "bedrock":
221-
from langchain_aws import BedrockEmbeddings
222-
223-
return BedrockEmbeddings(model_id=model_name, **kwargs)
224231
if provider == "cohere":
225232
from langchain_cohere import CohereEmbeddings
226233

libs/langchain_v1/langchain/chat_models/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _call(cls: type[BaseChatModel], **kwargs: Any) -> BaseChatModel:
3838
_BUILTIN_PROVIDERS: dict[str, tuple[str, str, Callable[..., BaseChatModel]]] = {
3939
"anthropic": ("langchain_anthropic", "ChatAnthropic", _call),
4040
"anthropic_bedrock": ("langchain_aws", "ChatAnthropicBedrock", _call),
41-
"azure_ai": ("langchain_azure_ai.chat_models", "AzureAIChatCompletionsModel", _call),
41+
"azure_ai": ("langchain_azure_ai.chat_models", "AzureAIOpenAIApiChatModel", _call),
4242
"azure_openai": ("langchain_openai", "AzureChatOpenAI", _call),
4343
"baseten": ("langchain_baseten", "ChatBaseten", _call),
4444
"bedrock": ("langchain_aws", "ChatBedrock", _call),

libs/langchain_v1/langchain/embeddings/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def _call(cls: type[Embeddings], **kwargs: Any) -> Embeddings:
1313

1414

1515
_BUILTIN_PROVIDERS: dict[str, tuple[str, str, Callable[..., Embeddings]]] = {
16+
"azure_ai": ("langchain_azure_ai.embeddings", "AzureAIOpenAIApiEmbeddingsModel", _call),
1617
"azure_openai": ("langchain_openai", "AzureOpenAIEmbeddings", _call),
1718
"bedrock": (
1819
"langchain_aws",
@@ -84,7 +85,7 @@ def _get_embeddings_class_creator(provider: str) -> Callable[..., Embeddings]:
8485
try:
8586
module = importlib.import_module(module_name)
8687
except ImportError as e:
87-
pkg = module_name.replace("_", "-")
88+
pkg = module_name.split(".", maxsplit=1)[0].replace("_", "-")
8889
msg = f"Could not import {pkg} python package. Please install it with `pip install {pkg}`"
8990
raise ImportError(msg) from e
9091

@@ -217,6 +218,7 @@ def init_embeddings(
217218
are:
218219
219220
- `openai` -> [`langchain-openai`](https://docs.langchain.com/oss/python/integrations/providers/openai)
221+
- `azure_ai` -> [`langchain-azure-ai`](https://docs.langchain.com/oss/python/integrations/providers/microsoft)
220222
- `azure_openai` -> [`langchain-openai`](https://docs.langchain.com/oss/python/integrations/providers/openai)
221223
- `bedrock` -> [`langchain-aws`](https://docs.langchain.com/oss/python/integrations/providers/aws)
222224
- `cohere` -> [`langchain-cohere`](https://docs.langchain.com/oss/python/integrations/providers/cohere)

0 commit comments

Comments
 (0)