streamlit: Improve "missing ReportContext" threading error

If you create a thread from your Streamlit script, and call streamlit functions from within that thread, you need to attach a ReportContext to it or else your st.foo() calls won’t do anything, and you’ll get a warning message that looks something like this:

“Thread ‘Thread-7’: missing ReportContext”

We should improve the error message to tell the user how to fix this! (We may just want an FAQ entry that we can link to from the error message.)

(The fix is to use add_report_ctx on the thread immediately after it’s created:)

from streamlit.ReportThread import add_report_ctx

thread = threading.Thread(target=...)
add_report_ctx(thread)
thread.start()

Related discussion: https://discuss.streamlit.io/t/how-to-run-a-subprocess-programs-using-thread-inside-streamlit/2440

(2022 edit: please see my comments here - this is an internal Streamlit API and has some major caveats. In particular, please do not call st.foo commands from other threads - at least in production code that needs to be stable.)


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you’d like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

Views

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 44
  • Comments: 34 (7 by maintainers)

Most upvoted comments

For anyone stumbling over this, it has been renamed to streamlit.report_thread.add_report_ctx(thread).

For version 1.12.0:

streamlit.runtime.scriptrunner import get_script_run_ctx

Also you can do

import streamlit as st
from threading import Thread
from streamlit.runtime.scriptrunner import add_script_run_ctx

def target():
    st.text("thread")

t = Thread(target=target)
add_script_run_ctx(t)

@valkenburg-prevue-ch Works like a charm, thanks! I’m using streamlit with Langchain and was running into this error due to Langchain spinning up various threads. Instead of having to modify the Langchain internals, I could just wrap the on_llm_new_token of my callback with your decorator and everything works right away 🚀

Can you share code? I’m trying the same but running into the same error.

I made a child class from OfficialStreamlitCallbackHandler and wrapped it with the above decorator:

class custom_callback(OfficialStreamlitCallbackHandler):
    @with_streamlit_context
    def on_llm_new_token(self, token, **kwargs):
        st.write(token)

No luck.

Hi, try my solution:

import inspect

from typing import Callable, TypeVar
from streamlit.runtime.scriptrunner import add_script_run_ctx, get_script_run_ctx
from streamlit.delta_generator import DeltaGenerator
from langchain_community.callbacks import StreamlitCallbackHandler

T = TypeVar('T')

def get_streamlit_cb(parent_container: DeltaGenerator):
    def decor(fn: Callable[..., T]) -> Callable[..., T]:
        ctx = get_script_run_ctx()
        def wrapper(*args, **kwargs) -> T:
            add_script_run_ctx(ctx=ctx)
            return fn(*args, **kwargs)
        return wrapper

    st_cb = StreamlitCallbackHandler(parent_container=parent_container)

    for name, fn in inspect.getmembers(st_cb, predicate=inspect.ismethod):
        if name.startswith('on_'):
            setattr(st_cb, name, decor(fn))

    return st_cb
# now you can do something with streamlit callback
st_cb = get_streamlit_cb(st.container())

Referring https://github.com/streamlit/streamlit/issues/1326#issuecomment-1220534436, I found the following.

It does work (script by @royassis ):

import streamlit as st
from threading import Thread
from streamlit.runtime.scriptrunner import add_script_run_ctx

def target():
    st.text("thread")

t = Thread(target=target)
add_script_run_ctx(t)
t.start()

It does not work (added time.sleep(1)):

import time
import streamlit as st
from threading import Thread
from streamlit.runtime.scriptrunner import add_script_run_ctx

def target():
    time.sleep(1)
    st.text("thread")

t = Thread(target=target)
add_script_run_ctx(t)
t.start()

I wonder if it relates to:

Adding the call to add_script_run_ctx removes the error Thread 'Dummy-9': missing ScriptRunContext, but it does not update the window.

I’m using the current latest version: 1.23.1

Here’s my donation to the list of solutions:

"""Implements with_streamlit_context.

Created by Wessel Valkenburg, 2024-03-27.
"""
import threading
from typing import Any, TypeVar, cast

