Skip to main content

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 official openai SDK, so it works with OpenAI itself or any OpenAI-compatible base_url (vLLM, Together, Groq, local servers, ...).
  • If you pass a Pydantic base_model, requests are made with a JSON-schema response_format and every response is validated against the model. Invalid output is retried, up to n_retries times.
  • If no base_model is 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 unless overwrite_existing=True.
  • Failures are logged (via the standard logging module) 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

  • messages is a list of conversations — each conversation is itself the standard OpenAI messages list ([{"role": ..., "content": ...}, ...]).
  • The cache key is a hash of the conversation only (not the model name), so if you change model_name and want fresh results, pass a different cache_dir or use overwrite_existing=True.
  • Failed samples (that exhausted all retries) are also cached.

Metadata

Release files for llm-batch-runner 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-batch-runner 0.1.0
File Size Uploaded Provenance Actions
llm_batch_runner-0.1.0.tar.gz 6.6 kB Without provenance Details for llm_batch_runner-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llm-batch-runner 0.1.0
File Interpreter ABI Platform Actions
llm_batch_runner-0.1.0-py3-none-any.whl Python 3 none any Details for llm_batch_runner-0.1.0-py3-none-any.whl

Total release size: 13.7 kB

Release files / llm_batch_runner-0.1.0.tar.gz

Download URL llm_batch_runner-0.1.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
74887370d623c195b58057249f67fcb78437383fa094c964224bcaa5f3afeaba
BLAKE2b-256 checksum
How to use checksums
854a952819e2bebd615bad79f9cf07a1657b2510f35889473c9dfc6a09574003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / llm_batch_runner-0.1.0-py3-none-any.whl

Download URL llm_batch_runner-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
256f9ac5977354ebb40ea7a5cae2972b98d36b26b43ea2b742621c210351d71c
BLAKE2b-256 checksum
How to use checksums
87b0d183a7abaf06f222b880117026fcc83c89635beaadec5bf95188131458da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Security for llm-batch-runner 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page