Batch inference

Enrich a CSV dataset

Turn support tickets into a labelled dataset, while keeping each answer matched to its source row.

Before you begin

You need pip install openai and REKA_API_KEY, as in the quickstart. Save your data as tickets.csv with id and text columns:

tickets.csv
id,text
ticket-001,Update my invoice address.
ticket-002,My application cannot connect.

1. Turn each row into a request

The row's id becomes the request's custom_id, so every answer can find its way back.

prepare.py
import csv
import json

PROMPT = "Reply with one word, billing or technical. Ticket: "

# tickets.csv has id,text columns; every id must be unique.
with open("tickets.csv", newline="", encoding="utf-8") as source, \
     open("requests.jsonl", "w", encoding="utf-8") as out:
    for row in csv.DictReader(source):
        request = {
            "custom_id": row["id"],
            "method": "POST",
            "url": "/v1/chat/completions",
            "body": {
                "model": "qwen3.8-27b",
                "max_tokens": 64,
                "messages": [{"role": "user", "content": PROMPT + row["text"]}],
            },
        }
        out.write(json.dumps(request) + "\n")

2. Run the batch

Upload, create, wait, then save the results. If the script stops while waiting, the batch keeps running; retrieve it later by its ID.

submit.py
import os
import time
from pathlib import Path
from openai import OpenAI

client = OpenAI(base_url="https://api.reka.ai/v1", api_key=os.environ["REKA_API_KEY"])

batch_file = client.files.create(file=open("requests.jsonl", "rb"), purpose="batch")
batch = client.batches.create(
    input_file_id=batch_file.id,
    endpoint="/v1/chat/completions",
    completion_window="24h",
)
print("Started", batch.id)

while batch.status not in ("completed", "failed", "expired", "cancelled"):
    time.sleep(60)
    batch = client.batches.retrieve(batch.id)
    print(batch.status, batch.request_counts)

def save(file_id, path):
    text = client.files.content(file_id).text if file_id else ""
    Path(path).write_text(text, encoding="utf-8")

save(batch.output_file_id, "results.jsonl")
save(batch.error_file_id, "errors.jsonl")

3. Add the answers to your CSV

join.py
import csv
import json

answers = {}
for line in open("results.jsonl", encoding="utf-8"):
    row = json.loads(line)
    message = row["response"]["body"]["choices"][0]["message"]
    answers[row["custom_id"]] = (message.get("content") or "").strip()

with open("tickets.csv", newline="", encoding="utf-8") as source, \
     open("enriched.csv", "w", newline="", encoding="utf-8") as out:
    writer = csv.writer(out)
    writer.writerow(["id", "text", "category"])
    for row in csv.DictReader(source):
        writer.writerow([row["id"], row["text"], answers.get(row["id"], "")])

An empty category means the row failed (the reason is in errors.jsonl) or the model returned no text.

Over 50,000 rows or 100 MB? Split requests.jsonl into several files and run one batch per file.

Next steps