OpenAI and Foundry resources returning 401 unauthorised using the API

Alex Visan (alvisan) 0 Reputation points
2025-05-22T19:42:48.43+00:00

I created an OpenAI resource within a resource group (all network access), I created some deployments on it (in the same region as the resources), but when I try to reach it through my python app or via curl, I am getting -> {"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."}}.

I also tried creating a foundry project and creating some deployments in there, but I am getting the same error.

The interesting thing is that prior to creating these 2 resources, I was playing around with another openai resource the other day and that was working with the exact same setup.

Am I missing something or is this an issue I should be reporting somewhere?

Foundry Tools
Foundry Tools

Formerly known as Azure AI Services or Azure Cognitive Services is a unified collection of prebuilt AI capabilities within the Microsoft Foundry platform


1 answer

Sort by: Most helpful
  1. Alex Visan (alvisan) 0 Reputation points
    2025-05-23T07:26:52.76+00:00

    After debugging this further, I noticed the following discrepancy:

    When running the file with debugpy, the environment variable is updated and the call is made properly.
    When running the application with python filename.py, the env variable has an old value.

    Making the following adjustment in my code resolves the issue:

    load_dotenv(override=True)
    
    

    Was this answer helpful?


Your answer

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