Translation api returns incorect api key while the same key works for chat

I am trying to get a transcription for an audio file but I get “AuthenticationError: Error code: 401 - {‘error’: {‘message’: 'Incorrect API key provided:”

This is the code that generates the error:

audio_file = open("chunk0.mp3", "rb")

transcription = client.audio.transcriptions.create(
  model="whisper-1", 
  file=audio_file
)

In the same time a call to the openai.chat.completions works fine with the key.

I have tried generating a few keys already and I have allowed “All” so permissions should not be an issue.

Thank you!

1 Like

Here’s a single script to see what authentication is being used after calling both chat completions and Whisper transcriptions. That may be the first in unlocking this puzzle.

from openai import OpenAI
client = OpenAI()
try:
    print(client.chat.completions.create(
        model="gpt-4o-mini", max_tokens=10,
        messages=[{'role':'user','content': 'hi'}]
    ).choices[0].message.content)
except Exception as e:
    print('Chat Completions error {e}')
print('--- chat completions authentication used:')
print(client.api_key or "")
print(client.organization or "")
print(client.project or "", "\n")
del client

client2 = OpenAI()
input_file_path = ".\chunk0.mp3"
with open(input_file_path, "rb") as audio_file:
    try:
        transcription = client2.audio.transcriptions.create(
            file=audio_file, # a flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, webm
            language="en",   # ISO code
            model="whisper-1",
            response_format="text",  # json, text, srt, verbose_json, or vtt
            temperature=0.2)
        print(transcription[:60])
    except Exception as e:
        print(f"Transcriptions API error: {e}")
print('--- transcriptions authentication used:')
print(client2.api_key or "")
print(client2.organization or "")
print(client2.project or "")

You can ensure that the environment variables OPENAI_ORG_ID and OPENAI_PROJECT_ID are also provided and match the organization and project containing the API key.

I hope that helps to discover any client-side issue.

2 Likes

Welcome @vasilescu_anton

How are you providing the API keys in both cases?

1 Like