How to solve this error message?

validation error for OpenAIEmbeddings root Did not find openai_api_key, please add an environment variable OPENAI_API_KEY which contains it, or pass openai_api_key as a named parameter. (type=value_error)

Hey @kevinjosue1,

It looks like your app doesn’t have an OpenAI API key and you’re trying to use the OpenAI API.

Try adding your own Api key as i think you have not set an api key in your app

Try adding your own openai api key

I have this same error coming up but I have an API key in my .env that I know is good and I can see where dotenv is being imported in my app.py so I am really unsure what is going wrong here. Any help would be appreciated.

Instead of storing in the .env use the following method:-

If you are running the application in your host then maintain a folder named . streamlit and that create a file named secrets.toml . That file structured should be in the following format:-

api_key='xxxxx'

Then use the following code in your main file as follows:-

import streamlit as st
openai.api_key=st.secrets['api_key']

If you want publish your app in cloud, before hitting the deploy button, click on advance settings then click on secrets. In there add the secret as follows:-

api_key=‘xxxxx’

(Note:- don’t add any other extra things in that window)

If you are deploying in the cloud no need of maintaining the folder . streamlit in your directory.

Also make sure your main file should as the following format:-

openai.api_key=st.secrets['api_key']