Skip to main content

Async batch runner for OpenAI API calls with streaming support

Project description

batch-stream-openai

Async batch runner for OpenAI API calls with streaming support. Fire many requests concurrently and process results as they complete—ideal for batch inference, evals, or any workload where you need to call the OpenAI API (or LiteLLM-compatible providers) at scale.

Features

  • stream_batch: Fire N requests concurrently and yield each (index, result) as it completes. Process results incrementally (e.g., write to DB after every single result).
  • run_batch: Convenience wrapper that collects all results into an ordered list.
  • Automatic retries on transient errors (rate limits, timeouts, 502/503).
  • OpenAI models (gpt-, o1, o3, o4, chatgpt-) use the native OpenAI SDK.
  • Non-OpenAI models (e.g. gemini/gemini-2.0-flash) route through LiteLLM when the optional [litellm] extra is installed.

Installation

# Core (OpenAI only)
pip install batch-stream-openai

# With LiteLLM support (Gemini, Anthropic, etc.)
pip install batch-stream-openai[litellm]

Quick Start

from batch_stream_openai import OpenAIRequest, stream_batch, run_batch

requests = [
    OpenAIRequest(
        system_prompt="You are a helpful assistant.",
        user_prompt="What is 2+2?",
        model="gpt-4o-mini",
        api="chat",
    ),
    OpenAIRequest(
        system_prompt="You are a helpful assistant.",
        user_prompt="What is the capital of France?",
        model="gpt-4o-mini",
        api="chat",
    ),
]

# Stream results as they complete
for idx, result in stream_batch(requests, max_concurrency=5):
    if isinstance(result, str):
        print(f"Request {idx}: {result[:50]}...")
    else:
        print(f"Request {idx} failed: {result}")

# Or collect all results in order
results = run_batch(requests)

API Reference

OpenAIRequest

Field Type Default Description
system_prompt str required System message for the model
user_prompt str required User message
model str "gpt-5-mini" Model name (OpenAI or LiteLLM-style, e.g. gemini/gemini-2.0-flash)
api "responses" | "chat" "responses" "responses" for Responses API, "chat" for Chat Completions
response_format dict | None None JSON schema for structured output (Chat API)
reasoning_effort str | None None For reasoning models: "minimal", "low", "medium", "high"

stream_batch(requests, *, max_concurrency=5)

Yields (index, str | Exception) as each request completes. Results arrive in completion order, not input order. Failed requests yield an Exception instead of raising.

run_batch(requests, *, max_concurrency=5)

Returns list[str | Exception] with results in the same order as requests.

Environment Variables

  • OpenAI: OPENAI_API_KEY (required for OpenAI models)
  • LiteLLM: Provider-specific keys (e.g. GEMINI_API_KEY, ANTHROPIC_API_KEY) when using non-OpenAI models with the [litellm] extra

License

Apache 2.0

Project details


Download files

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

Source Distribution

batch_stream_openai-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

batch_stream_openai-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file batch_stream_openai-0.1.0.tar.gz.

File metadata

  • Download URL: batch_stream_openai-0.1.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for batch_stream_openai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a904180ca88e2a7a9b757d2ff0881e24154e228fb2944d75cdb5f38d0bb995b
MD5 437459ff215175774122ccdb53f2e45e
BLAKE2b-256 17aa2cd4d0d6eb9379e5937477620ddfdb5a11c57196aa3ce05cce527a09ecc7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for batch_stream_openai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 666d97996776b26a7b99d877cf03f3b1ca5df6947ca9b19dade0ed879cac8d05
MD5 9c13308a518214b0f93c6fc38caa66c7
BLAKE2b-256 14fb4f95c022638464aebafbd3a0a1edd7ea33ebab77d83500e7ddf0178ef3ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page