'Secrets' object is not callable

Summary (This is my first project.)

I am trying to set an API key as a secret environmental variable. I cannot get it to work with the secrets.toml file, either locally or online. The project worked before, when I just set the variable right in the code, but this exposed my API key when I uploaded it to GitHub.

Problem: It breaks on line 6:

#also do: pip install streamlit
import streamlit as st

#import secret API key through Streamlit secret variables
import os
openai.api_key = st.secrets[“API_KEY”] ### <------------BREAKS HERE

Local error:

Error parsing secrets file at C:\Users\cy\LearnPython\BookWriter.streamlit\secrets.toml

TomlDecodeError: invalid literal for int() with base 0: ‘sk-wqvhJxxxxxxxxxxxxxxxxxxxxxhIy3Kpc’ (line 1 column 1 char 0)

Traceback:

File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)File "C:\Users\cy\LearnPython\BookWriter\BookWriterLevel_6.py", line 6, in <module>
    openai.api_key = st.secrets["API_KEY"]
                     ~~~~~~~~~~^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\secrets.py", line 305, in __getitem__
    value = self._parse(True)[key]
            ^^^^^^^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\secrets.py", line 204, in _parse
    secrets.update(toml.loads(secrets_file_str))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\toml\decoder.py", line 514, in loads
    raise TomlDecodeError(str(err), original, pos)

Online error:

TypeError: ‘Secrets’ object is not callable

Traceback:

File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)File "C:\Users\cy\LearnPython\BookWriter\BookWriterLevel_6.py", line 6, in <module>
    openai.api_key = st.secrets("API_KEY")
                     ^^^^^^^^^^^^^^^^^^^^^

Notes

When I originally developed the code, I had the API key right in the code at this point, though the tutorial I was copying noted that it was better to use environmental variables. The code worked just fine when the line read like this:

openai.api_key = “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”
(this api key is no longer valid; openai shut it down because I exposed it to GitHub, which is why I’m now trying to figure out how to set it secretly as an environmental variable)

I have created a secrets.toml file both locally and on the streamlit website in the Secrets section of the app. I know that the local file can be accessed locally because I tried printing the value. I just cannot get it to set this value as openai.api_key.

Links

Hey @CynthiaAtOnSchooler,

Thanks for sharing your question!

Yes, you should delete your secrets.toml file from GitHub. It sounds like you haven’t correctly formatted your secrets.toml file – can you share the contents of the file but remove the actual secrets?

1 Like

The contents of the secrets.toml file, with a bunch of the key redacted with x’s:

API_KEY = “sk-wqvhJxxxxxxxxxxxxxxxxxxxxxxxxxxxxIy3Kpc”

st.secrets acts like a dictionary, so the syntax should be:

openai.api_key = st.secrets["API_KEY"]

Assuming you defined API_KEY in the secrets.toml. Check Secrets management - Streamlit Docs for details and examples.

1 Like

I also tried it with brackets instead of parentheses and got the same error (but with brackets):

File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)File "C:\Users\cy\LearnPython\BookWriter\BookWriterLevel_6.py", line 6, in <module>
    openai.api_key = st.secrets["API_KEY"]
                     ~~~~~~~~~~^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\secrets.py", line 305, in __getitem__
    value = self._parse(True)[key]
            ^^^^^^^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\secrets.py", line 204, in _parse
    secrets.update(toml.loads(secrets_file_str))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\cy\AppData\Local\Programs\Python\Python311\Lib\site-packages\toml\decoder.py", line 514, in loads
    raise TomlDecodeError(str(err), original, pos)

Maybe the answer is in this section above the “traceback” box, when I run it locally:

Error parsing secrets file at C:\Users\cy\LearnPython\BookWriter.streamlit\secrets.toml

TomlDecodeError: invalid literal for int() with base 0: ‘sk-wqvhxxxxxxxxxxxxxxxxxxxxxx8hIy3Kpc’ (line 1 column 1 char 0)

Here is a clue Toml decoder error (TomlDecodeError).

Using apostrophes gets me the same TomlDecodeError you are seeing. Try writing your key as a string, that is, between single quotes (API_KEY = 'hello') or double-quotes (API_KEY = "hello"), not between left and right apostrophes (‘ ’) or backticks (``).


Edit: for future reference maybe

Unicode Name Is string?
U+0027 Apostrophe or single quote Yes
" U+0022 Quotation Mark or double quote Yes
U+2018 Left single quotation mark No
U+2019 Right single quotation mark No
` U+0060 Grave accent or backtick No
U+201C Left double quotation mark No
U+201D Right double quotation mark No

Well, for Pete’s sake. I had it in double-quotes (as you can see in the example I pasted in above where it says

API_KEY = “sk-wqvhJxxxxxxxxxxxxxxxxxxxxxxxxxxxxIy3Kpc”

but for the sake of thoroughness I decided to try it with no quotes, single quotes, and then back to double quotes. The first two didn’t work, but when I went back to double quotes it did!!!

One of them must have been something else masquerading as double quotes.

Thank you everyone for your help!

1 Like

Yeah, those were not double-quotes but left and right double quotation marks :melting_face:. Glad it worked! (I’ll add those to the table)

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.