> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/BerriAI/litellm/llms.txt
> Use this file to discover all available pages before exploring further.

# Azure OpenAI

> Use OpenAI models deployed on Azure through LiteLLM

## Overview

LiteLLM provides comprehensive support for Azure OpenAI Service, allowing you to use GPT-4, GPT-3.5, embeddings, and more through your Azure deployments.

## Quick Start

<Steps>
  <Step title="Install LiteLLM">
    ```bash theme={null}
    pip install litellm
    ```
  </Step>

  <Step title="Set Azure Credentials">
    ```bash theme={null}
    export AZURE_API_KEY="your-azure-api-key"
    export AZURE_API_BASE="https://your-resource.openai.azure.com"
    export AZURE_API_VERSION="2024-02-15-preview"
    ```
  </Step>

  <Step title="Make Your First Call">
    ```python theme={null}
    from litellm import completion

    response = completion(
        model="azure/gpt-4o",  # Your Azure deployment name
        messages=[{"role": "user", "content": "Hello Azure!"}]
    )
    print(response.choices[0].message.content)
    ```
  </Step>
</Steps>

## Authentication

<Tabs>
  <Tab title="Environment Variables">
    Set Azure credentials via environment variables:

    ```bash theme={null}
    export AZURE_API_KEY="your-api-key"
    export AZURE_API_BASE="https://your-resource.openai.azure.com"
    export AZURE_API_VERSION="2024-02-15-preview"
    ```

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="azure/gpt-4o",  # Your deployment name
        messages=[{"role": "user", "content": "Hello!"}]
    )
    ```
  </Tab>

  <Tab title="Direct Parameters">
    Pass credentials directly:

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="azure/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}],
        api_key="your-api-key",
        api_base="https://your-resource.openai.azure.com",
        api_version="2024-02-15-preview"
    )
    ```
  </Tab>

  <Tab title="Azure Active Directory">
    Use Azure AD authentication:

    ```bash theme={null}
    export AZURE_AD_TOKEN="your-ad-token"
    export AZURE_API_BASE="https://your-resource.openai.azure.com"
    export AZURE_API_VERSION="2024-02-15-preview"
    ```

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="azure/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    ```
  </Tab>

  <Tab title="Managed Identity">
    Use Azure Managed Identity:

    ```bash theme={null}
    export AZURE_USE_MANAGED_IDENTITY="true"
    export AZURE_API_BASE="https://your-resource.openai.azure.com"
    export AZURE_API_VERSION="2024-02-15-preview"
    ```

    ```python theme={null}
    from litellm import completion

    response = completion(
        model="azure/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    ```
  </Tab>
</Tabs>

## Model Naming

Azure uses deployment names, not model names. Format: `azure/{deployment_name}`

```python theme={null}
# If your Azure deployment is named "gpt-4o-deployment"
response = completion(
    model="azure/gpt-4o-deployment",
    messages=[{"role": "user", "content": "Hello!"}]
)

# If your deployment is named "my-gpt-35-turbo"
response = completion(
    model="azure/my-gpt-35-turbo",
    messages=[{"role": "user", "content": "Hello!"}]
)
```

## Common Azure Deployments

<CardGroup cols={2}>
  <Card title="GPT-4o" icon="sparkles">
    ```python theme={null}
    model="azure/gpt-4o"
    ```

    Latest GPT-4o model
  </Card>

  <Card title="GPT-4" icon="brain">
    ```python theme={null}
    model="azure/gpt-4"
    ```

    GPT-4 Turbo
  </Card>

  <Card title="GPT-3.5 Turbo" icon="bolt">
    ```python theme={null}
    model="azure/gpt-35-turbo"
    ```

    Fast and efficient
  </Card>

  <Card title="Embeddings" icon="vector-square">
    ```python theme={null}
    model="azure/text-embedding-ada-002"
    ```

    Text embeddings
  </Card>
</CardGroup>

