Gradio Python Client: Submit a Remote Job and Check Its Status
Check api_name and parameters with view_api, submit a Job, then verify it with status, done, result and stop rules.
Task and deliverable · Prepare the input and environment · Mechanism and data flow
At a glance
This guide is for readers who call a remote Gradio app. The official documentation describes the Gradio Python client as a way to use any Gradio app as an API. The task is narrow: read the remote endpoint's contract, send work with submit, and use status, done and result to see how far the job got. Building chat pages, audio components and share access are outside this guide.
Task workflow
Endpoint contract: where api_name and parameters come from
The source says that after connecting you can call
Client.view_api()to see the available APIs. The "Use via API" link in the app footer shows the same information plus example usage, and its "API Recorder" turns UI interactions into client code. For the Whisper Space, the source lists the endpoint/predict, a requiredaudioparameter and a text return. It also saysapi_namemay be omitted when an app has only one named endpoint, and that it selects the endpoint when there are several. Before submitting, confirmapi_name, parameter names, types and required status from the view_api output instead of copying an example.Submitting and retrieving: submit does not block, result does
The source states that
.predict()waits for the job to finish before returning..submit()creates a Job object and returns immediately; the source code comment says this step is not blocking, and callingjob.result()is what blocks until the result is available. You can do other work in between. The source also offersresult_callbacks, which run after the job completes. Returning a Job from submit does not establish that remote processing has started or that output is ready.Status and completion: status reports queue state, not output correctness
job.status()returns a StatusUpdate object. The source lists these attributes:code,rank,queue_size,eta,successandtime, meaning the status code, the position in the queue, the total queue size, the estimated completion time, whether the job completed successfully, and when the status was generated. The source also says.done()returns a boolean for whether the job has completed. These fields describe running and queue state. The source does not say they judge whether the output content is right, so content needs its own check.Cancellation: distinguish unstarted jobs from iterative outputs
For ordinary non-iterative jobs, the source says
.cancel()can cancel jobs that are queued but not started. A job that has started returns False and is not cancelled; a job that has not started returns True and is removed from the queue. For jobs with iterative outputs, cancelling finishes the job when the current iteration ends. The source gives no way to cancel a started ordinary job, and this guide does not invent one.
Task and deliverable
This guide is for readers who call a remote Gradio app. The official documentation describes the Gradio Python client as a way to use any Gradio app as an API. The task is narrow: read the remote endpoint's contract, send work with submit, and use status, done and result to see how far the job got. Building chat pages, audio components and share access are outside this guide.
Prepare the input and environment
- Environment: the source says the lightweight gradio_client package installs from pip and lists Python 3.10 or higher. Its command is
pip install --upgrade gradio_client. A recent gradio install already includes gradio_client as a dependency, but the documentation reflects the latest client, so upgrade when unsure. - Target app and access: a public Space can be reached by name. A private Space needs a Hugging Face token passed as the
tokenparameter. An app that asks for a username and password needs both passed toauth. An app hosted elsewhere needs its full URL, including http:// or https://. You also need to know what input the endpoint accepts, and the source suggests general familiarity with Gradio input and output components. - Limits and cost: the source notes that a public Space may rate limit you if you send too many requests. Duplicating a Space needs a token or a Hugging Face CLI login. If the original Space uses GPUs, the private copy does too and your account is billed at the GPU price, with automatic sleep after 1 hour of inactivity. This guide does not require duplicating, so that choice is yours.
Mechanism and data flow
Endpoint contract: where api_name and parameters come from: The source says that after connecting you can call Client.view_api() to see the available APIs. The "Use via API" link in the app footer shows the same information plus example usage, and its "API Recorder" turns UI interactions into client code. For the Whisper Space, the source lists the endpoint /predict, a required audio parameter and a text return. It also says api_name may be omitted when an app has only one named endpoint, and that it selects the endpoint when there are several. Before submitting, confirm api_name, parameter names, types and required status from the view_api output instead of copying an example.
Submitting and retrieving: submit does not block, result does: The source states that .predict() waits for the job to finish before returning. .submit() creates a Job object and returns immediately; the source code comment says this step is not blocking, and calling job.result() is what blocks until the result is available. You can do other work in between. The source also offers result_callbacks, which run after the job completes. Returning a Job from submit does not establish that remote processing has started or that output is ready.
Status and completion: status reports queue state, not output correctness: job.status() returns a StatusUpdate object. The source lists these attributes: code, rank, queue_size, eta, success and time, meaning the status code, the position in the queue, the total queue size, the estimated completion time, whether the job completed successfully, and when the status was generated. The source also says .done() returns a boolean for whether the job has completed. These fields describe running and queue state. The source does not say they judge whether the output content is right, so content needs its own check.
Cancellation: distinguish unstarted jobs from iterative outputs: For ordinary non-iterative jobs, the source says .cancel() can cancel jobs that are queued but not started. A job that has started returns False and is not cancelled; a job that has not started returns True and is removed from the queue. For jobs with iterative outputs, cancelling finishes the job when the current iteration ends. The source gives no way to cancel a started ordinary job, and this guide does not invent one.
Workflow map
| Stage | What happens |
|---|---|
| Endpoint contract: where api_name and parameters come from | The source says that after connecting you can call Client.view_api() to see the available APIs. |
| Submitting and retrieving: submit does not block, result does | The source states that .predict() waits for the job to finish before returning. |
| Status and completion: status reports queue state, not output correctness | job.status() returns a StatusUpdate object. |
| Cancellation: distinguish unstarted jobs from iterative outputs | For ordinary non-iterative jobs, the source says .cancel() can cancel jobs that are queued but not started. |
Complete the task
Confirm the environment and prepare credentials:Check that Python meets the 3.10 or higher listed in the source, then install or upgrade gradio_client with the source command. Next pick credentials for the target app: token for a private Space, auth for an app with a login. Keep credentials out of code files. The source's duplicate example reads the token from the HF_TOKEN environment variable, and you can follow that habit. Nothing was installed and no account was used for this guide.
Read the endpoint contract:After connecting, call Client.view_api() or open the app's "Use via API" page. Record the api_name, each parameter's name and type, which are required, and the return type. The source says parameters with defaults can be omitted in the client, with the default taken from the component's initial value. If the app uses gr.State session state, the API information shows only the visible inputs and outputs. The client passes state between requests automatically, and Client.reset_session() resets it.
Prepare known inputs:Prepare inputs to match the contract. Plain text is passed directly, and multiple parameters are passed as separate arguments. The source recommends keyword arguments over positional ones. File or URL inputs go inside handle_file(), which the source says uploads the file to the Gradio server and preprocesses it correctly. Use only files you are allowed to send.
Submit the job and keep the Job:Use the first source code block below. It imports Client from gradio_client, connects to the English to French Space with space="abidlabs/en2fr", and calls client.submit("Hello", api_name="/predict"), storing the returned Job in job. The code assumes the Space still exists and is reachable, and it handles no token, exception or empty output. The "Do something else" line is a placeholder comment. Keep the space= and src= spellings as the source gives them. These are independent source examples, not consecutive steps on the same job. The first excerpt’s final result() blocks and implements no wait limit. For bounded waiting, retain the same Job returned by this submission, defer that final line, and check completion as described next.
Check status and completion:Use the second source block. It connects to the calculator app with src="gradio/calculator", submits 5, "add" and 4, then calls job.status(). In its interactive example the source shows STARTING, but it gives no final result for this submission. A bare job.status() line in a script prints nothing, so store or print the return value if you want to see it. You can pair it with done() to see whether the job has finished. Bounded waiting is an editorial recommendation: choose a deadline before waiting and check done() on the same Job at intervals until that deadline, saving or printing status() when useful. If it is still unfinished at the deadline, stop polling and take the stopping path; do not then enter an unbounded result() call. Retrieve only after done() becomes true.
Retrieve the result and check content:After done() is true on the same Job, call job.result() and save or print its actual return value. If it raises, record the failure and take the cleanup path. This ordering is an editorial control; the source describes result() itself as blocking. The source's translation example shows Bonjour in its interactive output; that is source-reported, not observed in this batch. Compare the result with the return type you read from the contract and judge the content by your own task criteria, since status and done do not prove correctness. For a generator endpoint the source warns that result() returns only the first value, and the full series comes from outputs() or by iterating over the Job.
Handle stopping, failure and cleanup: these controls are editorial recommendations. If the deadline passes while the Job is unfinished, retrieval raises, or the content check fails, stop further requests and do not adopt the result. Record the issue without endless retries. On a user stop, cancel() for an ordinary queued job returns True when that unstarted job is cancelled and False when it has started and will not be cancelled this way. Discard its result; any continued observation still uses your deadline. For iterative outputs, the source says cancellation stops after the current iteration, so the ordinary queue-only scope does not apply. In every exit path, remove tokens and passwords from scripts and clear environment credentials no longer needed. If you separately created a paid copy, check whether it still needs to remain. No copy was created or operated in this batch.
Worked example
Take the English to French Space abidlabs/en2fr from the source. The reader first confirms with view_api that the endpoint is /predict, then submits "Hello". The source reports that job.result() returns Bonjour. The reader's own expected check is: a Job was created, it finished within the wait limit, and the output is non-empty French text that matches the input's meaning. The calculator example works the same way. After submitting 5, "add", 4, the source shows only a STARTING status and no result for that submission, so the reader must retrieve and check the result rather than treat status as the answer. All outputs here come from the official documentation and none was run in this batch.
from gradio_client import Client
client = Client(space="abidlabs/en2fr")
job = client.submit("Hello", api_name="/predict") # This is not blocking
# Do something else
job.result() # This is blocking
from gradio_client import Client
client = Client(src="gradio/calculator")
job = client.submit(5, "add", 4, api_name="/predict")
job.status()
Check the result and failure paths
- Success (editorial recommendation): the
view_apicontract matches the submittedapi_nameand parameters,successin the status is true ordone()is true, andresult()returns non-empty content that fits the input. - Failure (editorial recommendation): a refused connection, missing permission, rate limiting, a false
success, an exception fromresult(), or passing the self-set wait limit. On any of these, stop, record and clean up instead of retrying with guessed parameters. - Empty output (editorial recommendation): if
result()returns an empty string or None, count it as a failed check and recheck the input type andapi_name. A normal queue status is not evidence that the content is acceptable. - User stop (editorial recommendation): for ordinary non-iterative jobs, interpret
cancel()True/False as documented—an unstarted job is cancelled, while a started job is not cancelled this way. Discard the result or observe only within the existing deadline. Iterative-output jobs stop after the current iteration; do not describe all cancellation as queue-only. The source supplies no other way to forcibly stop a started ordinary job. - Clean up on every exit path: remove tokens, passwords and related environment variables, and confirm no unneeded private copy is left from this check.
Scope and limits
- The source shows interactive output for its snippets but does not say they run unchanged in every environment. The first needs an existing Space, the second needs the calculator app to be available, and neither includes exception handling or credentials.
- The meanings of the
statusfields come from the source, anddone()only reports whether the job has finished. The source does not say either verifies output correctness. - The cancel distinction, generator behavior and the first-value-only behavior of
result()follow the source documentation alone. The source gives no way to cancel a started ordinary job. - The source gives no current pricing, compatibility guarantee beyond the stated Python version, or login-state information, and this guide infers none.
No product, API or Space was run or called in this batch, and no dependency was installed. Every output in this guide comes from the official documentation, not from batch observation. The completion checks, stopping rules and credential cleanup are editorial recommendations, not official behavior.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.