Please help... I'm having error connecting my app to OpenAI it's an issue with secret key

Help please…

I cannot seem to share my app on Streamlit

I have set the keys in TOML format in the advanced settings of the app on share.streamlit.io

When I deploy I am getting this error

KeyError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/home/appuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)File "/app/promptengineeringspecialists/app.py", line 80, in <module>
    openai.api_key = st.secrets["OPENAI_KEY"]
                     ~~~~~~~~~~^^^^^^^^^^^^^^File "/home/appuser/venv/lib/python3.11/site-packages/streamlit/runtime/secrets.py", line 311, in __getitem__
    raise KeyError(_missing_key_error_message(key))

In my app I am using secrets.py instead of secrets.toml but that should not matter.

My code is setting to this

Set OpenAI API key

openai.api_key = st.secrets[“OPENAI_KEY”]

and advanced settings is set to this…

[secrets]
OPENAI_KEY = "sk-TVZiJeHVorKKceV2ng2HT…

I have the full key set obviously

I have verified the key is active on openai

Please help

thx

Check out the documentation for st.secrets.

If you put your secrets in a section like [secrets] you have an extra layer of the dictionary to navigate.

this = "foo"

[secrets]
that = "bar"
import streamlit as st

st.secrets['this']
st.secrets['secrets']['that']
2 Likes

Thx for offering a solution

I have this locally in my .streamlit/secrets.toml/

[secrets]
OPENAI_KEY = “sk-TVZiJeHVorKKceV2ng2HT3BlbkFJ9Xj5ls94…CMP7”
service_account_key = “C:/Prompts/urlarticles/urlarticles-d2…d84f.json”
spreadsheet_id = “194CCm6M-Cy4-rQSrow…2IKdEgr_yilpvAzQ”

in the advanced/secrets settings within my app on streamlit i have replaced the url with the longstring json

[secrets]
OPENAI_KEY = “sk-TVZiJeHVorKKceV2ng2HT3Bl…ls94j6hVRs7WCMP7”
spreadsheet_id = “194CCm6M-Cy4-rQSrow…c2IKdEgr_yilpvAzQ”

[secrets.service_account]
type = “service_account”
project_id = “urlarticles”
private_key_id = “d27f5203d84fb3d2aa85a…0b5f574867d4”
private_key = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0bwTjGBEF2c0Z\nOZaBqibhkPYN8kCaszOFKnHHG1ASdHbw5/iwhBfIqb7+YPqjAWiGtpE4epJOoKFa\nO6PNQAuEzRw6P6acbzi4c2JBEZkGOdy3KdiMf/NOsqsHpZ4us9

Are you saying you still have a key error? If yes, what is your updated code?

correct… I still cannot connect

my code is on a private repo I do not want to share

I have opted to remove this [secrets] from the toml leaving the connector in my code untouched

Maybe I am going about things wrong in the first place

My app is iterating through a list of urls i provide through google sheets. This means the app is connecting to google sheets and iterated through row data. The row data are urls to web pages where it scrapes content from the body using beautifulsoup. It then uses the scraped data to rewrite content based on a prompt I have embedded in the code. The output is placed in a new sheet.

I am able to run this locally no problem. What I am unable to do is to get streamlit to run the app using the share.streamlit.io service

I have been trying for weeks to connect. I have many apps that do different tasks for me but all share a common thread in that they are iterating through rows of data in a google sheet, parse it and replace it in a new sheet using gpt to rewrite the data. In some cases I am merely using apps script with functions like combine, find and replace, match and so on…

My goal is to publish all of these apps I have locally to the web and charge a subscription fee to access them. I was hoping to use streamlit for my hosting service.

Can you help me please, at this stage I am pulling hair out of my head. I’m sure its a simple fix I just have not been able to find the right combination.

Thanks for listening :wink:

Cheers,

J

This was your original code that I was asking about. In your original post, you said you had a key error, which was expected because it wasn’t calling to the [secrets] section within st.secrets as needed per your description of your secrets file.

What error are you getting now? Are you still getting a key error or do you get a different error now?

You mentioned OpenAI keys, but how are you handling your Google credentials then? Is it OpenAI or Google that you are failing to connect to through Community Cloud?

Locally I can run app no problem

It may be that I am unable to connect to either. I have never successfully deployed to stream lit

ATM I am trying the gsheetsdb method but am still getting errors

What errors are you getting, specifically? How are you trying to connect to Google?

I’m having the same issue. And it also deletes my openai key because openai determined that it was leaked. Any solution ?

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