Python library for creating and managing OpenAI Structured Outputs batch API calls
Project description
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: falserequirement - 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 fileoutput_file: Path where output will be savederror_file: Path where errors will be savedjob_name: Name identifier for the batch jobbatch_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. Theresponse_modelshould be a Pydantic model that will be converted to JSON Schema withadditionalProperties: falseautomatically applied.upload(): Upload the batch to OpenAIget_status(): Get the current status of the batchdownload(): 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 costsoutput_cost(filename): Calculate output token costsinput_tokens(filename): Count input tokensoutput_tokens(filename): Count output tokens
Supported Models
The library supports the following OpenAI models with cost tracking:
gpt-4o-minigpt-4oo3o3-minio4-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: falseschema requirement - Cost tracking and estimation
- Pydantic model support for structured outputs
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openai_so_batch-0.1.1.tar.gz.
File metadata
- Download URL: openai_so_batch-0.1.1.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5963d2310ea99b41980b02493770bb3b4223836b31e1a69fa8f14c708dd2eb09
|
|
| MD5 |
03de8a62694a4bd06089127beb213881
|
|
| BLAKE2b-256 |
60261d3c891b137b275329f8ddc669965c933ccccba1291b7032a383d1734a26
|
File details
Details for the file openai_so_batch-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openai_so_batch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c13eb03eae2b2ba9f2515b0e027166c9d897be27d969a986f23691493b56c282
|
|
| MD5 |
9c8d633c0cf8f49c38c504499ee6f3f6
|
|
| BLAKE2b-256 |
b1b6b9a5071b6926c88695747cffa1b5e38da74f3a518e05592045280ec3056d
|