Sub topic 3
Your first chat UI with gr.ChatInterface
This chapter assumes you can write Python functions, install packages with pip, and run a script from a terminal. It also assumes you know what a chat API does: you send messages and get a reply. Install Gradio with pip install gradio.
What gr.ChatInterface does
gr.ChatInterface is a high-level class that builds a full chat page from one function. It gives you a message box, a send button, a scrolling conversation, and the wiring between them. You supply only the function that produces a reply.
The function takes two arguments. message is the text the user just sent. history is the list of earlier turns. Whatever string the function returns is shown as the assistant's reply.
A minimal app
import gradio as gr
def respond(message, history):
return f"You said: {message}"
demo = gr.ChatInterface(fn=respond, type="messages")
if __name__ == "__main__":
demo.launch()Save this as app.py and run python app.py. Gradio starts a local web server and prints a URL, usually http://127.0.0.1:7860. Open it in a browser, type a message, and the app echoes it back.
The type="messages" argument tells Gradio to pass history as a list of dictionaries with role and content keys. This setting applies to Gradio 5.x. Newer versions may use this format by default. The next chapter covers the history format in detail.
What happens on each message
Gradio keeps the conversation in the browser session and sends the full history to your function on every call. Your function does not need to store anything between calls. This also means the function is stateless by default, so any memory comes from the history argument.
Using the history
Because history holds every earlier turn, your function can use it right away. This version reports how many messages came before the current one.
import gradio as gr
def respond(message, history):
return f"You said: {message} (earlier messages: {len(history)})"
demo = gr.ChatInterface(fn=respond, type="messages")
if __name__ == "__main__":
demo.launch()On the first message the count is 0. After one full exchange it is 2, because the user turn and the assistant turn each count as one message.
Common errors
- Wrong argument count.
respondmust accept at leastmessageandhistory. A function with one parameter raises aTypeErrorwhen the first message is sent. - Returning a non-string. Return a string for plain text replies. Returning
Noneshows an empty reply. - Port already in use. If another process holds port 7860, Gradio picks the next free port and prints it. Read the URL in the terminal output instead of assuming 7860.
- Code reload. Editing
app.pydoes not update a running app. Stop it with Ctrl+C and start it again, or rungradio app.pyto get auto-reload during development. - Missing main guard. Without
if __name__ == "__main__":, importing the file starts a server. Keep the guard if other code might importdemo.
Summary of the contract
| Part | Meaning |
|---|---|
| `message` | The latest user input, as a string |
| `history` | Earlier turns, as role and content dictionaries |
| return value | The assistant reply shown in the chat |
| `type="messages"` | Selects the dictionary history format in Gradio 5.x |
Exercise
You change the text in respond from "You said" to "I heard" and save the file, but the browser still shows "You said". What is the most likely cause, and what are two ways to fix it?
Show answer
The server is still running the old code, because Gradio does not reload a script that was started with python app.py. Fix it by stopping the process with Ctrl+C and running it again, or by starting the app with gradio app.py, which reloads on file changes.
Exercise
Modify respond so it returns "This is the first message." when history is empty and "We have talked before." otherwise.
Show answer
def respond(message, history): if not history: return "This is the first message." return "We have talked before."