Stream a generator, iterable, or stream-like sequence to the app.
st.write_stream iterates through the given sequences and writes all chunks to the app. String chunks will be written using a typewriter effect. Other data types will be written using st.write.
Function signature[source] | |
---|---|
st.write_stream(stream) | |
Parameters | |
stream (Callable, Generator, Iterable, OpenAI Stream, or LangChain Stream) | The generator or iterable to stream. If you pass an async generator, Streamlit will internally convert it to a sync generator. Note To use additional LLM libraries, you can create a wrapper to manually define a generator function and include custom output parsing. |
Returns | |
(str or list) | The full response. If the streamed output only contains text, this is a string. Otherwise, this is a list of all the streamed objects. The return value is fully compatible as input for st.write. |
Example
You can pass an OpenAI stream as shown in our tutorial, Build a basic LLM chat app. Alternatively, you can pass a generic generator function as input:
Tip
If your stream object is not compatible with st.write_stream
, define a wrapper around your stream object to create a compatible generator function.
for chunk in unsupported_stream:
yield preprocess(chunk)
For an example, see how we use Replicate with Snowflake Arctic in this code.
Still have questions?
Our forums are full of helpful information and Streamlit experts.