How to beginner · 3 min read

Azure OpenAI SDK installation

Quick answer
Install the openai Python package (version 1.0 or higher) using pip. Then, create an AzureOpenAI client with your Azure endpoint and API key from environment variables to start calling Azure OpenAI models.

PREREQUISITES

  • Python 3.8+
  • Azure OpenAI API key
  • Azure OpenAI endpoint URL
  • pip install openai>=1.0

Setup

Install the official openai Python package version 1.0 or later. Set your Azure OpenAI API key and endpoint as environment variables for secure access.

bash
pip install openai>=1.0

Step by step

Use the AzureOpenAI class from the openai package to create a client. Pass your API key and Azure endpoint URL from environment variables. Call chat.completions.create with your deployment name and messages to get a response.

python
import os
from openai import AzureOpenAI

client = AzureOpenAI(
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_version="2024-02-01"
)

response = client.chat.completions.create(
    model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
    messages=[{"role": "user", "content": "Hello from Azure OpenAI SDK!"}]
)

print(response.choices[0].message.content)
output
Hello from Azure OpenAI SDK!

Common variations

  • Use different deployment names by changing the model parameter.
  • Enable streaming by passing stream=True to chat.completions.create.
  • Use async calls with asyncio and await if your environment supports it.
python
import asyncio
import os
from openai import AzureOpenAI

async def main():
    client = AzureOpenAI(
        api_key=os.environ["AZURE_OPENAI_API_KEY"],
        azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
        api_version="2024-02-01"
    )

    response = await client.chat.completions.acreate(
        model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
        messages=[{"role": "user", "content": "Async call example"}]
    )

    print(response.choices[0].message.content)

asyncio.run(main())
output
Async call example response text

Troubleshooting

  • If you see authentication errors, verify your AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT environment variables are set correctly.
  • Ensure your deployment name matches the model parameter exactly.
  • Check your network connectivity to the Azure endpoint.

Key Takeaways

  • Use the official openai Python package version 1.0+ for Azure OpenAI integration.
  • Set AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT environment variables before running code.
  • Create an AzureOpenAI client and call chat.completions.create with your deployment name.
  • Support for async and streaming calls is available via the SDK.
  • Verify environment variables and deployment names to avoid common errors.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