from streamlit.errors import NoSessionContext
from streamlit.runtime.scriptrunner.script_run_context import (
    SCRIPT_RUN_CONTEXT_ATTR_NAME,
    get_script_run_ctx,
)

T = TypeVar("T")


def with_streamlit_context(fn: T) -> T:
    """Fix bug in streamlit which raises streamlit.errors.NoSessionContext."""
    ctx = get_script_run_ctx()

    if ctx is None:
        raise NoSessionContext(
            "with_streamlit_context must be called inside a context; "
            "construct your function on the fly, not earlier."
        )

    def _cb(*args: Any, **kwargs: Any) -> Any:
        """Do it."""

        thread = threading.current_thread()
        do_nothing = hasattr(thread, SCRIPT_RUN_CONTEXT_ATTR_NAME) and (
            getattr(thread, SCRIPT_RUN_CONTEXT_ATTR_NAME) == ctx
        )

        if not do_nothing:
            setattr(thread, SCRIPT_RUN_CONTEXT_ATTR_NAME, ctx)

        # Call the callback.
        ret = fn(*args, **kwargs)

        if not do_nothing:
            # Why delattr? Because tasks for different users may be done by
            # the same thread at different times. Danger danger.
            delattr(thread, SCRIPT_RUN_CONTEXT_ATTR_NAME)
        return ret

    return cast(T, _cb)

Decorate your callback just before passing it to whatever function, to make sure that the context exists and is the right context for this session.

if user_input:
    @with_streamlit_context
    def my_callback(*args):
        report_progress(*args)

    do_work(callback=my_callback)

This function checks if the thread has no context, and if so, adds it and then removes it again after the callback, as if nothing ever happened. This is to safeguard against the hypothetical situation where the same thread is re-used in another user’s session.

Do not do anything async in your callback, though!

For version 1.12.0:

streamlit.runtime.scriptrunner import get_script_run_ctx

I’m getting the same error using joblib’s Parallel and delayed. so I have no control of the threads creation. Any Ideas to workaround this ?

For version 1.10 : from streamlit.scriptrunner.script_run_context import get_script_run_ctx

For anyone stumbling over this, it has been renamed to streamlit.report_thread.add_report_ctx(thread).

