Prebuilt utilities for memory management and retrieval.
Project description
BatchBridge
BatchBridge is a library for efficient batch processing with LangGraph. It provides a mechanism to collect items, process them in batches, and handle results asynchronously using LangGraph's interrupt semantics.
What is BatchBridge?
Batch APIs can cut AI inference costs by 50% or more, but they're difficult to use in agent workflows. They force you to manually aggregate requests and design your entire agent loop around batch processing rather than focusing on individual tasks. This makes your code more complex, harder to maintain, and less shareable.
BatchBridge solves this by making batch APIs work like standard completion APIs in LangGraph. Your code makes normal API calls while BatchBridge handles batching, submission, polling, and resumption behind the scenes. This lets you design and improve an agent using single completions, then make a one-line change to let it economically scale.
We aim to give you significant cost savings with minimal code complexity.
Installation
pip install -e .
Since BatchBridge relies on LangGraph's durable execution and cron functionality, it must be run on the LangGraph platform.
Example with OpenAI's Batch API
BatchBridge has a native integration with OpenAI's Batch API:
from batch_bridge import patch_openai
from openai import AsyncOpenAI
from langgraph.graph import StateGraph
from typing_extensions import Annotated, TypedDict
# Patch the client at the global level
client = patch_openai(AsyncOpenAI())
class State(TypedDict):
messages: Annotated[list[dict], lambda x, y: x + y]
async def my_model(state: State):
# This will:
# 1. submit the message to our bridge graph
# 2. Interrupt this agent graph.
# 3. resume once the bridge graph detects that the batch is complete
result = await client.chat.completions.create(
model="gpt-4o-mini", messages=state["messages"]
)
return {"messages": [result]}
graph = StateGraph(State).add_node(my_model).add_edge("__start__", "my_model").compile()
Basic Usage
Under the hood, BatchBridge relies on two basic functions: a submit() function and a poll() function.
Here's a simple example of how to use BatchBridge:
from datetime import datetime, timedelta
from batch_bridge import Batcher
# Define functions for batch processing
def submit_batch(items):
"""Submit a batch of items for processing."""
# In a real implementation, this would submit to an external API
# and return a batch ID
print(f"Submitting batch of {len(items)} items")
return "batch_123"
def poll_batch(batch_id):
"""Poll for the results of a batch."""
# In a real implementation, this would check the status of the batch
# and return results when available
import time
time.sleep(2) # Simulate processing time
return [f"Processed: {item}" for item in ["item1", "item2"]]
# Create a batcher with default flush criteria
batcher = Batcher(
submit_func=submit_batch,
poll_func=poll_batch,
)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file batch_bridge-0.0.1rc0.tar.gz.
File metadata
- Download URL: batch_bridge-0.0.1rc0.tar.gz
- Upload date:
- Size: 92.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef35afd51092c8523927875117493b92225e22517c30c7c5d836795b09c1081e
|
|
| MD5 |
50b901c58cb1632ad4f8675660d7417b
|
|
| BLAKE2b-256 |
0dc3aef04fb61f54bffbda9f6f82e50b613eee82d0b8e1eff78e0518b4fb6c98
|
File details
Details for the file batch_bridge-0.0.1rc0-py3-none-any.whl.
File metadata
- Download URL: batch_bridge-0.0.1rc0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ee818eaac73b87e0416cb5e10858440db17b65e4123f15cc924930481e17e65
|
|
| MD5 |
4bd62fe9e11c533a06cef1c4525dd07c
|
|
| BLAKE2b-256 |
60bd605e7f3af0f25276c7a3d275e1207a3d51fe1397bd1e7ba39cb3a2648fe7
|