langchain-azure-ai to build LangChain apps that call models deployed
in Microsoft Foundry. Models with OpenAI-compatible APIs can be directly
used. In this article, you create
chat and embeddings clients, run prompt chains, and combine generation with
verification workflows.
Prerequisites
- An Azure subscription. Create one for free.
- A Foundry project.
- The Foundry User role on the Foundry project.
The Foundry RBAC roles were recently renamed. Foundry User, Foundry Owner, Foundry Account Owner, and Foundry Project Manager were previously named Azure AI User, Azure AI Owner, Azure AI Account Owner, and Azure AI Project Manager. You might still see the previous names in some places while the rename rolls out. The role IDs and core permissions are unchanged by the rename.
- A deployed chat model that supports OpenAI-compatible APIs, such as
gpt-4.1orMistral-Large-3. - A deployed embeddings model, such as
text-embedding-3-large. - Python 3.9 or later.
langchain-azure-ai uses the new Microsoft Foundry SDK (v2). If you’re using Foundry classic, use langchain-azure-ai[v1],
which uses Azure AI Inference SDK (legacy). Learn more.Configure the environment
Set one of the following connection patterns:- Project endpoint with Microsoft Entra ID (recommended).
- Direct endpoint with an API key.
langchain-azure-ai model classes for project-based or direct endpoint access.
Use chat models
You can easily instantiate a model by usinginit_chat_model:
Using
init_chat_model requires langchain>=1.2.13. If you can’t update your version, configure clients directly.project_endpoint (environment variable FOUNDRY_PROJECT_ENDPOINT) requires Microsoft Entra ID for authentication and the role Foundry User.
What this snippet does: Creates a chat model client by using the
init_chat_model convenience method. The client routes to the specified model
through the Foundry project endpoint or direct endpoint configured in the environment.
References:
Verify your setup
Run a simple model invocation:Configurable models
You can also create a runtime-configurable model by specifyingconfigurable_fields. When you omit the model parameter, it becomes a configurable field by default.
model parameter is missing in init_chat_model,
it’s by default a configurable field and can be passed with invoke(). You can add other
fields to be configurable by configuring configurable_fields.
Configure clients directly
You can also create a chat model client by usingAzureAIOpenAIApiChatModel class.
AzureAIOpenAIApiChatModel uses the OpenAI Responses API. You can change this behavior by passing use_responses_api=False:
Run asynchronous calls
Use asynchronous credentials if your app calls models withainvoke. When using Microsoft Entra ID for authentication, use
the corresponding asynchronous implementation for credentials:
ainvoke.
References:
Reasoning
Many models can perform multi-step reasoning to arrive at a conclusion. This involves breaking down complex problems into smaller, more manageable steps.Server-side tools
OpenAI models deployed in Foundry support server-side tool-calling loops: models can interact with web search, code interpreters, and other tools, and then analyze the results in a single conversational turn. If a model invokes a tool server-side, the content of the response message will include content representing the invocation and result of the tool.Tools in the namespace
langchain_azure_ai.tools.builtin are only supported in OpenAI models.azure-ai-projects to configure those resources and then reference them from LangChain/LangGraph.
The following example shows how to configure a file store before using it in a tool:
FileSearchTool in the next code block).
Use Foundry models in agents
Usecreate_agent with models connected to Foundry to create ReAct-style agent loops:
bind_tools.
Use embedding models
You can easily instantiate a model by usinginit_embeddings:
init_embeddings convenience method.
All Foundry models supporting OpenAI-compatible APIs can be used with the client, but you need to deploy them to your Foundry resource first. Using project_endpoint (environment variable FOUNDRY_PROJECT_ENDPOINT) requires Microsoft Entra ID for authentication and the role Foundry User.
Or create the embeddings client with AzureAIOpenAIApiEmbeddingsModel.
Example: Run similarity search with a vector store
Use an in-memory vector store for local experimentation.Debug requests with logging
Enablelangchain_azure_ai debug logging to inspect request flow.
Environment variables reference
You can configure the following environment variables. These values can also be configured when constructing the objects:Environment variables
AZURE_AI_INFERENCE_ENDPOINT and AZURE_AI_CREDENTIALS used for AzureAIChatCompletionsModel or AzureAIEmbeddingsModel (legacy) are no longer used.