Skip to main content
The OpenAI Python SDK supports Azure OpenAI Service through the AzureOpenAI class.
The Azure API shape differs from the core OpenAI API shape, which means that the static types for responses and parameters won’t always be correct.

Basic Usage

With API Key

With Azure Deployment

You can specify an azure_deployment to include it in the base URL:
The azure_deployment parameter is not supported with Assistants APIs.

Authentication Methods

Azure OpenAI supports multiple authentication methods:

API Key

The simplest method uses an API key:
For production deployments, use Azure AD authentication:

Azure AD Token (Static)

You can also pass a static Azure AD token:

Configuration

Required Parameters

The following parameters are required:
  • api_version: Azure API version (or OPENAI_API_VERSION environment variable)
  • azure_endpoint OR base_url: Your Azure endpoint (or AZURE_OPENAI_ENDPOINT environment variable)

Environment Variables

The SDK automatically reads these environment variables:

Available Options

All options from the base OpenAI client are available, plus:

Async Client

Use AsyncAzureOpenAI for async operations:

API Version

Azure OpenAI uses versioned APIs. See the Azure REST API versioning docs for available versions. Currently recommended version: 2023-07-01-preview

Deployment Names

In Azure OpenAI, you deploy models with custom deployment names. Use your deployment name in the model parameter:

Learn More