Sub topic 13
Calling the Chat App from Code with gradio_client
This chapter assumes you can run a gr.ChatInterface app locally, know the (message, history) function contract, and have used pip to install packages. It covers how to call a running chat app from another Python program.
Why call the app from code
A running Gradio app is also an HTTP service. Every event handler is exposed as a named API endpoint. A script, a test, or another service can send a message and read the reply without opening the browser. The gradio_client package wraps this in a small Python API.
Install it with pip install gradio_client. It is also installed as a dependency of gradio.
Start a chat app to call
Save this as app.py and run python app.py. By default Gradio serves on port 7860.
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()Send a message
A gr.ChatInterface exposes an endpoint named /chat. In a second terminal, run this client script.
from gradio_client import Client
client = Client("http://127.0.0.1:7860/")
result = client.predict("Hello!", api_name="/chat")
print(result)Client takes the URL of the running app. predict sends the inputs in order and waits for the final output. Here the only input is the message, so the script prints You said: Hello!.
Discover the endpoints
Do not guess the endpoint name or its parameters. Ask the client.
from gradio_client import Client
client = Client("http://127.0.0.1:7860/")
client.view_api()view_api() prints each named endpoint with its parameters and return types. The output also shows the Python call you can copy.
If your app uses additional_inputs, the endpoint takes those values too. They follow the message, in the same order as the components. For the system prompt and temperature example from the LLM chapter, the call looks like this:
result = client.predict(
"Hello!",
"You are a pirate.",
0.3,
api_name="/chat",
)History is kept on the client side
The chat endpoint does not store a conversation for you. In the browser, the page keeps the history and sends it with each request. With gradio_client, you send only the new message, and the app sees an empty history each time. Two predict calls are therefore two unrelated conversations.
If your function needs earlier turns, store them in your own program and keep them there. Alternatively, move the memory to the server side, for example with gr.State in a gr.Blocks app.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Connection refused | The app is not running, or the port differs | Start the app and check the URL it prints |
| Endpoint not found | Wrong `api_name` | Run `client.view_api()` and use the listed name |
| Wrong number of arguments | Missing values for `additional_inputs` | Pass one value per extra component, in order |
| Authentication error | The app was launched with `auth` | Pass `auth=("user", "password")` to `Client` |
Streaming results with submit
predict blocks until the reply is complete. For a streaming chat function, use submit, which returns a job right away.
from gradio_client import Client
client = Client("http://127.0.0.1:7860/")
job = client.submit("Tell me a story", api_name="/chat")
for partial in job:
print(partial)
print(job.result())Iterating over the job yields the outputs as the server produces them. Each item is the full reply so far, matching how yield works in the chat function. job.result() returns the final output.
Exercise
You call client.predict("My name is Sam", api_name="/chat") and then client.predict("What is my name?", api_name="/chat"). The chat function uses history. Why does the second reply not know the name, and what are two ways to fix it?
Show answer
Each predict call sends only the new message, so the server sees an empty history. The history lives in the browser in normal use, and the client does not send it. Fix it by keeping the conversation in your own program and building the full context yourself (for example, including earlier turns in the message text or in a custom endpoint), or by storing the conversation on the server, for example with gr.State in a gr.Blocks app.
Exercise
Which method do you call to list a running app's endpoints and parameters, and which method lets you read partial replies from a streaming chat function?
Show answer
client.view_api() lists the endpoints and parameters. client.submit(...) returns a job you can iterate over to receive partial replies.