Batched is a flexible and efficient batch processing library implemented in Python. It supports asynchronous batch processing with dynamic batching and prioritization.
Low maintenance activity. Last release 17 months ago. Weaker on commit activity and recent releases.
pip install batchedBatched (Dynamic Batching)
The Batched API provides a flexible and efficient way to process multiple requests in a batch, with a primary focus on dynamic batching of inference workloads. It is designed to optimize throughput while maintaining a low-latency experience, especially useful in scenarios where you need to handle a high volume of requests simultaneously. It is designed for both async and sync execution.

Table of Contents
Why Dynamic Batching?
Dynamic batching is a technique that automatically groups multiple incoming inference requests into a single batch for processing. This is particularly beneficial for inference workloads, where processing multiple inputs together can significantly improve throughput and efficiency.
In machine learning models, dynamic batching matters because it optimizes hardware utilization, especially for GPUs and specialized AI hardware designed for parallel processing. By batching requests, we can fully leverage this parallel processing, leading to higher throughput. It also reduces overhead by amortizing fixed costs (such as data transfer and model initialization) across multiple requests, improving overall efficiency. Furthermore, dynamic batching enhances real-time performance by adapting to varying request rates, maintaining low latency during quiet periods while maximizing throughput during busy times.
This makes dynamic batching a crucial technique for deploying ML models in production environments where request patterns can be unpredictable and resource optimization is key.
Installation
To install the Batched, you can use pip:
pip install batched
Usage
Basic Example
Below is a basic example of how to use the Batched API to process text data in batches.
from sentence_transformers import SentenceTransformer
import numpy as np
+ import batched
class SentenceEmbedder:
def __init__(self, model_name='mixedbread-ai/mxbai-embed-large-v1'):
self.model = SentenceTransformer(model_name)
+ @batched.dynamically
def embed_sentences(self, sentences: list[str]) -> list[np.ndarray]:
# Convert sentences to embeddings
return self.model.encode(sentences)
# Create an instance of SentenceEmbedder
embedder = SentenceEmbedder()
# Embed single sentences
single_sent = "This is a test sentence."
embedding = embedder.embed_sentences(single_sent)
+ awaited_embedding = await embedder.embed_sentences.acall(single_sent)
# Embed a batch of 1000 sentences
batch_sentences = [f"This is test sentence number {i}." for i in range(1000)]
batch_embeddings = embedder.embed_sentences(batch_sentences)
+ awaited_batch_embeddings = await embedder.embed_sentences.acall(batch_sentences)
# Check the statistics
+ stats = embedder.embed_sentences.stats