How to fix - openai.AuthenticationError: Error code: 401 - {'error': {'code': 'PermissionDenied', 'message': 'Principal does not have access to API/Operation.'}}

Rawat, Rahul 0 Reputation points
2025-02-16T07:34:48.21+00:00

Hi Team,

I am using below git repo for reference -

https://github.com/Azure-Samples/azure-search-openai-demo.git

All required roles assigned to service principle being used by the code -

  • OpenAI Contributor Role

Trying to setup locally when running to get chat answers as stream via api - /chat/stream

getting below error -

File "/Users/rrawat33/AIWorkspace/python_playground_space/app/backend/app.py", line 203, in format_as_ndjson

*async for event in r:*

openai.AuthenticationError: Error code: 401 - {'error': {'code': 'PermissionDenied', 'message': 'Principal does not have access to API/Operation.'}}

[2025-02-16 12:33:47 +0530] [87938] [INFO] 127.0.0.1:52223 POST /chat/stream 1.1 200 - 3851673

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,080 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,598 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vikram Singh 2,585 Reputation points Microsoft Employee Moderator
    2025-02-27T11:16:26.0033333+00:00

    Hi Rawat, Rahul,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!

    Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer. Accepted answers show up at the top, resulting in improved discoverability for others.

    Issue: How to fix - openai.AuthenticationError: Error code: 401 - {'error': {'code': 'PermissionDenied', 'message': 'Principal does not have access to API/Operation.

    Solution: Following are the Role assignment which helped me to resolved the permission issue with open AI demo python code.

    Azure Open AI -

    1. Azure AI developer
    2. Cognitive services contributor

    Azure Search (Cognitive search service) -

    1. Search index data manager
    2. Azure AI Administrator

    If your issue remains unresolved or have further questions, please let us know in the comments how we can assist. We are here to help you and strive to make your experience better and greatly value your feedback.

    Cheers

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.