AsyncClient class provides asynchronous (non-blocking) access to all SDK features. It is accessed through the client.aio property of a synchronous Client instance.
Accessing AsyncClient
You don’t directly instantiateAsyncClient. Instead, access it through the aio property:
Usage Examples
Basic Async Request
Using Async Context Manager
The async client supports async context managers for automatic resource cleanup:Concurrent Requests
Useasyncio.gather() to make multiple requests concurrently:
Vertex AI with Async
Properties
All properties return async versions of the corresponding synchronous APIs.models
AsyncModels
Async access to the Models API for content generation, embeddings, and model management.See generate_content for available methods.
chats
AsyncChats
Async access to multi-turn conversation functionality.See chats.create for details.
files
AsyncFiles
Async access to the Files API for uploading and managing media files.See files.upload for details.
caches
AsyncCaches
Async access to the Caches API for context caching.See caches.create for details.
file_search_stores
AsyncFileSearchStores
Async access to the File Search Stores API for semantic search.
batches
AsyncBatches
Async access to the Batches API for batch processing.See batches.create for details.
tunings
AsyncTunings
Async access to the Tunings API for model fine-tuning.See tunings.tune for details.
live
AsyncLive
Async access to the Live API for real-time streaming interactions.
auth_tokens
AsyncTokens
Async access to authentication token management.
operations
AsyncOperations
Async access to long-running operations management.
interactions
AsyncInteractionsResource
Async access to the experimental Interactions API for live, streaming interactions.
Methods
aclose()
Closes the async client explicitly and releases resources.This method only closes the async client. It does not close the sync client, which can be closed using
client.close() or the sync context manager.Context Manager Methods
The AsyncClient supports the async context manager protocol for automatic resource cleanup.__aenter__()
Enters the async runtime context and returns the async client.__aexit__(exc_type, exc_value, traceback)
Exits the async runtime context and closes the async client.Advanced Patterns
Error Handling with Async
Async Iteration with Streaming
Combining Sync and Async
You can use both sync and async clients from the sameClient instance:
Performance Considerations
When to Use AsyncClient
Use the async client when:- Making multiple concurrent API requests
- Building web applications with async frameworks (FastAPI, aiohttp, etc.)
- Processing large batches of requests efficiently
- Integrating with other async libraries
Connection Pooling
The async client automatically manages connection pooling for efficient resource usage. You can customize this behavior usinghttp_options:
See Also
- Client - Synchronous client documentation
- generate_content - Generate content with models
- chats.create - Multi-turn conversations
- files.upload - Upload and manage media files