Submit a list of requests inline, kick off a batch, poll until done. Batch jobs run at discounted pricing.
1 from meshapi import ( 2 BatchRequestItem, 3 CreateBatchParams, 4 ) 5 6 # 1. Create the batch 7 batch = client.batches.create( 8 CreateBatchParams( 9 requests=[ 10 BatchRequestItem( 11 custom_id="req-1", 12 body={ 13 "model": "openai/gpt-4o-mini", 14 "messages": [{"role": "user", "content": "Say hi."}], 15 }, 16 ), 17 BatchRequestItem( 18 custom_id="req-2", 19 body={ 20 "model": "openai/gpt-4o-mini", 21 "messages": [{"role": "user", "content": "Say bye."}], 22 }, 23 ), 24 ], 25 completion_window="24h", 26 ) 27 ) 28 29 # 2. Poll later 30 status = client.batches.get(batch.id) 31 if status.status == "completed" and status.output_file_id: 32 output_bytes = client.files.content(status.output_file_id) 33 # output_bytes is JSONL