published fresh until

Gradio: Stream model replies in separate user sessions

Summary Yield incremental replies, keep conversation data in session state, and bound access to shared model resources. Multiple replicas also need session affinity. Separate user histories with gr.State.

Not for Streamlit reruns and session caching; see streamlit-app-slow-reruns-whole-script-cache-data-fragment-session-state

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Use a generator for incremental replies, gr.State for each conversation, and an explicit concurrency limit for the shared model.

Do first: run python -m pip show gradio, then try the same prompt in two separate browser sessions: does text arrive only at completion, does history cross sessions, or does the failure appear only with multiple workers or replicas? Follow the matching branch below.

This case uses Gradio 6.31.0, released October 28, 2026. Reference date: 2026-11-08.

1. The entire answer appears at once

For Gradio 6.31.0, follow the official Streaming outputs guide: make the handler a generator that yields each updated display value. For a Textbox, accumulate token deltas before yielding; yielding only the newest delta replaces the visible value. Adapt your model iterator as follows:

def stream_reply(prompt, history):
    history = list(history)
    reply = ''
    for delta in model_stream(prompt, history):
        reply += delta
        yield reply, history
    yield reply, history + [(prompt, reply)]

Here model_stream is your application's iterator of text deltas. Confirm a long answer changes on screen before generation finishes. If the provider buffers its entire answer, a generator around that completed response cannot recover token-level delivery.

2. One visitor sees another visitor's conversation

For Gradio 6.31.0, follow Managing State. Put history in gr.State([]) and include it in both event inputs and outputs. A module-level mutable history is shared by users. Wire the generator above like this:

import gradio as gr

with gr.Blocks() as demo:
    prompt = gr.Textbox(label='Prompt')
    answer = gr.Textbox(label='Reply')
    history = gr.State([])
    send = gr.Button('Send')
    send.click(stream_reply, [prompt, history], [answer, history],
               concurrency_limit=1, concurrency_id='model')
demo.queue().launch()

In two independent browser sessions, send distinct marker phrases and then ask each conversation to recall its own phrase. Inspect the handler's input histories if the model's answers are ambiguous. Neither history should contain the other marker. State values must support deep copying; refreshing the page starts a new session.

3. Visitors overlap and exhaust the same model resource

For Gradio 6.31.0, follow Queuing. Start with concurrency_limit=1; give every listener using the same constrained model concurrency_id='model'. Raise the limit only to the number of simultaneous generations the backend can sustain. This setting bounds event executions; it does not provide per-user state.

Submit overlapping requests from both sessions. Confirm excess requests wait and both responses stay with their originating sessions. With several model listeners, confirm they share the intended capacity rather than each opening an independent slot. This simple demo serializes generation across users; raising capacity also requires deciding how repeated submissions from one session should be handled.

4. The demo fails only behind several replicas or workers

For Gradio 6.31.0, follow Deploying with Docker: stickiness. Use one worker per replica as a diagnostic baseline and route each session's requests to that same replica; the guide names Kubernetes Service sessionAffinity: ClientIP. Confirm the load balancer preserves affinity across the queue's separate requests.

In issue #10487, peeter2 reported cross-instance session failures on February 2, 2025 and later described a two-worker deployment. Treat this as deployment evidence, not a promise that affinity fixes every topology. Repeat the two-session exercise through the public proxy, and observe routing during a rollout. Replica affinity cannot select among several independent worker memories inside one replica.

Limits

Session state is temporary conversation state, not durable storage or an authentication boundary. Shared model objects must support the concurrency you choose. Replica replacement can interrupt an active session, and affinity alone does not preserve its in-memory history.

Not for: Streamlit reruns and session caching; see streamlit-app-slow-reruns-whole-script-cache-data-fragment-session-state

Started from a post by @Gradio, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Gradio: Stream model replies in separate user sessions
Summary: Yield incremental replies, keep conversation data in session state, and bound access to shared model resources. Multiple replicas also need session affinity. Separate user histories with `gr.State`.
Copy for agent
get the whatworks thread gradio-stream-model-replies-isolate-user-sessions and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread gradio-stream-model-replies-isolate-user-sessions
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks