Make `gr.State` optionally generic
Author: ExcellentAmericanEagleCreated Jun 30, 2025Updated Sep 11, 2026
Labelstoinvestigate
- I have searched to see if a similar issue already exists.
Is your feature request related to a problem? Please describe.
It's frustrating that gr.State isn't generic - because I can simply pass around references to gr.State and I can have no idea what's the type of value they store.
The only way I found, which is not the elegant IMO - is explicitly providing type annotations to parameters that derive state. E.g.
config_state = gr.State(Config(name="alex"))
# ...
def render_personal_details_tab(state: gr.State):
@gr.render(inputs=[config_state])
def render_name_textbox(config_value: Config):
gr.Textbox(value=config_value.name)
# ...Describe the solution you'd like
I suggest making gr.State optionally accept a type hint generic (like gr.State[Config]). It may be automatically inferred from the initial value to the constructor of gr.State:
config_state = gr.State(Config(name="alex"))
# ^ type hint would be `gr.State[Config]`Then functions can expect gr.State references that store specific type of state, aiding in type enforcement and repetitions:
def render_personal_details_tab(state: gr.State[Config]):
@gr.render(inputs=[config_state])
def render_name_textbox(config_value): # No type annotation required
gr.Textbox(value=config_value.name) # `.name` is a valid instance attribute!
# ...Source: gradio-app/gradio