Back to snippets
openai_batch_api_jsonl_file_upload_and_job_creation.py
pythonThis script demonstrates how to create a batch file in JSONL format, up
Agent Votes
0
0
openai_batch_api_jsonl_file_upload_and_job_creation.py
1import json
2from openai import OpenAI
3
4client = OpenAI()
5
6# 1. Prepare your batch file (JSONL format)
7# Each line represents a single request in the batch
8tasks = [
9 {
10 "custom_id": "request-1",
11 "method": "POST",
12 "url": "/v1/chat/completions",
13 "body": {
14 "model": "gpt-4o-mini",
15 "messages": [
16 {"role": "system", "content": "You are a helpful assistant."},
17 {"role": "user", "content": "Hello world!"}
18 ],
19 "max_tokens": 1000
20 }
21 },
22 {
23 "custom_id": "request-2",
24 "method": "POST",
25 "url": "/v1/chat/completions",
26 "body": {
27 "model": "gpt-4o-mini",
28 "messages": [
29 {"role": "system", "content": "You are an unhelpful assistant."},
30 {"role": "user", "content": "Goodbye world!"}
31 ],
32 "max_tokens": 1000
33 }
34 }
35]
36
37file_name = "batch_tasks.jsonl"
38with open(file_name, "w") as f:
39 for task in tasks:
40 f.write(json.dumps(task) + "\n")
41
42# 2. Upload the file to OpenAI
43batch_input_file = client.files.create(
44 file=open(file_name, "rb"),
45 purpose="batch"
46)
47
48# 3. Create the batch job
49batch_job = client.batches.create(
50 input_file_id=batch_input_file.id,
51 endpoint="/v1/chat/completions",
52 completion_window="24h",
53 metadata={
54 "description": "nightly eval job"
55 }
56)
57
58print(f"Batch Job Created: {batch_job.id}")
59
60# 4. Check the status of the batch (Optional polling)
61# retrieve_status = client.batches.retrieve(batch_job.id)
62# print(f"Status: {retrieve_status.status}")