No API KEY provided

Hello,
I’m Anna and I am new in this area. I have the same error as you @mp .
When I try to finetune a model I get thes message: >

openai api fine_tunes.follow -i ft-xxxxxx
←[91mError:←[0m No API key provided. You can set your API key in code using ‘openai.api_key = ’, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with ‘openai.api_key_path = ’. You can generate API keys in the OpenAI web interface. See the onbard page from openai for details, or email support@openai.com if you have any questions.

How did you solve the problem ?

Unfortunately I could not find it in your answers…

I would be very grateful for help

thanks, it works for me :grin: :+1: :+1: :+1:. Just change my_api_key with your api key

In my case I basically had to move the

import os
os.environ["OPENAI_API_KEY"] = <OPENAI_API_KEY>

to the very top of the file ABOVE OTHER IMPORTS and it basically works.

2 Likes

That solved it for me - the import of openai itself had to come after the environment setting:

import os
os.environ['OPENAI_API_KEY'] = 'your key here'
import openai

blah blah blah...

I’m getting the error:

Listening on http://127.0.0.1:4370
! OPENAI_API_KEY is not valid. Generate a key at <https://platform.openai.com/account/api-keys>
Set the key in your .Renviron file usethis::edit_r_environ()
Warning: Error in purrr::map_chr: ℹ In index: 1.
Caused by error:
! Result must be length 1, not 0.

Despite having an API Key set

I’m using OpenAI in my Svelte kit web application. Added API key in .env and loads for server-side code.

const _apiKey = import.meta.env.VITE_OPENAI_API_KEY;
import OpenAI from "openai";
const openai = new OpenAI({apiKey:_apiKey });

Everything works fine during development, threads and messages are loading fine.
When I try to do a build for production (npm run build), I get the below error.

Error: The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: ‘My API Key’ })

Can someone help to solve this error?

Are you setting the .env for the production environment? Sounds like you might be missing that stage, put in some temp debugging to print the first 5 letters of your API _Key to console and see if you get a blank test log entry!