Sub topic 10
Building a custom layout with gr.Blocks and gr.Chatbot
This chapter assumes you can run a gr.ChatInterface app and that you know the messages-style history format: a list of dictionaries with role and content keys. You should also be comfortable with Python functions and a little event-driven programming.
Why move to Blocks
gr.ChatInterface is a ready-made layout with one chatbot, one textbox, and a fixed wiring between them. gr.Blocks gives you the parts instead. You place components yourself, and you connect them with event listeners. Use it when you need extra panels, buttons, side-by-side components, or a different order of steps.
The trade-off is that ChatInterface handles history for you, and in Blocks you handle it yourself.
The pieces
gr.Blocksis a context manager. Components created inside it are laid out top to bottom by default.gr.Chatbotdisplays the conversation. Withtype="messages", its value is a list of{"role": ..., "content": ...}dictionaries.gr.Textboxcollects the user's input.- Event listeners such as
.submit()call a Python function with input components' values and write the return values to output components.
A two-step chat
The app below uses two chained steps. The first step adds the user message and clears the textbox. The second step adds the assistant reply.
import gradio as gr
def bot_reply(history):
last = history[-1]["content"]
history.append({"role": "assistant", "content": f"You said: {last}"})
return history
with gr.Blocks() as demo:
chatbot = gr.Chatbot(type="messages")
msg = gr.Textbox(placeholder="Type a message and press Enter")
def add_user_message(message, history):
return "", history + [{"role": "user", "content": message}]
msg.submit(add_user_message, [msg, chatbot], [msg, chatbot]).then(
bot_reply, chatbot, chatbot
)
if __name__ == "__main__":
demo.launch()Read the wiring in msg.submit(...) as inputs, then outputs:
[msg, chatbot]as inputs means Gradio passes the textbox text and the current history toadd_user_message.[msg, chatbot]as outputs means the two returned values go back to the same two components. The empty string clears the textbox, and the new list updates the display..then(...)runsbot_replyafter the first step finishes. It reads the chatbot and writes the chatbot.
Splitting the work this way means the user's message appears immediately, before the slow part of the reply starts.
Common errors
- Format mismatch. If
Chatbothas notype="messages"in Gradio 5.x, it expects the older tuple format. Your dictionaries then cause an error or display wrongly. Newer versions may default to messages. - Mutating the wrong list. Returning
history + [...]creates a new list. Appending in place also works, but be consistent about which one you do. - Forgetting
.then. If you put both steps in one function, the user's message only appears after the reply is finished. - Wrong input order. Gradio passes values to your function in the order of the inputs list. Swapping
[chatbot, msg]for[msg, chatbot]swaps your arguments.
When to stay with ChatInterface
If you only need extra settings such as a system prompt, additional_inputs on ChatInterface is shorter and less error-prone. Choose Blocks when the layout or the event flow is the real requirement.
Exercise
In the example, why does the first listener return an empty string as its first value?
Show answer
The first output is the textbox, so the empty string clears it after the message is sent. The second returned value is the updated history, which goes to the chatbot.
Exercise
What does .then(bot_reply, chatbot, chatbot) do that putting everything in one function would not?
Show answer
It runs bot_reply as a separate step after add_user_message completes. The user's message shows in the chatbot right away, before the reply is computed.