Skip to main content
State management allows your Gradio applications to remember information between user interactions. This is essential for building applications that need to track conversation history, maintain user sessions, or accumulate data over multiple steps.

What is state?

In Gradio, “state” refers to data that persists across multiple function calls within a user session. Without state, each interaction is independent - the application has no memory of previous inputs or outputs. State in Gradio is:
  • Session-specific: Each user has their own isolated state
  • Temporary: State resets when the user refreshes the page or their session expires
  • Invisible: State components don’t appear in the UI
  • Flexible: Can store any Python object (lists, dicts, custom objects)

The State component

The simplest way to manage state is with the State component:
Key points:
  • Initialize state with a default value: gr.State(value=0)
  • Include state in both inputs and outputs to update it
  • Return the updated state from your function

Common state patterns

Accumulating chat history

Maintain conversation context in a chatbot:
The chatbot component itself acts as state, storing the message history.

Multi-step workflows

Collect information across multiple interactions:

Maintaining counters

Track user actions or iterations:

State in Interface

When using gr.Interface, you can use the special "state" string shortcut:
When using Interface with state, there must be exactly one state input and one state output.

Complex state objects

State can hold any Python object, including custom classes:

State vs component values

When to use State

Use gr.State when you need to:
  • Store data that shouldn’t be visible to users
  • Maintain large data structures (models, datasets)
  • Track internal application state
  • Keep intermediate computation results

When to use regular components

Use regular components as “state” when:
  • Users need to see the persisted value
  • The state is simple and fits a component type
  • You want users to potentially edit the state

Initializing state

Default values

Set initial state when creating the component:

Dynamic initialization

Initialize state based on computations:

Clearing state

Reset state to initial values:
Or use the ClearButton:

State limitations

Session isolation

Each user session has independent state:
User A clicking “Increment” doesn’t affect User B’s counter.
If you need to share data across all users, use global Python variables or a database. However, be careful with thread safety!

Not persistent across refreshes

State resets when the page refreshes:
For true persistence, use external storage (databases, files, etc.).

Memory considerations

State is stored in server memory:
Avoiding storing very large objects in State, especially with many concurrent users. Consider using file storage or databases for large data.

Advanced patterns

Conditional state updates

Update state only under certain conditions:

State with multiple functions

Pass state through a chain of operations:

See also

  • Components - State is a special component
  • Events - How to update state via events
  • Blocks - Using state in custom layouts
  • ChatInterface - Built-in chat history state management