Request is badly formatted Error with Azure OpenAI
When attempting to call Azure OpenAI using a Managed Identity, you may encounter the following error:
ERROR:root:Error generating response: Error code: 400 - {'error': {'code': 'Request is badly formated', 'message': 'Resource Id is badly formed or from wrong namespace: NA'}}
2024-11-26T18:57:08.224640325Z ERROR:root:Response content: b'{"error":{"code":"Request is badly formated","message":"Resource Id is badly formed or from wrong namespace: NA"}}'
I had issues when attempting to call the Azure OpenAI endpoint from an Azure Container App using a Managed Identity. The error message that was getting returned to my App was:
An error generating a reponse.
After enabling some logging on my Python application, I was able to review the Console Logs of my Container (running in a Container Apps environment) and found the following error message:
After some testing, I was able to confirm that I could receive a response using the API keys, but when using the Managed Identity, I got an error message.
My Azure OpenAI instance was created with Infrastructure as Code and I noticed that the endpoint of my Azure OpenAI was:
https://eastus2.api.cognitive.microsoft.com/
On an Azure OpenAI instance that was created in the Portal, the endpoint was:
https://azureopenaitestlol.openai.azure.com/
So, I made a comparison between the two resources:
And my IaC-created Azure OpenAI resource was missing the following:
- customSubDomainName
After some digging, I found that the customSubDomainName is the key to the issue. When using a Managed Identity, the customSubDomainName is required to be set for Entra ID to be able to authenticate the request
Custom subdomain names are required to enable features like Microsoft Entra ID for authentication.
So after updating the Azure OpenAI resource to use the subdomain, updating my environment variables to use the right Azure OpenAI endpoint (and making sure that the CLIENT_ID of the User Assigned Managed identity is correct) I was able to call the Azure OpenAI endpoint using the Managed Identity successfully.