Implementation:Langchain ai Langchain AzureChatOpenAI
| Knowledge Sources | |
|---|---|
| Domains | LLM, Chat Model, Azure, OpenAI |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
AzureChatOpenAI is a LangChain chat model integration for Azure-hosted OpenAI deployments, providing chat completions via the Azure OpenAI API.
Description
The AzureChatOpenAI class, defined in the langchain-openai partner package, extends BaseChatOpenAI. It wraps the openai.AzureOpenAI and openai.AsyncAzureOpenAI clients to provide chat completions through Azure OpenAI Service deployments. The class handles Azure-specific configuration including deployment names, API versioning, Azure Active Directory (AAD) authentication (both sync and async token providers), Azure endpoint configuration, and content filter result processing. It supports tool calling, structured output (via JSON schema, function calling, and JSON mode), streaming (with routing between Chat Completions and Responses APIs), and Azure-specific features like content filter metadata and prompt filter results. The class also supports model versioning for accurate cost tracking and configurable parameter disabling for compatibility with different Azure-deployed models.
Usage
Import this class when you need to use OpenAI models deployed on Azure OpenAI Service, particularly when Azure AD authentication, content filtering, or Azure-specific deployment configurations are required.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/openai/langchain_openai/chat_models/azure.py - Lines: 1-1164
Signature
class AzureChatOpenAI(BaseChatOpenAI):
azure_endpoint: str | None = Field(
default_factory=from_env("AZURE_OPENAI_ENDPOINT", default=None)
)
deployment_name: str | None = Field(default=None, alias="azure_deployment")
openai_api_version: str | None = Field(alias="api_version", ...)
openai_api_key: SecretStr | None = Field(alias="api_key", ...)
azure_ad_token: SecretStr | None = Field(...)
azure_ad_token_provider: Callable[[], str] | None = None
azure_ad_async_token_provider: Callable[[], Awaitable[str]] | None = None
model_version: str = ""
openai_api_type: str | None = Field(default_factory=from_env("OPENAI_API_TYPE", default="azure"))
validate_base_url: bool = True
model_name: str | None = Field(default=None, alias="model")
disabled_params: dict[str, Any] | None = Field(default=None)
max_tokens: int | None = Field(default=None, alias="max_completion_tokens")
Import
from langchain_openai import AzureChatOpenAI
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| azure_endpoint | str or None | No | Azure OpenAI endpoint URL. Inferred from AZURE_OPENAI_ENDPOINT env var.
|
| deployment_name | str or None | No | Azure deployment name. Alias: azure_deployment.
|
| openai_api_version | str or None | No | Azure OpenAI REST API version. Alias: api_version. Inferred from OPENAI_API_VERSION env var.
|
| openai_api_key | SecretStr or None | No | API key. Inferred from AZURE_OPENAI_API_KEY or OPENAI_API_KEY env vars. Alias: api_key.
|
| azure_ad_token | SecretStr or None | No | Azure Active Directory token. Inferred from AZURE_OPENAI_AD_TOKEN env var.
|
| azure_ad_token_provider | Callable or None | No | Function returning an Azure AD token. Invoked on every sync request. |
| azure_ad_async_token_provider | Callable or None | No | Async function returning an Azure AD token. Invoked on every async request. |
| model_name | str or None | No | Name of the underlying OpenAI model (e.g. "gpt-4o"). Used for tracing, not for routing. Alias: model.
|
| model_version | str | No | Model version string appended for cost calculation. Defaults to "".
|
| max_tokens | int or None | No | Maximum tokens to generate. Alias: max_completion_tokens.
|
| disabled_params | dict or None | No | Parameters to disable for the given model deployment (e.g. {"parallel_tool_calls": None}).
|
Outputs
| Name | Type | Description |
|---|---|---|
| ChatResult | ChatResult | Contains ChatGeneration objects with AI messages, content filter results, and prompt filter results in metadata.
|
| ChatGenerationChunk | Iterator[ChatGenerationChunk] | When streaming, yields message chunks with content and metadata. |
Key Methods
with_structured_output
Returns a Runnable that produces structured output. Supports three methods:
"json_schema"(default) -- uses OpenAI's Structured Output API"function_calling"-- uses OpenAI's tool-calling API"json_mode"-- uses OpenAI's JSON mode
Supports a strict parameter to guarantee schema-exact output.
_create_chat_result
Overrides the parent method to add Azure-specific metadata including content filter results, prompt filter results, and model version appending.
Usage Examples
Basic Usage
from langchain_openai import AzureChatOpenAI
model = AzureChatOpenAI(
azure_deployment="my-gpt-4o-deployment",
api_version="2024-05-01-preview",
temperature=0,
max_tokens=None,
)
messages = [
("system", "You are a helpful translator. Translate to French."),
("human", "I love programming."),
]
response = model.invoke(messages)
print(response.content)
Tool Calling
from langchain_openai import AzureChatOpenAI
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
"""Get the current weather in a given location."""
location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
model = AzureChatOpenAI(
azure_deployment="my-gpt-4o-deployment",
model="gpt-4o",
temperature=0,
)
model_with_tools = model.bind_tools([GetWeather])
ai_msg = model_with_tools.invoke("What is the weather in Paris?")
print(ai_msg.tool_calls)
Structured Output
from langchain_openai import AzureChatOpenAI
from pydantic import BaseModel
class Joke(BaseModel):
"""Joke to tell user."""
setup: str
punchline: str
model = AzureChatOpenAI(
azure_deployment="my-gpt-4o-deployment",
model="gpt-4o",
temperature=0,
)
structured_model = model.with_structured_output(Joke)
result = structured_model.invoke("Tell me a joke about cats")
print(result)