As of Streamlit version 1.4.0, this cannot be imported successfully anymore, with the error of `ModuleNotFoundError", any help?

But I don’t see this “missing ReportContext” anymore without using the add_report_ctx() for my MQTT client connected with the paho-mqtt library. So I guess this is not needed anymore?

UPDATE: I actually just realized that the MQTT client did not work properly. Now I see missing ScriptRunContext instead of missing ReportContext. And I found that they have changed the module name and function name, so from now on they need to be imported as such:

from streamlit.script_run_context import add_script_run_ctx

instead of the previous

from streamlit.report_thread import add_report_ctx

The title of this issue may need to be changed to missing ScriptRunContext to match recent code.


IMO if a API is designed to be called from script thread, calling it from other threads should be an explicit error like “missing ScriptRunThread. Please ensure you are calling from a script thread” , rather than the current warning and ignore behavior.

@ging-dev your solution worked! I used it in StreamlitCallbackHandler_example/st_callable_util.py if anyone is trying to use LangGraph with Streamlit you can now invoke it like this or below and you won’t get missing ReportContext threading error.

graph_runnable.invoke({"messages": HumanMessage(content="write me a story about Harry Potter in Hogsmeade")},
                      config={"callbacks" :[get_streamlit_cb(st.empty())]})

awesome work!

@valkenburg-prevue-ch Works like a charm, thanks! I’m using streamlit with Langchain and was running into this error due to Langchain spinning up various threads. Instead of having to modify the Langchain internals, I could just wrap the on_llm_new_token of my callback with your decorator and everything works right away 🚀

Can you share code? I’m trying the same but running into the same error.

I made a child class from OfficialStreamlitCallbackHandler and wrapped it with the above decorator:

class custom_callback(OfficialStreamlitCallbackHandler):
    @with_streamlit_context
    def on_llm_new_token(self, token, **kwargs):
        st.write(token)

No luck.

I thought I’d add my own bit of sugar to the mix with an example that updates a progress bar asynchronously using a with statement and @valkenburg-prevue-ch’s with_streamlit_context decorator:

from typing import Callable
import streamlit as st
import threading
import time

# Import from wherever you are keeping this function
from .session import with_streamlit_context


class AsyncProgressBar:
    """
    A context manager that displays a progress bar asynchronously.

    Args:
        cb (Callable[..., tuple[float, str]]): The callback function that returns the progress percentage and text.
        initial_text (str): The initial text to display on the progress bar.
        update_interval (float): The interval at which to update the progress bar. 
    """

    def __init__(self, cb: Callable[..., tuple[float, str]], initial_text: str = '', update_interval_secs: float = 1):
        self.cb: Callable[..., tuple[float, str]] = cb
        self.initial_text: str = initial_text
        self.update_interval_secs: float = update_interval_secs
        self.running = False
        self.thread: threading.Thread

    def __enter__(self) -> 'AsyncProgressBar':
        """
        Start the progress bar when you enter the `with` context.

        Returns:
            AsyncProgressBar: The current instance.
        """
        bar = st.progress(0, self.initial_text)
        self.running = True

        @with_streamlit_context
        def update_progress() -> None:
            """
            Update the progress bar asynchronously.
            """
            try:
                while self.running:
                    perc, text = self.cb()
                    bar.progress(perc, text)
                    time.sleep(self.update_interval_secs)

                perc, text = self.cb()
                bar.progress(perc, text)
            except KeyboardInterrupt:
                self.stop()
                raise

        self.thread = threading.Thread(target=update_progress)
        self.thread.start()
        return self

    def __exit__(self, *args) -> None:
        """
        Stop the progress bar when you exit the `with` context.
        """
        self.stop()

    def stop(self) -> None:
        """
        Stop the progress bar.
        """
        if self.thread.is_alive():
            self.running = False
            self.thread.join()

Example

time_limit: float = 10
start_time: float = time.time()

def update_progress() -> tuple[float, str]:
    elapsed = min(time.time() - start_time, time_limit)
    time_remaining = time_limit - elapsed
    perc_progress = elapsed / time_limit
    text = f'{perc_progress * 100:.0f}% | Time remaining: {time_remaining:.0f}s'
    return perc_progress, text

def do_work():
    time.sleep(time_limit)

with AsyncProgressBar(update_progress, initial_text='0%'):
    do_work()

st.success('All done!')

Demo

Screen Recording 2024-04-25 at 16 15 18

@valkenburg-prevue-ch Works like a charm, thanks! I’m using streamlit with Langchain and was running into this error due to Langchain spinning up various threads. Instead of having to modify the Langchain internals, I could just wrap the on_llm_new_token of my callback with your decorator and everything works right away 🚀

I think it would be really helpful to add the add_script_run_ctx method to the __init__.py file instead of chasing its absoute path in each new version and causing backward incompatibilities !

So everyone can just do : from streamlit import add_script_run_ctx

Does add_script_run_ctx(t) have to be called immediately after the thread is created?

I’m working with an asynchronous library that works via callback functions. I’m hoping that these functions, when called, can update the browser window. However, the callbacks appear to be called from a “dummy” thread. I’ve tried to do the following:

    output_box = st.empty()
    ctx = get_script_run_ctx()
    def callback(event):
        add_script_run_ctx(threading.current_thread(), ctx)
        output_box.write(event.text)

    # Not the actual code, but establishes the idea
    model = create_model(callback=callback)

Adding the call to add_script_run_ctx removes the error Thread 'Dummy-9': missing ScriptRunContext, but it does not update the window.

I assume there is something wrong with what I’m doing here, but I think I don’t quite understand well enough how streamlit works, and why this doesn’t work. Can someone help me to understand, or provide a workaround?

For everybody comming from a search engine: In Streamlit 1.3.1 and below ist was

import streamlit.report_thread as ReportThread
st.report_thread.add_report_ctx(thread)

and now for streamlit 1.4.0 and above it has to be updated to

from streamlit.script_run_context import add_script_run_ctx
st.script_run_context.add_script_run_ctx(thread)