Skip to main content

OpenAI Batch Processing Library

Python library for creating and managing OpenAI Structured Outputs batch API calls.

Makes it easy to extract structured data from large datasets using OpenAI's batch API.

Key Features

  • 🎯 Structured Outputs Only: Built specifically for OpenAI's Structured Outputs API in batch mode
  • 🔧 Schema Fix: Automatically handles the additionalProperties: false requirement - a common gotcha when working with Structured Outputs
  • 🚀 Simple batch creation and management
  • 💰 Built-in cost tracking and estimation
  • 📊 Progress monitoring and status checking
  • 🔄 Automatic file handling (input, output, errors)
  • 🛡️ Input validation and error handling
  • 📝 Pydantic model support for structured outputs

What is Structured Outputs?

Structured Outputs is an OpenAI API feature that allows you to extract structured data from text using JSON Schema. This library makes it easy to process large amounts of text in batch mode while automatically handling the schema requirements.

Installation

From PyPI (recommended)

pip install openai-so-batch

From source

git clone https://github.com/ollieglass/openai-so-batch.git
cd openai-so-batch
pip install -e .

Quick Start

Basic Structured Outputs Usage

from pydantic import BaseModel
from openai_so_batch import Batch, Costs

# Define your response model (this will be converted to JSON Schema)
class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]

# Create a batch
batch = Batch(
    input_file="batch-input.jl",
    output_file="batch-output.jl",
    error_file="batch-errors.jl",
    job_name="calendar-extract",
)

# Add tasks to the batch
examples = [
    "Alice and Bob are going to a science fair on Friday.",
    "Jane booked a meeting with Max and Omar next Tuesday at 2 pm.",
]

for i, sentence in enumerate(examples, 1):
    batch.add_task(
        id=i,
        model="gpt-4o-mini",
        system_prompt="Extract the event information.",
        user_prompt=sentence,
        response_model=CalendarEvent  # The library automatically handles schema conversion
    )

# Upload the batch
batch.upload()
print(f"Batch ID: {batch.batch_id}")

# Check status and download results
status = batch.get_status()
print(f"Status: {status}")

if status == "completed":
    batch.download()

Cost Tracking

from openai_so_batch import Costs

# Calculate costs for a model
costs = Costs(model="gpt-4o-mini")

# Estimate input costs
input_cost = costs.input_cost("batch-input.jl")
print(f"Input cost: ${input_cost:.4f}")

# Calculate actual output costs
output_cost = costs.output_cost("batch-output.jl")
print(f"Output cost: ${output_cost:.4f}")

Retrieving Existing Batches

# Retrieve an existing batch by ID
batch = Batch(
    input_file=None,
    output_file="batch-output.jl",
    error_file="batch-errors.jl",
    job_name="calendar-extract",
    batch_id="batch_6890b93c276c819091452db39758b32a"
)

status = batch.get_status()
print(f"Status: {status}")

if status == "completed":
    batch.download()

API Reference

Batch Class

The main class for managing Structured Outputs batch operations.

Constructor

Batch(
    input_file: str,
    output_file: str,
    error_file: str,
    job_name: str,
    batch_id: Optional[str] = None
)

Parameters:

  • input_file: Path to the input JSONL file
  • output_file: Path where output will be saved
  • error_file: Path where errors will be saved
  • job_name: Name identifier for the batch job
  • batch_id: Optional batch ID for retrieving existing batches

Methods

  • add_task(id, model, system_prompt, user_prompt, response_model): Add a Structured Outputs task to the batch. The response_model should be a Pydantic model that will be converted to JSON Schema with additionalProperties: false automatically applied.
  • upload(): Upload the batch to OpenAI
  • get_status(): Get the current status of the batch
  • download(): Download results and errors

Costs Class

Utility class for cost tracking and estimation.

Constructor

Costs(model: str)

Parameters:

  • model: OpenAI model name (e.g., "gpt-4o-mini", "gpt-4o", "o3")

Methods

  • input_cost(filename): Calculate input token costs
  • output_cost(filename): Calculate output token costs
  • input_tokens(filename): Count input tokens
  • output_tokens(filename): Count output tokens

Supported Models

The library supports the following OpenAI models with cost tracking:

  • gpt-4o-mini
  • gpt-4o
  • o3
  • o3-mini
  • o4-mini

Environment Setup

Make sure you have your OpenAI API key set in your environment:

export OPENAI_API_KEY="your-api-key-here"

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please open an issue on GitHub.

Changelog

0.1.1

  • Fixed description on Pypi

0.1.0

  • Initial release
  • Structured Outputs batch processing functionality
  • Automatic handling of additionalProperties: false schema requirement
  • Cost tracking and estimation
  • Pydantic model support for structured outputs

Metadata

Release files for openai-so-batch 0.1.1

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

Source distribution (sdist)

Source distribution for openai-so-batch 0.1.1
File Size Uploaded
openai_so_batch-0.1.1.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-so-batch 0.1.1
File Interpreter ABI Platform
openai_so_batch-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / openai_so_batch-0.1.1.tar.gz

Download URL openai_so_batch-0.1.1.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5963d2310ea99b41980b02493770bb3b4223836b31e1a69fa8f14c708dd2eb09
BLAKE2b-256 checksum
How to use checksums
60261d3c891b137b275329f8ddc669965c933ccccba1291b7032a383d1734a26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release files / openai_so_batch-0.1.1-py3-none-any.whl

Download URL openai_so_batch-0.1.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c13eb03eae2b2ba9f2515b0e027166c9d897be27d969a986f23691493b56c282
BLAKE2b-256 checksum
How to use checksums
b1b6b9a5071b6926c88695747cffa1b5e38da74f3a518e05592045280ec3056d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

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