I am following this tutorial to generate images via API with Imagen 3: Generate images using Imagen 3 | Gemini API | Google AI for Developers . I am using a clean Python 3.12.7 install via PowerShell 7.4.5 on a Win11 machine. The python install includes pip, pillow, git and winget (among others). My company has access to Imagen 3 which I have confirmed by generating images using it in Vertex AI Studio.
I am able to successfully follow the tutorial (thanks to help from @afirstenberg) until I get to this line:
genai.configure(api_key=os.environ['API_KEY'])
That line results in an error, which I have searched but can’t figure out how to resolve:
>>> genai.configure(api_key=os.environ['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen os>", line 714, in __getitem__
KeyError: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
>>>
I have confirmed that my API key is active, and even generated a new one to be sure using Google AI Studio.
In this case, you should leave API_KEY exactly as it is.
This line reads the API Key from the “API_KEY” environment variable, as suggested in an earlier step of the tutorial:
This tutorial assumes that you’re accessing your API key as an environment variable.
Since you’re using PowerShell, you would set the environment variable with the key using something like this:
$Env:API_KEY = "XXXXX-your-key-here-XXXXX"
in the shell before you try to run your python script.
(And, for the record, it was @OrangiaNebula that pointed you in the right direction about which version of python to use. I was just warning other folks that came along that Imagen 3 on AI Studio is currently in private preview.)
Use Google to search for “environment variables” and how to set them (it depends on operating system and command interpreter / shell).
The code expects API_KEY to be the name of the environment variable, and the value assigned to that environment variable, before your process starts up, is supposed to be the api key indecipherable bytes.