How to use managed identity with Azure OpenAI
Quick answer
Use the
AzureOpenAI client with the azure_ad_token_provider parameter to authenticate via managed identity instead of an API key. This leverages Azure's managed identity service for secure, keyless access to the Azure OpenAI endpoint.PREREQUISITES
Python 3.8+Azure subscription with Azure OpenAI resourceManaged identity enabled on your Azure compute resourcepip install openai>=1.0 azure-identity
Setup
Install the required Python packages and set environment variables for your Azure OpenAI endpoint and deployment name. Ensure your Azure resource has a managed identity enabled and granted access to the Azure OpenAI resource.
pip install openai azure-identity Step by step
This example shows how to use the AzureOpenAI client with managed identity authentication by passing an azure_ad_token_provider from DefaultAzureCredential. Replace environment variables with your Azure OpenAI endpoint and deployment name.
import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential
# Initialize Azure AD token provider for managed identity
credential = DefaultAzureCredential()
client = AzureOpenAI(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
azure_ad_token_provider=credential,
api_version="2024-02-01"
)
response = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Hello from managed identity!"}]
)
print(response.choices[0].message.content) output
Hello from managed identity!
Common variations
- Use other Azure AD credential classes like
ManagedIdentityCredentialif you want to specify client ID. - Switch models by changing the deployment name in
modelparameter. - Use async calls with
asyncioandawaitif your environment supports it.
import os
import asyncio
from openai import AzureOpenAI
from azure.identity.aio import DefaultAzureCredential
async def main():
credential = DefaultAzureCredential()
client = AzureOpenAI(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
azure_ad_token_provider=credential,
api_version="2024-02-01"
)
response = await client.chat.completions.acreate(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Async hello from managed identity!"}]
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Async hello from managed identity!
Troubleshooting
- If you get authentication errors, verify your Azure resource has managed identity enabled and the identity has proper role assignments to access Azure OpenAI.
- Ensure environment variables
AZURE_OPENAI_ENDPOINTandAZURE_OPENAI_DEPLOYMENTare correctly set. - Check network connectivity to the Azure OpenAI endpoint.
- Use
DefaultAzureCredentiallogging to debug credential acquisition issues.
Key Takeaways
- Use
DefaultAzureCredentialwithAzureOpenAIfor managed identity authentication. - Set
azure_ad_token_providerinstead ofapi_keyto enable keyless access. - Ensure your Azure resource's managed identity has permission to the Azure OpenAI resource.
- Async usage is supported with
acreateandazure.identity.aiocredentials.