Why i can't store API Key in st.session_state?

Hello,

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?

Please add code example?

here’s my example:

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.

Have you tried removing is in if 'gemini_api_key' is not in st.session_state?