ValueError
langchain.callbacks.streaming_stdout.ValueError
Stack trace
ValueError: Streaming callback not set. Please provide a streaming callback handler to receive tokens.
File "/path/to/langchain/callbacks/streaming_stdout.py", line 45, in on_llm_new_token
raise ValueError("Streaming callback not set.")
File "app.py", line 27, in <module>
response = llm(..., streaming=True) # triggers error Why it happens
LangChain's streaming LLM calls require a callback function or handler to process tokens as they stream in. If you enable streaming but do not provide a callback, LangChain cannot deliver the streamed tokens and raises this error to prevent silent failures.
Detection
Check if your LLM call uses streaming=True without passing a streaming callback or handler; logging or debugging will show this ValueError before your app hangs or silently fails.
Causes & fixes
Streaming enabled but no streaming callback handler provided
Pass a valid streaming callback handler such as StreamingStdOutCallbackHandler() or a custom callback to the LLM constructor or call.
Using LangChain LLM wrapper without configuring callbacks for streaming
Instantiate the LLM with callbacks=[StreamingStdOutCallbackHandler()] or your own callback list to handle streaming tokens.
Custom LLM wrapper disables or omits streaming callback setup
Ensure your custom wrapper or subclass properly forwards streaming callbacks to the underlying LangChain LLM client.
Code: broken vs fixed
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model_name="gpt-4o-mini", streaming=True)
response = llm("Hello") # ValueError: Streaming callback not set error here import os
from langchain_openai import ChatOpenAI
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "your_api_key_here")
llm = ChatOpenAI(model_name="gpt-4o-mini", streaming=True, callbacks=[StreamingStdOutCallbackHandler()]) # Added streaming callback
response = llm("Hello")
print(response) Workaround
Wrap the LLM call in try/except ValueError and catch the streaming callback error; then log a warning and fallback to non-streaming calls.
Prevention
Always configure streaming callbacks explicitly when enabling streaming in LangChain LLM calls to ensure tokens are processed and avoid runtime errors.