I'm encountering issues with both Azure CLI curl commands and the API endpoint for my app when trying to interact with an Azure OpenAI (AOAI) deployment.
I'm attempting to run the following curl command from the Azure CLI to test my OpenAI deployment, but I keep getting "401 Unauthorized" errors. Here’s the command I’m using:
curl -X POST "https://<resource-name>.openai.azure.com/openai/deployments/<deployment-name>/chat/completions?api-version=2024-08-01-preview" \
-H "Ocp-Apim-Subscription-Key: <my-subscription-key>" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, can you tell me a joke?"}
],
"max_tokens": 50,
"temperature": 0.7
}'
Despite double-checking the resource name, deployment name, API version, and Ocp-Apim-Subscription-Key, I continue to receive errors.
- 401 Unauthorized — Despite using the correct API key and ensuring role assignments are in place.
The reason this came about is because I have an application using a similar logic to interact with the Azure OpenAI endpoint. I figured to try the curl commands in Azure CLI to see if the problem is with the app or the AOAI deployment. The app is also failing with the same error:
Status Code: 401
Response: {"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}
Here’s a snippet of my API call logic within the app:
headers = {
"Ocp-Apim-Subscription-Key": self.api_key,
"Content-Type": "application/json"
}
payload = {
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, can you tell me a joke?"}
],
"max_tokens": 50,
"temperature": 0.7
}
response = requests.post(self.models_url, headers=headers, json=payload)
Here are the logs when I run the app:
Connected to Azure LLM at https://<resource-name>.openai.azure.com/ with deployment '<deployment-name>'.
=== DEBUG INFO ===
URL: https://<resource-name>.openai.azure.com/openai/deployments/<deployment-name>/chat/completions?api-version=2024-08-01-preview
Headers: {'Ocp-Apim-Subscription-Key': '<api-key1>', 'Content-Type': 'application/json'}
Payload: {'messages': [{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'Hello, can you tell me a joke?'}], 'max_tokens': 50, 'temperature': 0.7}
==================
=== RESPONSE INFO ===
Status Code: 401
Response Text: {"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}
=====================
Traceback (most recent call last):
...
raise Exception(
Exception: Failed to retrieve LLM info: 401 Unauthorized - Please check your API key or role permissions.
I have tried multiple troubleshooting steps but am still not able to resolve this error. What I have tried:
- Verified the keys from Keys and Endpoints of the AOAI match correctly
- Verified the deployment exists
- Verified the endpoint URL and deployment name are being used correctly
- Verified permissions and roles assigned to the user in IAM (Owner, Cognitive Services Contributor, Cognitive Services OpenAI Contributor)
- Checked for API key issues, regenerated keys, waited 5 min as suggested in documentation, and retried
- Attempted to re-deploy the OpenAI deployment, created a new deployment but still receive the same error