Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain AzureOpenAI LLM

From Leeroopedia
Revision as of 11:23, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Langchain_ai_Langchain_AzureOpenAI_LLM.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains LLM, Azure OpenAI, Text Completion
Last Updated 2026-02-11 00:00 GMT

Overview

AzureOpenAI is a LangChain LLM class for text completion using Azure-hosted OpenAI models, extending BaseOpenAI with Azure-specific endpoint and authentication configuration.

Description

The AzureOpenAI class extends BaseOpenAI from the langchain-openai package to provide Azure-specific LLM text completion capabilities. This is distinct from Azure OpenAI chat models -- it targets the completions API (e.g., gpt-3.5-turbo-instruct). The class supports Azure endpoints, deployment names, API versioning, and multiple authentication methods including API keys, Azure AD tokens, and AD token providers (both sync and async). It creates openai.AzureOpenAI and openai.AsyncAzureOpenAI client instances pointed at the completions endpoint. It includes backward-compatibility validation for the transition from openai_api_base to azure_endpoint. The class is LangChain-serializable and integrates with LangSmith tracing.

Usage

Import this class when you need text completion (not chat completion) from Azure-hosted OpenAI models in LangChain chains, agents, or direct generation workflows.

Code Reference

Source Location

Signature

class AzureOpenAI(BaseOpenAI):
    azure_endpoint: str | None = Field(...)
    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
    openai_api_type: str | None = Field(...)
    validate_base_url: bool = True

Import

from langchain_openai import AzureOpenAI

I/O Contract

Inputs

Name Type Required Description
azure_endpoint None No Azure endpoint URL (e.g., https://example-resource.azure.openai.com/). Read from AZURE_OPENAI_ENDPOINT env var.
deployment_name None No Azure model deployment name. Alias: azure_deployment.
openai_api_version None No API version. Read from OPENAI_API_VERSION env var.
openai_api_key None No API key. Read from AZURE_OPENAI_API_KEY or OPENAI_API_KEY env vars.
azure_ad_token None No Azure AD token. Read from AZURE_OPENAI_AD_TOKEN env var.
azure_ad_token_provider Callable[[], str] | None No Function returning an Azure AD token, invoked on every sync request.
azure_ad_async_token_provider Callable[[], Awaitable[str]] | None No Async function returning an Azure AD token, invoked on every async request.
openai_api_type None No API type. Read from OPENAI_API_TYPE env var. Default: "azure".
validate_base_url bool No Whether to validate and transform legacy base URL. Default: True.
model_name str No Model name (inherited from BaseOpenAI).

Outputs

Name Type Description
_call return str Generated completion text (inherited from BaseOpenAI).
generate return LLMResult Structured generation results (inherited from BaseOpenAI).

Properties

Property Return Type Description
_llm_type str Returns "azure".
_identifying_params Mapping[str, Any] Returns deployment_name plus parent identifying params.
_invocation_params dict[str, Any] Includes model set to deployment_name plus parent invocation params.
lc_attributes dict[str, Any] Returns openai_api_type and openai_api_version for tracing.
lc_secrets dict[str, str] Maps openai_api_key to AZURE_OPENAI_API_KEY and azure_ad_token to AZURE_OPENAI_AD_TOKEN.

Class Methods

Method Return Type Description
get_lc_namespace() list[str] Returns ["langchain", "llms", "openai"].
is_lc_serializable() bool Returns True, indicating this model can be serialized by LangChain.

Usage Examples

Basic Usage

from langchain_openai import AzureOpenAI

llm = AzureOpenAI(
    model_name="gpt-3.5-turbo-instruct",
    azure_deployment="my-deployment",
    azure_endpoint="https://my-resource.openai.azure.com/",
    # api_key="your-api-key",  # Or set AZURE_OPENAI_API_KEY env var
    # api_version="2024-02-01",  # Or set OPENAI_API_VERSION env var
)

# Generate text
response = llm.invoke("Tell me a joke about programming.")
print(response)

With Azure AD Authentication

from langchain_openai import AzureOpenAI
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

llm = AzureOpenAI(
    model_name="gpt-3.5-turbo-instruct",
    azure_deployment="my-deployment",
    azure_endpoint="https://my-resource.openai.azure.com/",
    azure_ad_token_provider=lambda: credential.get_token(
        "https://cognitiveservices.azure.com/.default"
    ).token,
)

Related Pages

  • Requires langchain-openai and openai packages
  • Extends langchain_openai.llms.base.BaseOpenAI

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment