Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Environment:Openai Openai python Azure OpenAI

From Leeroopedia
Knowledge Sources
Domains Infrastructure, Azure
Last Updated 2026-02-15 10:00 GMT

Overview

Azure-specific environment for the OpenAI Python SDK, requiring Azure credentials, endpoint configuration, and API version specification.

Description

This environment extends the base Python 3.9+ environment with Azure OpenAI Service credentials and configuration. Instead of using the standard `OPENAI_API_KEY` and `api.openai.com` endpoint, Azure deployments require Azure-specific authentication (API key or Azure AD token) and endpoint URLs. The SDK provides dedicated `AzureOpenAI` and `AsyncAzureOpenAI` client classes that handle Azure-specific URL routing and authentication.

Usage

Use this environment when deploying OpenAI models through Azure OpenAI Service instead of the direct OpenAI API. It supports the same API features (chat completions, embeddings, etc.) but routes through Azure infrastructure with Azure authentication. The `AzureOpenAI` client replaces the standard `OpenAI` client.

System Requirements

Category Requirement Notes
OS Linux, macOS, or Windows Same as base SDK
Python >= 3.9 Same as base SDK
Network HTTPS connectivity Requires outbound HTTPS to Azure OpenAI endpoints

Dependencies

System Packages

No additional system packages required beyond the base SDK.

Python Packages

Credentials

Authentication (one of the following is required):

  • `AZURE_OPENAI_API_KEY`: Azure API key for authentication.
  • `AZURE_OPENAI_AD_TOKEN`: Azure Active Directory token.
  • Or pass `azure_ad_token_provider` function to the client constructor.

Endpoint (required):

API Version (required):

  • `OPENAI_API_VERSION`: API version string (e.g., `2024-02-01`).

Quick Install

# Install core SDK (Azure client is included)
pip install openai

# Optional: Install Azure Identity for AD token authentication
pip install azure-identity

Code Evidence

Azure credential resolution from `lib/azure.py:192-201`:

if api_key is None:
    api_key = os.environ.get("AZURE_OPENAI_API_KEY")
if azure_ad_token is None:
    azure_ad_token = os.environ.get("AZURE_OPENAI_AD_TOKEN")
if api_key is None and azure_ad_token is None and azure_ad_token_provider is None:
    raise OpenAIError(
        "Missing credentials. Please pass one of `api_key`, `azure_ad_token`, "
        "`azure_ad_token_provider`, or the `AZURE_OPENAI_API_KEY` or "
        "`AZURE_OPENAI_AD_TOKEN` environment variables."
    )

API version requirement from `lib/azure.py:203-209`:

if api_version is None:
    api_version = os.environ.get("OPENAI_API_VERSION")
if api_version is None:
    raise ValueError(
        "Must provide either the `api_version` argument or the "
        "`OPENAI_API_VERSION` environment variable"
    )

Endpoint requirement from `lib/azure.py:216-223`:

if base_url is None:
    if azure_endpoint is None:
        azure_endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT")
    if azure_endpoint is None:
        raise ValueError(
            "Must provide one of the `base_url` or `azure_endpoint` arguments, "
            "or the `AZURE_OPENAI_ENDPOINT` environment variable"
        )

Common Errors

Error Message Cause Solution
`Missing credentials. Please pass one of api_key, azure_ad_token, azure_ad_token_provider...` No Azure authentication provided Set `AZURE_OPENAI_API_KEY` or `AZURE_OPENAI_AD_TOKEN` environment variable
`Must provide either the api_version argument or the OPENAI_API_VERSION environment variable` API version not specified Set `OPENAI_API_VERSION` environment variable (e.g., `2024-02-01`)
`Must provide one of the base_url or azure_endpoint arguments...` No Azure endpoint configured Set `AZURE_OPENAI_ENDPOINT` environment variable
`base_url and azure_endpoint are mutually exclusive` Both base_url and azure_endpoint provided Use only one: either `base_url` or `azure_endpoint`

Compatibility Notes

  • Assistants API: The `azure_deployment` parameter is not supported with Assistants APIs when used with `azure_endpoint`.
  • Deployment URLs: When `azure_deployment` is provided with `azure_endpoint`, the base URL is set to `{azure_endpoint}/openai/deployments/{azure_deployment}`.
  • API versions: Azure OpenAI Service uses a different API versioning scheme than the direct OpenAI API. Always specify the version explicitly.

Related Pages

Page Connections

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