Why do my Azure OpenAI credentials work with Go, but not with Python?

Daniel R 0 Reputation points
2024-07-23T21:17:06.55+00:00

I have an Azure OpenAI instance deployed in Azure OpenAI Studio. My environment variables are properly set up and exported according to the credentials associated with that deployment. I know they are the correct credentials because I have some Go code that makes use of the same Azure instance with the same exact credentials. However, I am trying to use python's openai and langchain libraries. Maybe it's important to note that the Go code does not make use of the api version, whereas the Python libraries explicitly request it. When using Python, I consistently run into one of five possible errors (depending on which module I call).

  1. httpx.InvalidURL: Invalid non-printable ASCII character in URL.
  2. httpx.LocalProtocolError: Illegal header value b'Bearer {azure_openai_api_key}\r'.
  3. openai.APIConnectionError: Connection error.
  4. AuthenticationError: 'Incorrect API key provided: {azure_openai_api_key}.'
  5. NotFoundError: 'Resource not found'.

From what I've been able to tell, errors 1 and 2 seems to be the root cause of all the others, but I could be wrong. Note that I've tried using .strip() and .rstrip() to remove the extra "\r" that appears, but doing so results in errors 3 or 5, depending on whether I am using openai or langchain_openai library.

The exhaustive list of fixes I've tried is probably too long to put here, so I'll just mention a few: renaming the environment variables, using .env versus .envrc (and os.getenv() versus os.environ[]), exporting environment variables directly from the command line, trying various different modules within openai and langchain_openai, trying Azure OpenAI deployment Key 1 versus Key 2, running the code in a virtual environment, running the code in a conda environment, running the code in jupyter, hard-coding in the credentials, and several other strategies---even ones I was certain would have no effect.

To simplify the issue, I tried running the example code on this page, under "Create A New Python Application", replacing ONLY the api_version with "2024-05-13" and the deployment_name with my deployment's name (NOT the deployment model, which is gpt-4o). My code is in the image below. Again, my environment variables (i.e. azure_openai_api_key and azure_openai_endpoint) are set exactly as the ones in my Go code, which has been able to successfully leverage Azure OpenAI.

setup_code

Line 4 raises the aforementioned httpx.InvalidURL error. I think if I can fix this error, then the other ones should fall into place, because the invalid character is likely causing the illegal header value (and reading an incorrect API key), which is probably to do with the APIConnectionError / NotFoundError.

Note that the way I run my code is as follows:

$ direnv allow
$ python3 example.py

My question is this: why is it that my Azure OpenAI credentials would work with my Go code, but not my Python code, when the connection is being made with the same credentials?

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,041 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 33,746 Reputation points
    2024-07-24T10:08:08.1366667+00:00

    Daniel R Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    To simplify the issue, I tried running the example code on this page, under "Create A New Python Application", replacing ONLY the api_version with "2024-05-13" and the deployment_name with my deployment's name (NOT the deployment model, which is gpt-4o). My code is in the image below. Again, my environment variables (i.e. azure_openai_api_key and azure_openai_endpoint) are set exactly as the ones in my Go code, which has been able to successfully leverage Azure OpenAI.

    As you have mentioned, it is working fine with GO and facing issues with python version, could you try with API version 2024-06-01 which is the latest GA data plane inference API release.

    Another thing to check is whether the Python libraries require any additional configuration or setup beyond what is required by the Go code. For example, you mentioned that the Python libraries explicitly request an API version, whereas the Go code does not. It's possible that there are other differences in the way the two libraries are configured that could be causing issues.

    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.