How to configure Azure OpenAI environment variables
Quick answer
Set the
AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and AZURE_OPENAI_DEPLOYMENT environment variables to configure Azure OpenAI in your Python environment. These variables enable the AzureOpenAI client to authenticate and target the correct deployment when calling the API.PREREQUISITES
Python 3.8+Azure OpenAI resource with deployed modelpip install openai>=1.0Azure OpenAI API key and endpoint URL
Setup environment variables
Configure the following environment variables in your system or development environment to use Azure OpenAI with the Python SDK:
AZURE_OPENAI_API_KEY: Your Azure OpenAI API key.AZURE_OPENAI_ENDPOINT: The endpoint URL of your Azure OpenAI resource, e.g.,https://your-resource-name.openai.azure.com/.AZURE_OPENAI_DEPLOYMENT: The deployment name of the model you want to use, as configured in the Azure portal.
Set these variables in your shell or IDE before running your Python code.
export AZURE_OPENAI_API_KEY="your_azure_openai_api_key"
export AZURE_OPENAI_ENDPOINT="https://your-resource-name.openai.azure.com/"
export AZURE_OPENAI_DEPLOYMENT="your-deployment-name" Step by step Python example
Use the AzureOpenAI client from the openai package to authenticate using the environment variables and send a chat completion request.
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!"}]
)
print(response.choices[0].message.content) output
Hello from Azure OpenAI!
Common variations
You can customize your Azure OpenAI usage with these variations:
- Async calls: Use
asynciowithAzureOpenAIfor asynchronous requests. - Different API versions: Change the
api_versionparameter to match your Azure resource version. - Streaming responses: Use streaming parameters if supported by your deployment.
import asyncio
from openai import AzureOpenAI
async def async_chat():
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 hello!"}]
)
print(response.choices[0].message.content)
asyncio.run(async_chat()) output
Async hello!
Troubleshooting tips
- If you get authentication errors, verify your
AZURE_OPENAI_API_KEYandAZURE_OPENAI_ENDPOINTare correct and have no trailing spaces. - If the deployment name is invalid, check your Azure portal for the exact deployment name and update
AZURE_OPENAI_DEPLOYMENT. - Ensure your environment variables are loaded in the shell or IDE session where you run your Python script.
Key Takeaways
- Always set
AZURE_OPENAI_API_KEY,AZURE_OPENAI_ENDPOINT, andAZURE_OPENAI_DEPLOYMENTenvironment variables before using Azure OpenAI SDK. - Use the
AzureOpenAIclient with these variables to authenticate and specify the deployment model. - Verify environment variables are correctly loaded in your runtime environment to avoid authentication or deployment errors.