# AzureChatOpenAI

> **Class** in `langchain_openai`

📖 [View in docs](https://reference.langchain.com/python/langchain-openai/chat_models/azure/AzureChatOpenAI)

Azure OpenAI chat model integration.

## Signature

```python
AzureChatOpenAI()
```

## Description

**Setup:**

Head to the Azure [OpenAI quickstart guide](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/chatgpt-quickstart?tabs=keyless%2Ctypescript-keyless%2Cpython-new%2Ccommand-line&pivots=programming-language-python)
to create your Azure OpenAI deployment.

Then install `langchain-openai` and set environment variables
`AZURE_OPENAI_API_KEY` and `AZURE_OPENAI_ENDPOINT`:

```bash
pip install -U langchain-openai

export AZURE_OPENAI_API_KEY="your-api-key"
export AZURE_OPENAI_ENDPOINT="https://your-endpoint.openai.azure.com/"
```

Key init args — completion params:
    azure_deployment:
        Name of Azure OpenAI deployment to use.
    temperature:
        Sampling temperature.
    max_tokens:
        Max number of tokens to generate.
    logprobs:
        Whether to return logprobs.

Key init args — client params:
    api_version:
        Azure OpenAI REST API version to use (distinct from the version of the
        underlying model). [See more on the different versions.](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning)
    timeout:
        Timeout for requests.
    max_retries:
        Max number of retries.
    organization:
        OpenAI organization ID. If not passed in will be read from env
        var `OPENAI_ORG_ID`.
    model:
        The name of the underlying OpenAI model. Used for tracing and token
        counting. Does not affect completion. E.g. `'gpt-4'`, `'gpt-35-turbo'`, etc.
    model_version:
        The version of the underlying OpenAI model. Used for tracing and token
        counting. Does not affect completion. E.g., `'0125'`, `'0125-preview'`, etc.

See full list of supported init args and their descriptions in the params section.

**Instantiate:**

```python
from langchain_openai import AzureChatOpenAI

model = AzureChatOpenAI(
    azure_deployment="your-deployment",
    api_version="2024-05-01-preview",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    # organization="...",
    # model="gpt-35-turbo",
    # model_version="0125",
    # other params...
)
```

!!! note
Any param which is not explicitly supported will be passed directly to the
`openai.AzureOpenAI.chat.completions.create(...)` API every time to the model is
invoked.

For example:

```python
from langchain_openai import AzureChatOpenAI
import openai

AzureChatOpenAI(..., logprobs=True).invoke(...)

# results in underlying API call of:

openai.AzureOpenAI(..).chat.completions.create(..., logprobs=True)

# which is also equivalent to:

AzureChatOpenAI(...).invoke(..., logprobs=True)
```

**Invoke:**

```python
messages = [
    (
        "system",
        "You are a helpful translator. Translate the user sentence to French.",
    ),
    ("human", "I love programming."),
]
model.invoke(messages)
```

```python
AIMessage(
    content="J'adore programmer.",
    usage_metadata={
        "input_tokens": 28,
        "output_tokens": 6,
        "total_tokens": 34,
    },
    response_metadata={
        "token_usage": {
            "completion_tokens": 6,
            "prompt_tokens": 28,
            "total_tokens": 34,
        },
        "model_name": "gpt-4",
        "system_fingerprint": "fp_7ec89fabc6",
        "prompt_filter_results": [
            {
                "prompt_index": 0,
                "content_filter_results": {
                    "hate": {"filtered": False, "severity": "safe"},
                    "self_harm": {"filtered": False, "severity": "safe"},
                    "sexual": {"filtered": False, "severity": "safe"},
                    "violence": {"filtered": False, "severity": "safe"},
                },
            }
        ],
        "finish_reason": "stop",
        "logprobs": None,
        "content_filter_results": {
            "hate": {"filtered": False, "severity": "safe"},
            "self_harm": {"filtered": False, "severity": "safe"},
            "sexual": {"filtered": False, "severity": "safe"},
            "violence": {"filtered": False, "severity": "safe"},
        },
    },
    id="run-6d7a5282-0de0-4f27-9cc0-82a9db9a3ce9-0",
)
```

**Stream:**

```python
for chunk in model.stream(messages):
    print(chunk.text, end="")
```

```python
AIMessageChunk(content="", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(content="J", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(content="'", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(content="ad", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(content="ore", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(content=" la", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(
    content=" programm", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f"
)
AIMessageChunk(content="ation", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(content=".", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
AIMessageChunk(
    content="",
    response_metadata={
        "finish_reason": "stop",
        "model_name": "gpt-4",
        "system_fingerprint": "fp_811936bd4f",
    },
    id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f",
)
```

```python
stream = model.stream(messages)
full = next(stream)
for chunk in stream:
    full += chunk
full
```

```python
AIMessageChunk(
    content="J'adore la programmation.",
    response_metadata={
        "finish_reason": "stop",
        "model_name": "gpt-4",
        "system_fingerprint": "fp_811936bd4f",
    },
    id="run-ba60e41c-9258-44b8-8f3a-2f10599643b3",
)
```

**Async:**

```python
await model.ainvoke(messages)

# stream:
# async for chunk in (await model.astream(messages))

# batch:
# await model.abatch([messages])
```

**Tool calling:**

```python
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"
    )

class GetPopulation(BaseModel):
    '''Get the current population in a given location'''

    location: str = Field(
        ..., description="The city and state, e.g. San Francisco, CA"
    )

model_with_tools = model.bind_tools([GetWeather, GetPopulation])
ai_msg = model_with_tools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?"
)
ai_msg.tool_calls
```

```python
[
    {
        "name": "GetWeather",
        "args": {"location": "Los Angeles, CA"},
        "id": "call_6XswGD5Pqk8Tt5atYr7tfenU",
    },
    {
        "name": "GetWeather",
        "args": {"location": "New York, NY"},
        "id": "call_ZVL15vA8Y7kXqOy3dtmQgeCi",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "Los Angeles, CA"},
        "id": "call_49CFW8zqC9W7mh7hbMLSIrXw",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "New York, NY"},
        "id": "call_6ghfKxV264jEfe1mRIkS3PE7",
    },
]
```

**Structured output:**

```python
from typing import Optional

from pydantic import BaseModel, Field

class Joke(BaseModel):
    '''Joke to tell user.'''

    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: int | None = Field(
        description="How funny the joke is, from 1 to 10"
    )

structured_model = model.with_structured_output(Joke)
structured_model.invoke("Tell me a joke about cats")
```

```python
Joke(
    setup="Why was the cat sitting on the computer?",
    punchline="To keep an eye on the mouse!",
    rating=None,
)
```

See `AzureChatOpenAI.with_structured_output()` for more.

**JSON mode:**

```python
json_model = model.bind(response_format={"type": "json_object"})
ai_msg = json_model.invoke(
    "Return a JSON object with key 'random_ints' and a value of 10 random ints in [0-99]"
)
ai_msg.content
```

```python
'\\n{\\n  "random_ints": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]\\n}'
```

**Image input:**

```python
import base64
import httpx
from langchain_core.messages import HumanMessage

image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ]
)
ai_msg = model.invoke([message])
ai_msg.content
```

```python
"The weather in the image appears to be quite pleasant. The sky is mostly clear"
```

**Token usage:**

```python
ai_msg = model.invoke(messages)
ai_msg.usage_metadata
```

```python
{"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}
```

Logprobs:
    ```python
    logprobs_model = model.bind(logprobs=True)
    ai_msg = logprobs_model.invoke(messages)
    ai_msg.response_metadata["logprobs"]
    ```

    ```python
    {
        "content": [
            {
                "token": "J",
                "bytes": [74],
                "logprob": -4.9617593e-06,
                "top_logprobs": [],
            },
            {
                "token": "'adore",
                "bytes": [39, 97, 100, 111, 114, 101],
                "logprob": -0.25202933,
                "top_logprobs": [],
            },
            {
                "token": " la",
                "bytes": [32, 108, 97],
                "logprob": -0.20141791,
                "top_logprobs": [],
            },
            {
                "token": " programmation",
                "bytes": [
                    32,
                    112,
                    114,
                    111,
                    103,
                    114,
                    97,
                    109,
                    109,
                    97,
                    116,
                    105,
                    111,
                    110,
                ],
                "logprob": -1.9361265e-07,
                "top_logprobs": [],
            },
            {
                "token": ".",
                "bytes": [46],
                "logprob": -1.2233183e-05,
                "top_logprobs": [],
            },
        ]
    }
    ```

Response metadata
    ```python
    ai_msg = model.invoke(messages)
    ai_msg.response_metadata
    ```

    ```python
    {
        "token_usage": {
            "completion_tokens": 6,
            "prompt_tokens": 28,
            "total_tokens": 34,
        },
        "model_name": "gpt-35-turbo",
        "system_fingerprint": None,
        "prompt_filter_results": [
            {
                "prompt_index": 0,
                "content_filter_results": {
                    "hate": {"filtered": False, "severity": "safe"},
                    "self_harm": {"filtered": False, "severity": "safe"},
                    "sexual": {"filtered": False, "severity": "safe"},
                    "violence": {"filtered": False, "severity": "safe"},
                },
            }
        ],
        "finish_reason": "stop",
        "logprobs": None,
        "content_filter_results": {
            "hate": {"filtered": False, "severity": "safe"},
            "self_harm": {"filtered": False, "severity": "safe"},
            "sexual": {"filtered": False, "severity": "safe"},
            "violence": {"filtered": False, "severity": "safe"},
        },
    }
    ```

## Extends

- `BaseChatOpenAI`

## Properties

- `azure_endpoint`
- `deployment_name`
- `openai_api_version`
- `openai_api_key`
- `azure_ad_token`
- `azure_ad_token_provider`
- `azure_ad_async_token_provider`
- `model_version`
- `openai_api_type`
- `validate_base_url`
- `model_name`
- `disabled_params`
- `max_tokens`
- `lc_secrets`
- `lc_attributes`

## Methods

- [`get_lc_namespace()`](https://reference.langchain.com/python/langchain-openai/chat_models/azure/AzureChatOpenAI/get_lc_namespace)
- [`is_lc_serializable()`](https://reference.langchain.com/python/langchain-openai/chat_models/azure/AzureChatOpenAI/is_lc_serializable)
- [`validate_environment()`](https://reference.langchain.com/python/langchain-openai/chat_models/azure/AzureChatOpenAI/validate_environment)
- [`with_structured_output()`](https://reference.langchain.com/python/langchain-openai/chat_models/azure/AzureChatOpenAI/with_structured_output)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/6fb37dba71da807af60aa7b909f71f0625a666bf/libs/partners/openai/langchain_openai/chat_models/azure.py#L37)