OpenaiBatchAPI: A Python Library for OpenAI Batch API
OpenAI Batch API Documentation
Note: Currently supports only gpt-4o and gpt-4o-mini models.
Installation
Install the package from PyPI using pip:
$ pip install OpenaiBatchAPI
Usage Example
Basic Example
from openai_batch_api import OpenaiBatchAPI
# Initialize the Batch API client
batch_client = OpenaiBatchAPI(
api_key="API_TOKEN",
max_retry=3,
timeout=5 * 60 # Timeout in seconds
)
messages = []
# Add messages with custom IDs (e.g., "calc_{i}")
for i in range(7):
messages.append({
"id": f"calc_{i}",
"content": [
{
"role": "user",
"content": f"Calculate: 1 + {i} = "
}
]
})
# Add messages with auto-generated IDs (index-based)
for i in range(7):
messages.append({
"role": "user",
"content": f"Calculate: 1 + {i} = "
})
# Execute batch completion
batchs = batch_client.batchs_completion(
messages,
max_completion_tokens=32,
model="gpt-4o-mini",
temperature=0,
seed=42,
batch_size=2
)
# Print batch outputs
for batch in batchs:
print("BATCH ID:", batch.id)
print("FILE ID:", batch.output_file.id)
for content in batch.output_file.contents:
print("Message ID:", content["id"])
print("Response:", content["choices"][0]['message']['content'])
print()
Output Sample
The script will output results similar to this:
Preparing: 100%|███████████████████████| 4/4 [00:10<00:00, 2.74s/it]
Sending: 100%|█████████████████████████| 4/4 [00:02<00:00, 1.60it/s]
Processing: 100%|██████████████████████| 4/4 [00:12<00:00, 3.10s/it]
BATCH ID: batch_672b237c62f88190b0d69cbf789eb754
FILE ID: file-IlJUOenuE8p7okKB3TX2ANKW
Message ID: calc_0
Response: 1 + 0 = 1
Message ID: calc_1
Response: 1 + 1 = 2.
BATCH ID: batch_672b237ce4fc8190813e7b1239f0336d
FILE ID: file-hTFNk0uBC37plDaWtgYtD0Dr
Message ID: calc_2
Response: 1 + 2 = 3.
Message ID: calc_3
Response: 1 + 3 = 4.
BATCH ID: batch_672b237d937c8190a2ec942aa9401732
FILE ID: file-AbzBSJGl2pPU1B6KPmMuIDBB
Message ID: calc_4
Response: 1 + 4 = 5.
Message ID: calc_5
Response: 1 + 5 = 6.
BATCH ID: batch_672b237e076c8190807819fde4cdd093
FILE ID: file-jRAePmaheNSNAWYcawdmSgYF
Message ID: calc_6
Response: 1 + 6 = 7.
Usage Statistics
The print_usage_table method outputs a table summarizing token usage and cost.
batch_client.print_usage_table(digits=8)
This outputs:
+------------+--------+-------------+
| Category | Tokens | Price |
+------------+--------+-------------+
| Prompt | 119 | $0.00000892 |
| Completion | 55 | $0.00001650 |
+------------+--------+-------------+
| Total | 174 | $0.00002543 |
+------------+--------+-------------+
Metadata
Release files for OpenaiBatchAPI 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| OpenaiBatchAPI-1.1.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for OpenaiBatchAPI-1.1.2-py2.py3-none-any.whl |
Release files / OpenaiBatchAPI-1.1.2-py2.py3-none-any.whl
| Download URL | OpenaiBatchAPI-1.1.2-py2.py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
d0a5bf6e852b4adfdbab8033c1b94fd2fb67ec47c31231a32470c1ddcfbb621e
|
|
BLAKE2b-256 checksum How to use checksums |
121d12e5a9b8736d524454e221a079a16bdbd6668a4276638643d95da40f9184
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|
Security for OpenaiBatchAPI 1.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.