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).
- httpx.InvalidURL: Invalid non-printable ASCII character in URL.
- httpx.LocalProtocolError: Illegal header value b'Bearer {azure_openai_api_key}\r'.
- openai.APIConnectionError: Connection error.
- AuthenticationError: 'Incorrect API key provided: {azure_openai_api_key}.'
- 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.

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?