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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_batch_runner-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_batch_runner-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

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

Hashes for llm_batch_runner-0.1.0.tar.gz
Algorithm Hash digest
SHA256 74887370d623c195b58057249f67fcb78437383fa094c964224bcaa5f3afeaba
MD5 e045342450817e70c7bf446a28528e03
BLAKE2b-256 854a952819e2bebd615bad79f9cf07a1657b2510f35889473c9dfc6a09574003

See more details on using hashes here.

File details

Details for the file llm_batch_runner-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_batch_runner-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 256f9ac5977354ebb40ea7a5cae2972b98d36b26b43ea2b742621c210351d71c
MD5 3a3556223a0bb5623e10ec76f45d9d3c
BLAKE2b-256 87b0d183a7abaf06f222b880117026fcc83c89635beaadec5bf95188131458da

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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