openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
22.85k stars 3.2k forks source link

AuthenticationError with Incorrect API Key #886

Closed ozfollowoz closed 11 months ago

ozfollowoz commented 11 months ago

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

Describe the bug

When attempting to run the provided code, an openai.AuthenticationError is raised with the error message stating, "Incorrect API key provided." The error code is 401.

To Reproduce

Traceback (most recent call last): File "d:\Chatbot_AzureAI_CSV_Project\open.py", line 23, in asyncio.run(main())

... (rest of the traceback)

openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: 9****p. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

Code snippets

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key="***************************",  # <-- Replace with the correct API key
)

async def main() -> None:
    chat_completion = await client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": "Say this is a test",
            }
        ],
        model="gpt-3.5-turbo",
    )

asyncio.run(main())

OS

Windows

Python version

3.11.0

Library version

Latest

RobertCraigie commented 11 months ago

Have you tried using an API key from the link provided in the error message?

ozfollowoz commented 11 months ago

Yes, I have added API key from the link provided

asaltanat commented 9 months ago

Could you solve the problem? I am facing the same error. I have checked my api_key it is working when I run my API on my computer, but when I put my API in a Docker container and try to use the API it results this error: openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: .....}}

rattrayalex commented 9 months ago

You may need to check that there's no leading or trailing spaces, or other errors with your API Key.