Chat completions model for Azure AI.
Azure AI chat model using the OpenAI-compatible API.
This class wraps :class:langchain_openai.ChatOpenAI and adds support
for the project-endpoint pattern available in Azure AI Foundry, in addition
to the classic endpoint + API-key style used by OpenAI-compatible services.
Use AzureAIOpenAIApiChatModel with any Foundry model compatible with
OpenAI APIs (e.g. gpt-5, Mistral, Cohere.) to get the benefits of
unified authentication, single configuration, and seamless integration
with other Azure services.
By default, this class uses Responses API. Set use_responses_api=False
to disable it and use the standard chat completions API instead.
Project-endpoint pattern (recommended for Azure AI Foundry):
from langchain_azure_ai.chat_models import AzureAIOpenAIApiChatModel
from azure.identity import DefaultAzureCredential
model = AzureAIOpenAIApiChatModel(
project_endpoint=(
"https://resource.services.ai.azure.com/api/projects/my-project"
),
credential=DefaultAzureCredential(),
model="gpt-4o",
)
Parameter model refers to the model deployment name in Azure AI Foundry,
which may differ from the base model name (e.g. "gpt-4o") depending on how
the deployment was configured.
If project_endpoint is omitted the value of the
AZURE_AI_PROJECT_ENDPOINT environment variable is used.
Direct endpoint + API-key pattern:
from langchain_azure_ai.chat_models import AzureAIOpenAIApiChatModel
model = AzureAIOpenAIApiChatModel(
endpoint="https://resource.services.ai.azure.com/openai/v1",
credential="your-api-key",
model="gpt-4o",
)
Use api_version parameter to specify the API version when using a
direct endpoint, or rely on the automatic API version detection
when using a project endpoint.
from langchain_azure_ai.chat_models import AzureAIOpenAIApiChatModel
model = AzureAIOpenAIApiChatModel(
endpoint="https://resource.services.ai.azure.com/openai/v1",
credential="your-api-key",
model="gpt-4o",
api_version="2025-05-12"
)
Environment variables:
The following environment variables are recognised as fallbacks when the corresponding constructor parameters are not provided:
AZURE_AI_PROJECT_ENDPOINT – used as project_endpoint.AZURE_AI_OPENAI_ENDPOINT – direct OpenAI-compatible endpoint
(e.g. https://<resource>.services.ai.azure.com/openai/v1).
Used as endpoint verbatim (no path is appended).AZURE_OPENAI_ENDPOINT – root Azure OpenAI endpoint (e.g.
https://<resource>.services.ai.azure.com). /openai/v1 is
appended automatically and the result is treated as endpoint.AZURE_OPENAI_DEPLOYMENT_NAME – model deployment name (model).AZURE_OPENAI_API_VERSION – API version passed as the
api-version query parameter on every request.Resolution priority (highest → lowest):
AZURE_AI_PROJECT_ENDPOINT environment variable.AZURE_AI_OPENAI_ENDPOINT environment variable.AZURE_OPENAI_ENDPOINT / AZURE_OPENAI_API_VERSION /
AZURE_OPENAI_DEPLOYMENT_NAME environment variables.AZURE_AI_PROJECT_ENDPOINT, AZURE_AI_OPENAI_ENDPOINT, and
AZURE_OPENAI_ENDPOINT may all be set at the same time; the project
endpoint takes precedence, then AZURE_AI_OPENAI_ENDPOINT, then
AZURE_OPENAI_ENDPOINT. However, passing both project_endpoint
and endpoint as constructor parameters raises :class:ValueError.
All other keyword arguments accepted by
:class:langchain_openai.ChatOpenAI are forwarded as-is, so you
retain full control over temperature, max_tokens, streaming, etc.
Azure AI chat model using the Anthropic Messages API.
Wraps :class:langchain_anthropic.ChatAnthropic so that Anthropic
Claude models hosted by Azure AI Foundry can be accessed through the
/anthropic endpoint with native Microsoft Entra ID authentication.
Authentication uses :class:anthropic.AnthropicFoundry (and
:class:anthropic.AsyncAnthropicFoundry) under the hood, which accepts
an azure_ad_token_provider callable that is invoked on every
request – so bearer tokens are refreshed automatically and the model
can be used in long-running services without manual token rotation.
Microsoft Entra ID authentication (recommended):
from langchain_azure_ai.chat_models import AzureAIAnthropicChatModel
from azure.identity import DefaultAzureCredential
model = AzureAIAnthropicChatModel(
endpoint="https://<resource>.services.ai.azure.com",
credential=DefaultAzureCredential(),
model="claude-sonnet-4-20250514",
)
The endpoint is the Azure AI Foundry resource root; the
/anthropic path is appended automatically. You may also pass a URL
that already ends in /anthropic – the result is the same.
Alternatively, supply a Foundry project endpoint and the resource root is derived automatically:
model = AzureAIAnthropicChatModel(
project_endpoint=(
"https://<resource>.services.ai.azure.com/api/projects/my-project"
),
credential=DefaultAzureCredential(),
model="claude-sonnet-4-20250514",
)
project_endpoint and endpoint are mutually exclusive.
API-key authentication:
model = AzureAIAnthropicChatModel(
endpoint="https://<resource>.services.ai.azure.com",
credential="your-api-key",
model="claude-sonnet-4-20250514",
)
Environment variables:
The following environment variables are checked when the corresponding constructor parameters are not provided:
AZURE_AI_PROJECT_ENDPOINT or FOUNDRY_PROJECT_ENDPOINT – resolved
as project_endpoint. AZURE_AI_PROJECT_ENDPOINT takes precedence
when both are set.ANTHROPIC_FOUNDRY_RESOURCE – resolved as endpoint when neither
endpoint nor project_endpoint is set (directly or via env var).
May be a bare resource name (e.g. my-resource) or a full URL. When
a bare name is provided the endpoint is synthesized as
https://<ANTHROPIC_FOUNDRY_RESOURCE>.services.ai.azure.com.
This is the same variable used by Claude Code when connected to
Azure AI Foundry, so models configured that way will work here without
any additional configuration.Resolution priority (highest to lowest):
project_endpoint, endpoint).AZURE_AI_PROJECT_ENDPOINT environment variable (or
FOUNDRY_PROJECT_ENDPOINT if the former is not set).ANTHROPIC_FOUNDRY_RESOURCE environment variable.All other keyword arguments accepted by
:class:langchain_anthropic.ChatAnthropic are forwarded as-is.
Azure AI Chat Completions Model.
This class has been deprecated in favor of AzureAIOpenAIApiChatModel.
The Azure AI model inference API (https://aka.ms/azureai/modelinference) provides a common layer to talk with most models deployed to Azure AI. This class providers inference for chat completions models supporting it. See documentation for the list of models supporting the API.
Examples:
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.messages import HumanMessage, SystemMessage
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
)
messages = [
SystemMessage(
content="Translate the following from English into Italian"
),
HumanMessage(content="hi!"),
]
model.invoke(messages)
For serverless endpoints running a single model, the model_name parameter
can be omitted:
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.messages import HumanMessage, SystemMessage
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].inference.ai.azure.com",
credential="your-api-key",
)
messages = [
SystemMessage(
content="Translate the following from English into Italian"
),
HumanMessage(content="hi!"),
]
model.invoke(messages)
You can pass additional properties to the underlying model, including
temperature, top_p, presence_penalty, etc.
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
temperature=0.5,
top_p=0.9,
)
Azure OpenAI models require to pass the route `openai/v1`.
```python
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/openai/v1",
model="gpt-4.1",
credential="your-api-key",
)
Structured Output:
To use structured output with Azure AI models, you can use the
with_structured_output method. This method supports the same methods
as the base class, including function_calling, json_mode, and
json_schema.
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.messages import HumanMessage
class Joke(BaseModel):
joke: str
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
).with_structured_output(Joke, method="json_schema")
<!--ADMON:eyJ0eXBlIjoibm90ZSIsInRpdGxlIjoiTm90ZSIsImljb24iOiI8c3ZnIHdpZHRoPVwiMTRcIiBoZWlnaHQ9XCIxNFwiIHZpZXdCb3g9XCIwIDAgMTQgMTRcIiBmaWxsPVwiY3VycmVudENvbG9yXCIgeG1sbnM9XCJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2Z1wiPjxwYXRoIGZpbGwtcnVsZT1cImV2ZW5vZGRcIiBjbGlwLXJ1bGU9XCJldmVub2RkXCIgZD1cIk03IDEuM0MxMC4xNCAxLjMgMTIuNyAzLjg2IDEyLjcgN0MxMi43IDEwLjE0IDEwLjE0IDEyLjcgNyAxMi43QzUuNDg5MDggMTIuNjk3NCA0LjA0MDggMTIuMDk2IDIuOTcyNDEgMTEuMDI3NkMxLjkwNDAzIDkuOTU5MiAxLjMwMjY0IDguNTEwOTIgMS4zIDdDMS4zIDMuODYgMy44NiAxLjMgNyAxLjNaTTcgMEMzLjE0IDAgMCAzLjE0IDAgN0MwIDEwLjg2IDMuMTQgMTQgNyAxNEMxMC44NiAxNCAxNCAxMC44NiAxNCA3QzE0IDMuMTQgMTAuODYgMCA3IDBaTTggM0g2VjhIOFYzWk04IDlINlYxMUg4VjlaXCIvPjwvc3ZnPiJ9-->
Using `method="function_calling"` requires the model to support
function calling and `tool_choice". Use "json_mode" or
"json_schema" for best support.
<!--/ADMON-->
**Troubleshooting:**
To diagnostic issues with the model, you can enable debug logging:
```python
import sys
import logging
from langchain_azure_ai.chat_models import AzureAIChatCompletionsModel
logger = logging.getLogger("azure")
# Set the desired logging level. logging.
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
model = AzureAIChatCompletionsModel(
endpoint="https://[your-service].services.ai.azure.com/models",
credential="your-api-key",
model="mistral-large-2407",
client_kwargs={ "logging_enable": True }
)