Using Qdrant asynchronously

Asynchronous programming is being broadly adopted in the Python ecosystem. Tools such as FastAPI have embraced this new paradigm, but it is also becoming a standard for ML models served as SaaS. For example, the Cohere SDK provides an async client next to its synchronous counterpart.

Databases are often launched as separate services and are accessed via a network. All the interactions with them are IO-bound and can be performed asynchronously so as not to waste time actively waiting for a server response. In Python, this is achieved by using async/await syntax. That lets the interpreter switch to another task while waiting for a response from the server.

When to use async API

There is no need to use async API if the application you are writing will never support multiple users at once (e.g it is a script that runs once per day). However, if you are writing a web service that multiple users will use simultaneously, you shouldn’t be blocking the threads of the web server as it limits the number of concurrent requests it can handle. In this case, you should use the async API.

Modern web frameworks like FastAPI and Quart support async API out of the box. Mixing asynchronous code with an existing synchronous codebase might be a challenge. The async/await syntax cannot be used in synchronous functions. On the other hand, calling an IO-bound operation synchronously in async code is considered an antipattern. Therefore, if you build an async web service, exposed through an ASGI server, you should use the async API for all the interactions with Qdrant.

Using Qdrant asynchronously

The simplest way of running asynchronous code is to use define async function and use the in the following way to run it:

from qdrant_client import models

import qdrant_client
import asyncio

async def main():
    client = qdrant_client.AsyncQdrantClient("localhost")

    # Create a collection
    await client.create_collection(
        vectors_config=models.VectorParams(size=4, distance=models.Distance.COSINE),

    # Insert a vector
    await client.upsert(
                    "color": "red",
                vector=[0.9, 0.1, 0.1, 0.5],

    # Search for nearest neighbors
    points = await
        query_vector=[0.9, 0.1, 0.1, 0.5],

    # Your async code using AsyncQdrantClient might be put here
    # ...

The AsyncQdrantClient provides the same methods as the synchronous counterpart QdrantClient. If you already have a synchronous codebase, switching to async API is as simple as replacing QdrantClient with AsyncQdrantClient and adding await before each method call.

Supported Python libraries

Qdrant integrates with numerous Python libraries. Until recently, only Langchain provided async Python API support. Qdrant is the only vector database with full coverage of async API in Langchain. Their documentation describes how to use it.