Azure OpenAI private endpoint setup
Quick answer
To set up a
private endpoint for Azure OpenAI, configure the private link in the Azure portal to restrict network access. Then use the AzureOpenAI client from the openai Python SDK with your private endpoint URL as azure_endpoint and your API key from environment variables.PREREQUISITES
Python 3.8+Azure subscription with Azure OpenAI resourcePrivate endpoint configured in Azure portalAzure OpenAI API keypip install openai>=1.0
Setup
Install the official openai Python package version 1.0 or higher. Set environment variables for your Azure OpenAI API key and private endpoint URL. The private endpoint URL is the fully qualified domain name (FQDN) of your Azure OpenAI resource's private link.
pip install openai>=1.0 Step by step
Use the AzureOpenAI client from the openai package. Pass your private endpoint URL as azure_endpoint and the API key from environment variables. Call chat.completions.create with your deployment name as the model. This example sends a simple prompt and prints the response.
import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_PRIVATE_ENDPOINT"],
api_version="2024-02-01"
)
response = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Hello from private endpoint!"}]
)
print(response.choices[0].message.content) output
Hello from private endpoint! How can I assist you today?
Common variations
- Use different
modeldeployment names as configured in Azure. - For async calls, use
asynciowithawait client.chat.completions.acreate(...). - Streaming responses are supported with
stream=Trueparameter.
import asyncio
from openai import AzureOpenAI
async def main():
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_PRIVATE_ENDPOINT"],
api_version="2024-02-01"
)
response = await client.chat.completions.acreate(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Async hello from private endpoint!"}]
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Async hello from private endpoint! How can I assist you today?
Troubleshooting
- If you get
403 Forbidden, verify your private endpoint is approved and DNS resolves to the private IP. - Ensure your
azure_endpointURL matches the private endpoint FQDN exactly, including protocolhttps://. - Check that your API key has permissions for the Azure OpenAI resource.
Key Takeaways
- Configure Azure private endpoint in the Azure portal before using it in code.
- Use
AzureOpenAIclient withazure_endpointset to your private endpoint URL. - Always load API keys and endpoint URLs from environment variables for security.
- Async and streaming calls are supported with the AzureOpenAI Python SDK.
- Verify DNS and network access if you encounter authorization or connectivity errors.