So recently i have been experimenting about making app using Gemini API and i’ve found the problem. I can’t store Gemini API Key that user input to st.session_state. How do i know this?
So i’ve been making class that have 2 function. First, it check when the API key was truly exist. Second, it store API-key to st.session_state so i can use it in another class or function. I make other class that contain model Gemini configuration. But it will result error because i save API-Key to st.session_state.
But when i try another option, using API-key and don’t store to st.session_state it works.
Can anyone explain why i can’t store API-key into st.session_state but it can as local variable?
Can you show the code for initializing the session state variable, how you assign its value, and how it’s then referenced in the Gemini API call?
If the key is user-supplied, and is user-specific then session state is the correct storage pattern to use. If the key is global, then hold it in secrets.toml, or in an environment variable.
I think it’s user-supplied and user-spesific and use it in global. But each user has different access and result. Did you have any documentation or tutorial for this spesific problem?
before i use local variable and sign it to st.session_state
''' i made function to create text area and also sign it to session_state'''
if 'gemini_api_key' is not in st.session_state:
st.session_state.gemini_api_key = None
input_gemini_api = st.text_input(
label='Gemini-AI API key',
placeholder='Input your own Gemini-AI API key',
type='password',
help='required to use this application'
)
api_key_valid = True
try: # Check first
self.check_gemini_api_key(input_gemini_api) #it's just check it in another function, this function doesn't have error
except Exception:
api_key_valid = False
if api_key_valid == True:
# return
st.session_state.gemini_api_key = input_gemini_api
else:
st.error("Invalid Gemini API key. Please check and try again.")
after i sign it to local variable
''' i made function to create text area and also sign it to session_state'''
input_gemini_api = st.text_input(
label='Gemini-AI API key',
placeholder='Input your own Gemini-AI API key',
type='password',
help='required to use this application'
)
api_key_valid = True
try: # Check first
self.check_gemini_api_key(input_gemini_api) #it's just check it in another function, this function doesn't have error
except Exception:
api_key_valid = False
if api_key_valid == True:
# return
return input_gemini_api
else:
st.error("Invalid Gemini API key. Please check and try again.")
return None
(this function in class GeminiAPIManager)
If you need to watch my code, i will give you link to my repo. I made this app only for Google AI hackaton, not for commercial or anything. I have made lot of documentation right there.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.