Sub topic 2
The chat function: message and history in, reply out
This chapter assumes you can write Python functions and generators, run a script from a terminal, and have used a chat or LLM API. It covers the one function every Gradio chat app depends on, and the mistakes people most often make when writing it.
The contract
gr.ChatInterface does not know how to produce replies. You give it a function, and it calls that function each time the user sends a message. The function must accept at least two parameters, in this order:
message: the text the user just submitted, as a string.history: the earlier turns of the conversation, as a list.
It returns the assistant's reply. Gradio shows the returned value as the next assistant message and appends both turns to the conversation.
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()Gradio calls the function by position, so the parameter names are up to you. Keep both parameters even if you ignore history. A function with only message fails when Gradio passes two arguments.
What happens on each turn
The server does not keep the conversation for your function. The browser holds the conversation, and Gradio passes the history in again on every call. Your function is just a mapping from the new message and the earlier turns to a reply.
The shape of history
With type="messages", history is a list of dictionaries. Each has a role ("user" or "assistant") and a content value. The list is empty on the first turn.
import gradio as gr
def respond(message, history):
user_turns = sum(1 for turn in history if turn["role"] == "user")
return f"This is user message {user_turns + 1}. You said: {message}"
gr.ChatInterface(fn=respond, type="messages").launch()The current message is not in history. It arrives only as message. If you build a prompt for an LLM, you must append the new message yourself. Chapter-level details of the format come later in the tutorial.
Setting type="messages" matters in Gradio 5.x. Without it, older versions pass history as [user, assistant] pairs. Code that indexes turn["role"] then fails with a TypeError. Newer versions may use the messages format by default, but setting it explicitly keeps your code stable across versions.
Return values and generators
The function can return a string, as above. It can also be a generator that yields progressively longer strings, and Gradio replaces the displayed reply on each yield. That is how streaming works, and a later chapter covers it. For now, remember that returning and yielding both satisfy the contract.
Common errors
| Symptom | Likely cause |
|---|---|
| `TypeError: respond() takes 1 positional argument but 2 were given` | The function omits the `history` parameter. |
| `TypeError` or `KeyError` when reading `turn["role"]` | History arrives as pairs because `type="messages"` is missing. |
| The reply is empty or shows `None` | The function has no `return` on some code path. |
| The model ignores the latest question | The new `message` was never added to the prompt, since it is not in `history`. |
| The history grows without bound | Every turn is sent to the model, so long chats eventually exceed its context limit. |
Exercise
A teammate writes def respond(message): return message.upper() and passes it to gr.ChatInterface(fn=respond, type="messages"). The first message fails with a TypeError. What is wrong, and what is the smallest fix?
Show answer
Gradio calls the function with two positional arguments, message and history, but respond accepts only one. Add a second parameter, as in def respond(message, history): return message.upper(). The function can ignore history, but it must accept it.