Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api-key missing from the request in case of async calls (AsyncOpenAI, AsyncAzureOpenAI) #1431

Closed
1 task done
SoranaBaciu opened this issue May 17, 2024 · 5 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@SoranaBaciu
Copy link

SoranaBaciu commented May 17, 2024

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

When trying to perform an await AsyncOpenAI.chat.completions.create call, it will results in 'statusCode': 401, 'message': 'Unauthorized. Access token is missing.

The found workaround it to add extra_headers

    response = await client.chat.completions.create(
        model="deployment_name",
        extra_headers={"api-key": "_my_api_key"},
        messages=[{"role": "user", "content": prompt_text}],
    )

After debugging I've seen that _prepare_options for sync calls (OpenAI, AzureOpenAI) is implemented, and it actually sets the api-key in the headers, while it is not for async chat completions (AsyncOpenAI, AsyncAzureOpenAI).

To Reproduce

Library version 1.30.1

   client = AsyncAzureOpenAI(
        azure_endpoint="my_azure_endpoinf",
        api_key="my_api_key",
        api_version="my_api_version",
        azure_deployment="my_deployment",
    )
    response = await client.chat.completions.create(
        model="my_deployment",
        # extra_headers={"api-key": "my_api_key"},  # will not work if this is commented out
        messages=[{"role": "user", "content": prompt_text}],
    )

=>

openai.AuthenticationError: Error code: 401 - {'statusCode': 401, 'message': 'Unauthorized. Access token is missing, invalid, audience is incorrect (https://cognitiveservices.azure.com), or have expired.'}

Code snippets

No response

OS

Windows

Python version

3.11.2

Library version

1.30.1

@SoranaBaciu SoranaBaciu added the bug Something isn't working label May 17, 2024
@kristapratico
Copy link
Contributor

@SoranaBaciu I'm not able to reproduce this. Can you check that you don't have any environment variables set that might be interfering with the auth?

@SoranaBaciu
Copy link
Author

@kristapratico no environment variables, actually seams to be ok for AsyncAzureOpenAI, issue appearing for AsyncOpenAI

Here's my scipt

import asyncio
from openai import AsyncOpenAI

async def a_test():
    client = AsyncOpenAI(
        api_key="my_api_key",
        base_url="https://<MY_AZURE_OPENAI>.openai.azure.com/openai/deployments/<MY_DEPLOYMENT>",
        default_query={"api-version": "2024-02-01"},
    )
    response = await client.chat.completions.create(
        model="my_deployment",
        # extra_headers={"api-key": "my_api_key"},  # will not work if this is commented out
        messages=[{"role": "user", "content": "What is Python?"}],
    )

    print(response.choices[-1].message.content)

asyncio.run(a_test())

@kristapratico
Copy link
Contributor

@SoranaBaciu based on your example, it looks like you're targeting the Azure OpenAI service through the AsyncOpenAI client. If you are using the Azure API, I recommend using the AsyncAzureOpenAI client since it's configured to work with Azure endpoints, auth, deployments, etc. To summarize,

  • OpenAI, AsyncOpenAI clients work with the OpenAI API
  • AzureOpenAI, AsyncAzureOpenAI clients work with the Azure OpenAI API

Is there a reason you want to use AsyncOpenAI with Azure?

@SoranaBaciu
Copy link
Author

SoranaBaciu commented May 23, 2024

@kristapratico No special reason. I wanted async support, and initially I've found AsyncOpenAI and discovered "the bug". Afterwards I've seen the dedicated class for Azure.

@rattrayalex
Copy link
Collaborator

Great. Krista is correct that AsyncAzureOpenAI is the correct class to use for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants