gr.Chatbot and gr.ChatInterface.
The ChatMessage dataclass
Every element of the chatbot value is a dictionary ofrole and content keys. You can always use plain Python dictionaries to add new values to the chatbot but Gradio also provides the ChatMessage dataclass to help you with IDE autocompletion. The schema of ChatMessage is as follows:
metadata key, which accepts a dictionary. If this dictionary includes a title for the message, it will be displayed in a collapsible accordion representing a thought. It’s that simple! Take a look at this example:
title, the dictionary provided to metadata can take several optional keys:
log: an optional string value to be displayed in a subdued font next to the thought titleduration: an optional numeric value representing the duration of the thought/tool usage, in seconds. Displayed in a subdued font inside parentheses next to the thought titlestatus: if set to"pending", a spinner appears next to the thought title and the accordion is initialized open. Ifstatusis"done", the thought accordion is initialized closed. Ifstatusis not provided, the thought accordion is initialized open and no spinner is displayedidandparent_id: if these are provided, they can be used to nest thoughts inside other thoughts
gr.Chatbot and gr.ChatInterface to display tool use or thinking UIs.
Building with agents
A real example using transformers.agents
We’ll create a Gradio application with a simple agent that has access to a text-to-image tool. We’ll start by importing the necessary classes from transformers and gradio:A real example using LangChain agents
We’ll create a UI for a LangChain agent that has access to a search engine. We’ll begin with imports and setting up the LangChain agent. Note that you’ll need an.env file with the following environment variables set:
Building with thinking LLMs
The Gradio Chatbot can natively display intermediate thoughts of a thinking LLM. This makes it perfect for creating UIs that show how an AI model “thinks” while generating responses. Below guide will show you how to build a chatbot that displays Gemini AI’s thought process in real-time.A real example using Gemini 2.0 Flash Thinking API
Let’s create a complete chatbot that shows its thoughts and responses in real-time. We’ll use Google’s Gemini API for accessing Gemini 2.0 Flash Thinking LLM and Gradio for the UI. We’ll begin with imports and setting up the Gemini client. Note that you’ll need to acquire a Google Gemini API key first:- Displays the model’s thoughts in a collapsible section
- Streams the thoughts and final response in real-time
- Maintains a clean chat history
Building with citations
The Gradio Chatbot can display citations from LLM responses, making it perfect for creating UIs that show source documentation and references. This guide will show you how to build a chatbot that displays Claude’s citations in real-time.A real example using Anthropic’s Citations API
Let’s create a complete chatbot that shows both responses and their supporting citations. We’ll use Anthropic’s Claude API with citations enabled and Gradio for the UI. We’ll begin with imports and setting up the Anthropic client. Note that you’ll need anANTHROPIC_API_KEY environment variable set:
- Supports both plain text and PDF documents for Claude to cite from
- Displays citations in collapsible sections using our
metadatafeature - Shows source quotes directly from the given documents
metadata support, allowing us to create collapsible sections that keep the chat interface clean while still providing easy access to source documentation.
You now have a chatbot that not only responds to users but also shows its sources, creating a more transparent and trustworthy interaction. See our finished Citations demo here.