llm-batch-runner
Concurrent, caching, retrying batch runner for OpenAI-compatible chat completion endpoints, with optional structured-output validation against a Pydantic model.
Features
- Fires requests concurrently (configurable
max_workers) through the officialopenaiSDK, so it works with OpenAI itself or any OpenAI-compatiblebase_url(vLLM, Together, Groq, local servers, ...). - If you pass a Pydantic
base_model, requests are made with a JSON-schemaresponse_formatand every response is validated against the model. Invalid output is retried, up ton_retriestimes. - If no
base_modelis passed, it just does a normal completion and returns the raw text. - Every sample is cached to disk as its own JSON file, keyed by a SHA-256
hash of its input
messages. On a re-run, already-cached samples are skipped unlessoverwrite_existing=True. - Failures are logged (via the standard
loggingmodule) and returned in the result list rather than raised, so one bad sample never kills the whole batch. LLMBatchRunner.load_cache(cache_dir)is a static method that loads every cached record back into a dict, keyed by hash.
Usage
from pydantic import BaseModel
from llm_batch_runner import LLMBatchRunner
class Answer(BaseModel):
reasoning: str
value: int
conversations = [
[{"role": "user", "content": "What is 12 * 7?"}],
[{"role": "user", "content": "What is 9 * 9?"}],
# ... as many as you like
]
runner = LLMBatchRunner(
base_model=Answer, # or None for plain text completions
messages=conversations, # List[List[dict]] — one conversation per sample
max_workers=16,
client_config={"api_key": "wow_very secret", "base_url": None},
model_name="gpt-4o-mini",
cache_dir="./llm_cache",
n_retries=3,
overwrite_existing=False,
conversations_meta=None # or list of dicts: additional values to store
)
results = runner.run()
print(results[0])
# OUTPUT:
Output:
{
"attempts": 1,
"conversation": [
{
"role": "user",
"content": "What is 12 * 7?"
}
],
"error": "None",
"from_cache": "False",
"hash": "f699170cc22a5e871fccdf3a414f35606d100c943d18387e58d75c0a1006b6e6",
"index": 0,
"output": {
"reasoning": "12 * 7 can be calculated as (10 * 7) + (2 * 7) = 70 + 14 = 84.",
"value": 84
},
"raw_output": "{\"reasoning\": \"12 * 7 can be calculated as (10 * 7) + (2 * 7) = 70 + 14 = 84.\", \"value\": 84}",
"success": "True",
"meta": null
}
Loading cached results later
from llm_batch_runner import LLMBatchRunner
cache = LLMBatchRunner.load_cache("./llm_cache")
# List of dicts - loaded data from the cache
# [
# {"success": True, "output": {...}, "error": None, ...},
# ...
# ]
Notes
messagesis a list of conversations — each conversation is itself the standard OpenAImessageslist ([{"role": ..., "content": ...}, ...]).- The cache key is a hash of the conversation only (not the model name),
so if you change
model_nameand want fresh results, pass a differentcache_diror useoverwrite_existing=True. - Failed samples (that exhausted all retries) are also cached.
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 llm_batch_runner-0.1.0.tar.gz.
File metadata
- Download URL: llm_batch_runner-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74887370d623c195b58057249f67fcb78437383fa094c964224bcaa5f3afeaba
|
|
| MD5 |
e045342450817e70c7bf446a28528e03
|
|
| BLAKE2b-256 |
854a952819e2bebd615bad79f9cf07a1657b2510f35889473c9dfc6a09574003
|
File details
Details for the file llm_batch_runner-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_batch_runner-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
256f9ac5977354ebb40ea7a5cae2972b98d36b26b43ea2b742621c210351d71c
|
|
| MD5 |
3a3556223a0bb5623e10ec76f45d9d3c
|
|
| BLAKE2b-256 |
87b0d183a7abaf06f222b880117026fcc83c89635beaadec5bf95188131458da
|