Streamlit calls not working in callback #23

Closed
opened 2026-02-20 17:22:50 -05:00 by yindo · 1 comment
Owner

Originally created by @i-merge on GitHub (Feb 17, 2024).

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.

Example Code

# Define callback with a streamlit call
@st.cache_data(experimental_allow_widgets=True, persist=True) 
def custom_callback():
   response = st.text_input("Do you want to continue?")
   return response == "y"

def run(...):
   # Calling the callback in run works
   custom_callback()
   handler = HumanApprovalCallbackHandler(custom_callback)
   # Passing it to callbacks will result in an NoSessionContext() error
   response = app.stream(inputs, config={"callbacks": [handler]})

Error Message and Stack Trace (if applicable)

File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3887, in invoke
return self.bound.invoke(
^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3353, in invoke
return self._call_with_config(
^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1246, in _call_with_config
context.run(
File "lib/python3.11/site-packages/langchain_core/runnables/config.py", line 326, in call_func_with_variable_args
return func(input, **kwargs) # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3229, in _invoke
output = call_func_with_variable_args(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/runnables/config.py", line 326, in call_func_with_variable_args
return func(input, **kwargs) # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langgraph/prebuilt/tool_executor.py", line 60, in _execute
output = tool.invoke(tool_invocation.tool_input, config=config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/tools.py", line 210, in invoke
return self.run(
^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/tools.py", line 331, in run
run_manager = callback_manager.on_tool_start(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_core/callbacks/manager.py", line 1308, in on_tool_start
handle_event(
File "lib/python3.11/site-packages/langchain_core/callbacks/manager.py", line 262, in handle_event
raise e
File "lib/python3.11/site-packages/langchain_core/callbacks/manager.py", line 234, in handle_event
event = getattr(handler, event_name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/langchain_community/callbacks/human.py", line 57, in on_tool_start
if self._should_check(serialized) and not self._approve(input_str):
^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 212, in wrapper
return cached_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 240, in call
with spinner(message, cache=True):
File "versions/3.11.7/lib/python3.11/contextlib.py", line 137, in enter
return next(self.gen)
^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/streamlit/elements/spinner.py", line 56, in spinner
message = st.empty()
^^^^^^^^^^
File "lib/python3.11/site-packages/streamlit/elements/empty.py", line 70, in empty
return self.dg._enqueue("empty", empty_proto)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib/python3.11/site-packages/streamlit/delta_generator.py", line 530, in _enqueue
_enqueue_message(msg)
File "lib/python3.11/site-packages/streamlit/delta_generator.py", line 869, in _enqueue_message
raise NoSessionContext()
streamlit.errors.NoSessionContext

Description

Adding a callback to stream or invoke with a streamlit call inside will result in NoSessionContext error

System Info

langchain : 0.1.0
langgraph: 0.0.20
streamlit: 1.31.0

Originally created by @i-merge on GitHub (Feb 17, 2024). ### Checked other resources - [X] I added a very descriptive title to this issue. - [X] I searched the LangChain documentation with the integrated search. - [X] I used the GitHub search to find a similar question and didn't find it. - [X] I am sure that this is a bug in LangChain rather than my code. ### Example Code ```python # Define callback with a streamlit call @st.cache_data(experimental_allow_widgets=True, persist=True) def custom_callback(): response = st.text_input("Do you want to continue?") return response == "y" def run(...): # Calling the callback in run works custom_callback() handler = HumanApprovalCallbackHandler(custom_callback) # Passing it to callbacks will result in an NoSessionContext() error response = app.stream(inputs, config={"callbacks": [handler]}) ``` ### Error Message and Stack Trace (if applicable) File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3887, in invoke return self.bound.invoke( ^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3353, in invoke return self._call_with_config( ^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1246, in _call_with_config context.run( File "lib/python3.11/site-packages/langchain_core/runnables/config.py", line 326, in call_func_with_variable_args return func(input, **kwargs) # type: ignore[call-arg] ^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3229, in _invoke output = call_func_with_variable_args( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/runnables/config.py", line 326, in call_func_with_variable_args return func(input, **kwargs) # type: ignore[call-arg] ^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langgraph/prebuilt/tool_executor.py", line 60, in _execute output = tool.invoke(tool_invocation.tool_input, config=config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/tools.py", line 210, in invoke return self.run( ^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/tools.py", line 331, in run run_manager = callback_manager.on_tool_start( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_core/callbacks/manager.py", line 1308, in on_tool_start handle_event( File "lib/python3.11/site-packages/langchain_core/callbacks/manager.py", line 262, in handle_event raise e File "lib/python3.11/site-packages/langchain_core/callbacks/manager.py", line 234, in handle_event event = getattr(handler, event_name)(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/langchain_community/callbacks/human.py", line 57, in on_tool_start if self._should_check(serialized) and not self._approve(input_str): ^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 212, in wrapper return cached_func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 240, in __call__ with spinner(message, cache=True): File "versions/3.11.7/lib/python3.11/contextlib.py", line 137, in __enter__ return next(self.gen) ^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/streamlit/elements/spinner.py", line 56, in spinner message = st.empty() ^^^^^^^^^^ File "lib/python3.11/site-packages/streamlit/elements/empty.py", line 70, in empty return self.dg._enqueue("empty", empty_proto) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "lib/python3.11/site-packages/streamlit/delta_generator.py", line 530, in _enqueue _enqueue_message(msg) File "lib/python3.11/site-packages/streamlit/delta_generator.py", line 869, in _enqueue_message raise NoSessionContext() streamlit.errors.NoSessionContext ### Description Adding a callback to stream or invoke with a streamlit call inside will result in NoSessionContext error ### System Info langchain : 0.1.0 langgraph: 0.0.20 streamlit: 1.31.0
yindo closed this issue 2026-02-20 17:22:50 -05:00
Author
Owner

@hinthornw commented on GitHub (Apr 14, 2025):

Gonna close as stale

@hinthornw commented on GitHub (Apr 14, 2025): Gonna close as stale
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#23