## API Versions

Azure OpenAI uses API versions. Recommended versions:

| Version              | Features        | Recommended For      |
| -------------------- | --------------- | -------------------- |
| `2024-02-15-preview` | Latest features | Production use       |
| `2024-08-01-preview` | Newest preview  | Testing new features |
| `2023-12-01-preview` | Stable          | Legacy support       |

```python theme={null}
response = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
    api_version="2024-02-15-preview"
)
```

## Streaming

```python theme={null}
from litellm import completion

response = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "Write a story"}],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

## Function Calling

```python theme={null}
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }
}]

response = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Seattle?"}],
    tools=tools
)

if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    print(f"Function: {tool_call.function.name}")
    print(f"Arguments: {tool_call.function.arguments}")
```

## Vision (Multimodal)

Use GPT-4 Vision on Azure:

```python theme={null}
response = completion(
    model="azure/gpt-4o",  # Or your GPT-4-vision deployment
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {
                "type": "image_url",
                "image_url": {"url": "https://example.com/image.jpg"}
            }
        ]
    }]
)
```

## Embeddings

Generate embeddings using Azure:

```python theme={null}
from litellm import embedding

response = embedding(
    model="azure/text-embedding-ada-002",  # Your deployment name
    input="Hello world"
)

print(response.data[0].embedding)
print(f"Dimensions: {len(response.data[0].embedding)}")

# Multiple texts
response = embedding(
    model="azure/text-embedding-ada-002",
    input=["Text 1", "Text 2", "Text 3"]
)
```

### Azure Embedding Models

```python theme={null}
# text-embedding-ada-002
embedding(model="azure/text-embedding-ada-002", input="...")

# text-embedding-3-small
embedding(model="azure/text-embedding-3-small", input="...")

# text-embedding-3-large
embedding(model="azure/text-embedding-3-large", input="...", dimensions=256)
```

## Image Generation (DALL-E)

Generate images using DALL-E on Azure:

```python theme={null}
from litellm import image_generation

response = image_generation(
    model="azure/dall-e-3",
    prompt="A sunset over mountains",
    n=1,
    size="1024x1024",
    quality="standard"  # or "hd"
)

print(response.data[0].url)
```

## Audio Transcription (Whisper)

Transcribe audio using Whisper on Azure:

```python theme={null}
from litellm import transcription

with open("audio.mp3", "rb") as audio_file:
    response = transcription(
        model="azure/whisper",  # Your Whisper deployment
        file=audio_file,
        language="en"
    )

print(response.text)
```

## Text-to-Speech

Generate speech from text:

```python theme={null}
from litellm import speech

response = speech(
    model="azure/tts",  # Your TTS deployment
    input="Hello, this is a test.",
    voice="alloy"  # alloy, echo, fable, onyx, nova, shimmer
)

# Save audio file
with open("output.mp3", "wb") as f:
    f.write(response.content)
```

## Batch Processing

Process requests in batches:

```python theme={null}
from litellm import create_batch, retrieve_batch

# Create batch
batch = create_batch(
    custom_llm_provider="azure",
    input_file_id="file-abc123",
    endpoint="/chat/completions",
    completion_window="24h"
)

print(f"Batch ID: {batch.id}")

# Check status
batch_status = retrieve_batch(
    custom_llm_provider="azure",
    batch_id=batch.id
)
```

## Advanced Features

### JSON Mode

```python theme={null}
response = completion(
    model="azure/gpt-4o",
    messages=[{
        "role": "user",
        "content": "Extract info: John is 30 and lives in NYC"
    }],
    response_format={"type": "json_object"}
)

import json
data = json.loads(response.choices[0].message.content)
```

### Seed for Reproducibility

```python theme={null}
response = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "Tell a joke"}],
    seed=42,
    temperature=0.7
)
```

### Logprobs

```python theme={null}
response = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    logprobs=True,
    top_logprobs=3
)

