Notebooks
G
Google Gemini
Asynchronous Requests

Asynchronous Requests

quickstartsgemini-cookbookgemini-apigemini
Copyright 2025 Google LLC.
[1]

Gemini API: Asynchronous Python requests

This notebook will show you how to make asynchronous and parallel requests using the Gemini API's Python SDK and Python 3's asyncio standard library.

The examples here run in Google Colab and use the implicit event loop supplied in Colab. You can also run these commands interactively using the asyncio REPL (invoked with python -m asyncio), or you can manage the event loop yourself.

[2]
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 200.0/200.0 kB 14.5 MB/s eta 0:00:00

Set up your API key

To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see the Authentication image quickstart for an example.

[3]

Now select the model you want to use in this guide, either by selecting one in the list or writing it down. Keep in mind that some models, like the 2.5 ones are thinking models and thus take slightly more time to respond (cf. thinking notebook for more details and in particular learn how to switch the thiking off).

[4]
MODEL_ID

Using local files

This simple example shows how can you use local files (presumed to load quickly) with the SDK's async API.

[5]

Start by downloading the files locally.

[6]
2025-06-09 09:43:19 URL:https://storage.googleapis.com/generativeai-downloads/images/firefighter.jpg [547369/547369] -> "firefighter.jpg.1" [1]
2025-06-09 09:43:20 URL:https://storage.googleapis.com/generativeai-downloads/images/elephants.jpeg [224007/224007] -> "elephants.jpeg.1" [1]
2025-06-09 09:43:20 URL:https://storage.googleapis.com/generativeai-downloads/images/jetpack.jpg [357568/357568] -> "jetpack.jpg.1" [1]
FINISHED --2025-06-09 09:43:20--
Total wall clock time: 0.2s
Downloaded: 3 files, 1.1M in 0.02s (56.7 MB/s)

The async code uses the aio.models.generate_content method to invoke the API. Most async API methods can be found in the aio namespace.

Note that this code is not run in parallel. The async call indicates that the event loop can yield to other tasks, but there are no other tasks scheduled in this code. This may be sufficient, e.g. if you are running this in a web server request handler as it will allow the handler to yield to other tasks while waiting for the API response.

[7]
Boy, cat, tree.
Forest elephant family
Jetpack Backpack Concept

Downloading images asynchronously and in parallel

This example shows a more real-world case where an image is downloaded from an external source using the async HTTP library aiohttp, and each image is processed in parallel.

[8]
[9]
Download and content generation queued for 3 images.

Wild elephant family.

Jetpack backpack concept

Cat, person, tree.

In the above example, a coroutine is created for each image that both downloads and then summarizes the image. The coroutines are executed in the final step, in the as_completed loop. To start them as early as possible without blocking the other work, you could wrap download_image in asyncio.ensure_future, but for this example the execution has been deferred to keep the creation and execution concerns separate.

Next Steps

  • Check out the AsyncClient class in the Python SDK reference.
  • Read more on Python's asyncio library