Using an API from inside Streamlit app

Hey! I recently tried using an API inside a streamlit app.
My only problem is that if I execute this API - as a standalone script (for example, from a jupyter notebook, or even a single python script.) then it works perfectly fine, without any issues, however if I call this api from a streamlit app - it returns an error.
The error is the following (returned by the api) :

  • ’ Some internal processing error occurred. Please give us some time, we are looking into ’

I know it says it is an internal server error, but trust me, every single time i run it as a standalone script, it works fine. Except when i run it from a streamlit web app. Why does this happen ? How can I possibly fix it ?

here’s the code that calls the api:

def call_api():
    import requests
    import json
    import streamlit as st
    payload = { some parameters that arent relevant in any way}
    print("executing....")
    with open(filename, 'rb') as f:
        r = requests.post('https://'api.endpoint...,
                          files={filename: f},
                          data=payload)
    st.markdown("Executed!")
    x = r.content.decode()

Does streamlit make some sort of a private environment that blocks communication from outside or what ? Its pretty annoying. This only happens if I’m using streamlit. Both python & streamlit are in the newest version

Hey @NoNeuronsNoStress,

Can you share what API is returning that error message?

1 Like

Hi, it does not matter what kind of api I use. So far anything i tried to run - which communicates with an outside endpoint - doesnt work.
However if this might help you then its just a OCR recognition api called SpaceOCR

Are you running the app locally or is it hosted with Streamlit Community Cloud?

I tried both and it doesn’t work in any of the cases. Also tried to run the streamlit code a different site hosting platform. It doesn’t work there either.

What happens if you remove the Streamlit pieces and just run the Python file?

def call_api():
    import requests
    import json
    payload = { some parameters that arent relevant in any way}
    print("executing....")
    with open(filename, 'rb') as f:
        r = requests.post('https://'api.endpoint...,
                          files={filename: f},
                          data=payload)
    x = r.content.decode()

then, it works perfectly fine - as long as there is no streamlit involved

I’m unable to reproduce this, unfortunately.

What type of error are you getting? When you change the API, what error do you get? I know you mentioned “Some internal processing error occurred. Please give us some time, we are looking into’”, but there should be an error code in addition to that, e.g. 400, 500, etc.

uh this is the output im getting (not sure which one is the error code):
{“OCRExitCode”:99,“IsErroredOnProcessing”
ErrorMessage":["E103: Some internal processing error occurred


If you want I could give you the entire code (its barely 50 lines), but only in private - i really dont want to share it here

If you switch to a different API, what’s the error message then?

ugh i really dont remember, it happend to me only once, but perhaps that was solved by something else, and I wasnt even using streamlit.

does streamlit block these requests that are communicating with an endpoint from outside or this is not supposed to happen ?

No, Streamlit doesn’t interfere with API requests. Many Streamlit apps make API calls.

It sounds like this issue is specific to the API you’re using, if you’re not able to reproduce the error with a different API.

Looking at the docs for this API, that error seems specific to a parsing error rather than some type of issue with your request being denied.

This thread also looks relevant (does the error go away if you specify a filetype?)

nope, I just tried specifying the filetype and it’s still the same error :frowning:

Just to clarify, you get a 200 response when you run the file after commenting out st.markdown("Executed!")?

well yes - to explain it simply : i get a 200 response back all the time when there is no streamlit involved.
if there is a streamlit app running when i send the request i always get back this error.

Can you share what the code looks like when Streamlit isn’t involved? Is it otherwise the exact same as the code snippet you shared?

yes the code is completely the same. I dont even have to specify the filetype it works fine without it.

Hey @NoNeuronsNoStress,

I wonder if you tried passing an explicit User-Agent within the headers of your request? This is how you’re identified when throwing HTTP requests. I can’t see why the request within a Streamlit app wouldn’t work (as @Caroline mentions this is something a lot of users leverage) but you may try that:

  • Copy your user agent, e.g. visit this website. It should look something like this:
Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
  • Pass it to your requests.post() call by doing:
r = requests.post("htps://...", ..., headers={"User-Agent": "Mozilla/5.0..."})

Hope that helps,

1 Like

Hey! I just tried it out and it still doesn’t work. I get back the same error.