for token in response.choices[0].logprobs.content:
    print(f"{token.token}: {token.logprob}")
```

## Multiple Azure Deployments

Use different Azure resources:

```python theme={null}
# Resource 1
response1 = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    api_base="https://resource1.openai.azure.com",
    api_key="key1"
)

# Resource 2
response2 = completion(
    model="azure/gpt-35-turbo",
    messages=[{"role": "user", "content": "Hello"}],
    api_base="https://resource2.openai.azure.com",
    api_key="key2"
)
```

## Content Filtering

Azure applies content filtering by default:

```python theme={null}
try:
    response = completion(
        model="azure/gpt-4o",
        messages=[{"role": "user", "content": "..."}]
    )
except Exception as e:
    # Check if content was filtered
    if "content_filter" in str(e).lower():
        print("Content was filtered by Azure")
    raise

# Access content filter results
if hasattr(response.choices[0], 'content_filter_results'):
    print(response.choices[0].content_filter_results)
```

## Error Handling

```python theme={null}
from litellm import completion
from litellm.exceptions import (
    AuthenticationError,
    RateLimitError,
    ContextWindowExceededError,
    APIError
)

try:
    response = completion(
        model="azure/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except AuthenticationError:
    print("Invalid API key or auth")
except RateLimitError:
    print("Rate limit exceeded")
except ContextWindowExceededError:
    print("Input too long")
except APIError as e:
    print(f"Azure API error: {e}")
```

## Cost Tracking

```python theme={null}
from litellm import completion, completion_cost

response = completion(
    model="azure/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Calculate cost
cost = completion_cost(completion_response=response)
print(f"Cost: ${cost:.6f}")

# Token usage
print(f"Prompt tokens: {response.usage.prompt_tokens}")
print(f"Completion tokens: {response.usage.completion_tokens}")
```

## Regional Deployments

Azure OpenAI is available in multiple regions:

```python theme={null}
# East US
response = completion(
    model="azure/gpt-4o",
    api_base="https://eastus.api.cognitive.microsoft.com/",
    api_key="..."
)

# West Europe
response = completion(
    model="azure/gpt-4o",
    api_base="https://westeurope.api.cognitive.microsoft.com/",
    api_key="..."
)
```

## Best Practices

<CardGroup cols={2}>
  <Card title="Use Latest API Version" icon="clock">
    Always use the latest stable API version for new features and improvements.
  </Card>

  <Card title="Handle Content Filters" icon="shield">
    Azure applies content filtering - handle these responses appropriately.
  </Card>

  <Card title="Use Managed Identity" icon="key">
    For Azure-hosted apps, use Managed Identity instead of API keys.
  </Card>

  <Card title="Monitor Rate Limits" icon="gauge">
    Track TPM (tokens per minute) and RPM (requests per minute) limits.
  </Card>
</CardGroup>

## Troubleshooting

### Deployment Not Found

```python theme={null}
# Make sure deployment name matches Azure
response = completion(
    model="azure/your-exact-deployment-name",  # Must match Azure portal
    messages=[{"role": "user", "content": "Hello"}]
)
```

### API Version Issues

```python theme={null}
# Use a supported API version
response = completion(
    model="azure/gpt-4o",
    api_version="2024-02-15-preview",  # Check Azure docs for valid versions
    messages=[{"role": "user", "content": "Hello"}]
)
```

## Related Documentation

<CardGroup cols={2}>
  <Card title="OpenAI" icon="openai" href="/providers/openai">
    Learn about OpenAI models and features
  </Card>

  <Card title="Streaming" icon="wave-pulse" href="/providers/streaming">
    Stream responses in real-time
  </Card>

  <Card title="Function Calling" icon="function" href="/providers/function-calling">
    Implement function calling
  </Card>

  <Card title="Embeddings" icon="vector-square" href="/providers/embeddings">
    Generate embeddings on Azure
  </Card>
</CardGroup>
