Skip to main content

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

Metadata

Release files for batch-stream-openai 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 batch-stream-openai 0.1.0
File Size Uploaded
batch_stream_openai-0.1.0.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for batch-stream-openai 0.1.0
File Interpreter ABI Platform
batch_stream_openai-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / batch_stream_openai-0.1.0.tar.gz

Download URL batch_stream_openai-0.1.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4a904180ca88e2a7a9b757d2ff0881e24154e228fb2944d75cdb5f38d0bb995b
BLAKE2b-256 checksum
How to use checksums
17aa2cd4d0d6eb9379e5937477620ddfdb5a11c57196aa3ce05cce527a09ecc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

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

Download URL batch_stream_openai-0.1.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
666d97996776b26a7b99d877cf03f3b1ca5df6947ca9b19dade0ed879cac8d05
BLAKE2b-256 checksum
How to use checksums
14fb4f95c022638464aebafbd3a0a1edd7ea33ebab77d83500e7ddf0178ef3ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release 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