Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 ยท Testing and Patterns

Errors, Notifications and Performance

Handle failures gracefully in Gradio with gr.Error, gr.Warning and gr.Info, validate inputs, cache examples and results, and measure slow steps, with runnable stand-ins.

Demos fail in boring ways: an empty box, an unreadable file, a model that times out, a slow first request. How the app responds decides whether visitors trust it. This lesson covers the tools Gradio gives you to report problems clearly, and the habits that make apps feel fast.

Real Gradio is not available on this site, so its code is read-only. The runnable blocks reproduce the patterns (custom exceptions, validation, caching, timing) in plain Python, labelled stand-ins.

gr.Error, gr.Warning and gr.Info

Raise gr.Error to stop the function and show a red message to the user instead of a vague failure:

Python
import gradio as gr

def divide(a, b):
    if b == 0:
        raise gr.Error("The divisor cannot be zero.")
    return a / b

The page shows a toast in the corner with your message, and the output stays unchanged. Without the custom error, visitors only see a generic "Error" and the traceback lands in your terminal. Two softer functions keep the function running:

Python
def process(text):
    if len(text) > 1000:
        gr.Warning("Long text: only the first 1000 characters are used.")
        text = text[:1000]
    gr.Info("Processing started...")
    return text.upper()

gr.Info shows a blue note, gr.Warning a yellow one; both stay on screen for a few seconds and do not stop the call. Use duration=None to keep a message until it is dismissed. Messages should say what happened and what to do next, never leak a stack trace or secret (see security).

Setting launch(show_error=True) also shows unexpected Python exceptions in the browser: handy while developing, but turn it off in public apps.

Validate early

Check inputs at the top of the function, before any expensive work, and use raise gr.Error so the message reaches the user. Put the checks in a function you can test:

Python Validation with a custom exception

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first call succeeds; the three bad inputs each produce a clear message. Notice the last except: unexpected errors become a friendly generic message (while the real error should be logged for you), so internal details never reach users.

Logging what really happened

Show users a short message and write the details to a log that you read:

Python
import logging
logger = logging.getLogger(__name__)

def predict(img):
    try:
        return model(img)
    except Exception:
        logger.exception("prediction failed")
        raise gr.Error("Could not process this image.")

logger.exception records the whole traceback. On a Space, logs are in the Logs tab. Add the user's request.session_hash (from a gr.Request argument) to log lines to correlate a complaint with a failure.

Cache examples

gr.Examples shows clickable sample inputs. With cache_examples=True Gradio runs each example once at startup and stores the outputs, so clicking is instant and does not use your model:

Python
gr.Examples(
    examples=[["I love it"], ["Terrible service"]],
    inputs=text,
    outputs=label,
    fn=predict,
    cache_examples=True,       # computed once; "lazy" computes on first click
)

Use cache_examples="lazy" for expensive functions so startup is quick, and results are stored on first use.

Cache results in your code

When the same inputs come back often, remember the answers. In Python, functools.lru_cache does it in one line, as long as the arguments are hashable (strings and numbers, not lists or images):

Python lru_cache and timing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two repeated calls with 4 return from the cache; only 4 and 5 are computed (two real computations). Caches live in the process memory, so each worker has its own, and they vanish when the app restarts. For large or shared caches use a disk cache or Redis. Never cache anything that depends on the user (private data) without including the user in the key.

Measure before optimizing

Guessing which step is slow wastes time. A tiny timing helper shows where it goes:

Python A timing context manager

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The model step takes most of the time (about two thirds here), so tuning pre- and postprocessing would be wasted effort. Measure first, then optimize the biggest slice.

Speed checklist

  • Load models once at startup and keep them in memory (see wrapping a model).
  • Use a smaller or quantized model, lower image resolutions and shorter maximum text lengths.
  • Batch GPU work (batch=True) and tune the queue.
  • Stream partial results so the page feels alive (streaming).
  • Avoid sending big arrays back and forth: return type="filepath" images and compress audio.
  • Cold starts: Spaces that sleep take a minute to wake; keep a lightweight "Loading..." message and consider always-on hardware.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does raising gr.Error do?

    gr.Error stops the call and displays your message in the UI.
  2. 2. Which function shows a message without stopping the function?

    gr.Warning and gr.Info show notices while the function keeps running.
  3. 3. What does cache_examples=True do?

    Clicking an example then returns the stored result instantly.
  4. 4. Why measure before optimizing?

    Optimizing a step that takes 5 percent of the time barely helps.

Summary

  • gr.Error stops with a clear message; gr.Warning and gr.Info inform without stopping.
  • Validate input first, catch unexpected errors, show a friendly message and log the details.
  • Cache examples and repeated results, keeping private data out of shared caches.
  • Time each step, optimize the largest, and use streaming and batching for responsiveness.

Next: Project: Sentiment Analyzer