Repository navigation
feat: add support for OpenAI batch processing #1479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 2 commits into
cheaper-inference:release/v3.7.0
from
hartmark:feature/openai-batching
Apr 21, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,374 @@ | ||
| import { v4 as uuidv4 } from "uuid"; | ||
| import { | ||
| getPendingBatches, | ||
| updateBatch, | ||
| getFileContent, | ||
| createFile, | ||
| getApiKeyById, | ||
| getBatch, | ||
| listBatches, | ||
| listFiles, | ||
| deleteFile, | ||
| updateFileStatus, | ||
| } from "@/lib/localDb"; | ||
| import { handleChat } from "@/sse/handlers/chat"; | ||
|
|
||
| let isProcessing = false; | ||
| let pollInterval: NodeJS.Timeout | null = null; | ||
|
|
||
| export function initBatchProcessor() { | ||
| if (pollInterval) return pollInterval; | ||
| console.log("[BATCH] Initializing batch processor polling..."); | ||
|
|
||
| // Fail any batches that were in_progress when the server last shut down — | ||
| // we cannot safely resume mid-batch without re-processing from scratch. | ||
| recoverOrphanedBatches(); | ||
|
|
||
| pollInterval = setInterval(async () => { | ||
| if (isProcessing) return; | ||
| try { | ||
| isProcessing = true; | ||
| await processPendingBatches(); | ||
| } catch (err) { | ||
| console.error("[BATCH] Polling error:", err); | ||
| } finally { | ||
| isProcessing = false; | ||
| } | ||
| }, 10000); // Poll every 10s | ||
| return pollInterval; | ||
| } | ||
|
|
||
| export function stopBatchProcessor() { | ||
| if (pollInterval) { | ||
| clearInterval(pollInterval); | ||
| pollInterval = null; | ||
| console.log("[BATCH] Stopped batch processor polling."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Mark any in_progress batches as failed on startup. | ||
| * These were orphaned by a server crash or restart and cannot be safely resumed. | ||
| */ | ||
| function recoverOrphanedBatches() { | ||
| try { | ||
| const pending = getPendingBatches(); | ||
| for (const batch of pending) { | ||
| if (batch.status === "in_progress") { | ||
| console.warn(`[BATCH] Failing orphaned in_progress batch ${batch.id} (server restarted)`); | ||
| updateBatch(batch.id, { | ||
| status: "failed", | ||
| failedAt: Math.floor(Date.now() / 1000), | ||
| errors: [{ message: "Batch interrupted by server restart and cannot be resumed" }], | ||
| }); | ||
| if (batch.inputFileId) { | ||
| updateFileStatus(batch.inputFileId, "processed"); | ||
| } | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.error("[BATCH] Orphan recovery error:", err); | ||
| } | ||
| } | ||
|
|
||
| export async function processPendingBatches() { | ||
| const pending = getPendingBatches(); | ||
| for (const batch of pending) { | ||
| if (batch.status === "validating") { | ||
| await startBatch(batch); | ||
| } else if (batch.status === "cancelling") { | ||
| await cancelBatch(batch); | ||
| } | ||
| // in_progress: currently being processed by processBatchItems running in background; | ||
| // orphaned in_progress batches are handled by recoverOrphanedBatches() at startup. | ||
| } | ||
|
|
||
| // Cleanup task: delete files for batches completed more than completionWindow ago | ||
| await cleanupExpiredBatches(); | ||
| } | ||
|
|
||
| async function cleanupExpiredBatches() { | ||
| try { | ||
| const now = Math.floor(Date.now() / 1000); | ||
| const batches = listBatches(undefined, 200); // Check last 200 batches | ||
|
|
||
| const parseWindow = (window: string): number => { | ||
| if (!window) return 86400; | ||
| const match = new RegExp(/^(\d+)([hdm])$/).exec(window); | ||
| if (!match) return 86400; | ||
| const val = Number.parseInt(match[1]); | ||
| const unit = match[2]; | ||
| if (unit === "h") return val * 3600; | ||
| if (unit === "d") return val * 86400; | ||
| if (unit === "m") return val * 60; | ||
| return 86400; | ||
| }; | ||
|
|
||
| for (const batch of batches) { | ||
| const windowSeconds = parseWindow(batch.completionWindow); | ||
|
|
||
| // Cleanup completed/failed/cancelled batches after their completion window | ||
| const completionTime = | ||
| batch.completedAt || batch.failedAt || batch.cancelledAt || batch.expiredAt; | ||
| if (completionTime && now - completionTime > windowSeconds) { | ||
| if (batch.inputFileId) deleteFile(batch.inputFileId); | ||
| if (batch.outputFileId) deleteFile(batch.outputFileId); | ||
| if (batch.errorFileId) deleteFile(batch.errorFileId); | ||
| } | ||
|
|
||
| // Expire pending batches that have exceeded their completion window | ||
| if (batch.status === "validating") { | ||
| if (now - batch.createdAt > windowSeconds) { | ||
| updateBatch(batch.id, { status: "expired", expiredAt: now }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Cleanup orphan files (batch-purpose files stuck in validating after 48h) | ||
| const allFiles = listFiles(); | ||
|
hartmark marked this conversation as resolved.
Resolved
|
||
| for (const file of allFiles) { | ||
| if ( | ||
| file.purpose === "batch" && | ||
| (file.status === "validating" || !file.status) && | ||
| now - file.createdAt > 172800 | ||
| ) { | ||
| deleteFile(file.id); | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.error("[BATCH] Cleanup error:", err); | ||
| } | ||
| } | ||
|
|
||
| async function startBatch(batch: any) { | ||
| console.log(`[BATCH] Starting batch ${batch.id}`); | ||
|
|
||
| const content = getFileContent(batch.inputFileId); | ||
| if (!content) { | ||
| failBatch(batch.id, "Input file content not found"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const lines = content.toString().split("\n").filter((l) => l.trim()); | ||
| const total = lines.length; | ||
|
|
||
| updateFileStatus(batch.inputFileId, "validating"); | ||
| updateBatch(batch.id, { | ||
| status: "in_progress", | ||
| inProgressAt: Math.floor(Date.now() / 1000), | ||
| requestCountsTotal: total, | ||
| }); | ||
|
|
||
| // Fire-and-forget: process items in the background so the poll loop isn't blocked. | ||
| // isProcessing prevents a second poll tick from overlapping. | ||
| // noinspection ES6MissingAwait | ||
| processBatchItems(batch, lines); | ||
|
hartmark marked this conversation as resolved.
Resolved
|
||
| } catch (err) { | ||
| console.error(`[BATCH] Error starting batch ${batch.id}:`, err); | ||
| failBatch(batch.id, err instanceof Error ? err.message : String(err)); | ||
| } | ||
| } | ||
|
|
||
| async function processBatchItems(batch: any, lines: string[]) { | ||
| const results: any[] = []; | ||
| const errors: any[] = []; | ||
| let completedCount = 0; | ||
| let failedCount = 0; | ||
| let totalInputTokens = 0; | ||
| let totalOutputTokens = 0; | ||
| let totalReasoningTokens = 0; | ||
| let usedModel = batch.model || null; | ||
|
|
||
| const apiKeyRow = batch.apiKeyId ? await getApiKeyById(batch.apiKeyId) : null; | ||
|
|
||
| for (const line of lines) { | ||
| // Check if cancelled mid-process | ||
| const current = getBatch(batch.id); | ||
| if (!current || current.status === "cancelling" || current.status === "cancelled") { | ||
| break; | ||
| } | ||
|
|
||
| try { | ||
| const item = JSON.parse(line); | ||
| const { custom_id: customId, url, body } = item; | ||
|
|
||
| const headers = new Headers(); | ||
| if (apiKeyRow?.key) { | ||
| headers.set("Authorization", `Bearer ${apiKeyRow.key}`); | ||
| } | ||
| headers.set("Content-Type", "application/json"); | ||
|
|
||
| // BATCH-SPECIFIC: Force stream: false — batches don't support SSE responses | ||
| const batchItemBody = { ...body, stream: false }; | ||
|
|
||
| const response = await handleChat({ | ||
| json: async () => batchItemBody, | ||
| url: `http://localhost${url}`, | ||
| headers, | ||
| method: "POST", | ||
| } as any); | ||
|
|
||
| let responseData: { error: any; id?: any; usage?: any; model?: any }; | ||
| let statusCode = 200; | ||
|
|
||
| if (response instanceof Response) { | ||
| statusCode = response.status; | ||
| const contentType = response.headers.get("content-type") || ""; | ||
| if (contentType.includes("application/json")) { | ||
| responseData = await response.json(); | ||
| } else { | ||
| const text = await response.text(); | ||
| try { | ||
| responseData = JSON.parse(text); | ||
| } catch { | ||
| responseData = { error: { message: text || "Unknown error", type: "invalid_response" } }; | ||
| } | ||
| } | ||
| } else { | ||
| responseData = response; | ||
| } | ||
|
|
||
| const hasError = responseData?.error; | ||
| const requestId = `batch_req_${uuidv4().replaceAll("-", "")}`; | ||
|
|
||
| results.push({ | ||
| id: requestId, | ||
| custom_id: customId, | ||
| response: { | ||
| status_code: statusCode, | ||
| request_id: responseData?.id || "req_unknown", | ||
| body: responseData, | ||
| }, | ||
| error: null, | ||
| }); | ||
|
|
||
| if (hasError || statusCode >= 400) { | ||
| failedCount++; | ||
| } else { | ||
| completedCount++; | ||
| if (responseData?.usage) { | ||
| totalInputTokens += responseData.usage.prompt_tokens || 0; | ||
| totalOutputTokens += responseData.usage.completion_tokens || 0; | ||
| totalReasoningTokens += | ||
| responseData.usage.completion_tokens_details?.reasoning_tokens || 0; | ||
| } | ||
| if (!usedModel && responseData?.model) { | ||
| usedModel = responseData.model; | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.error(`[BATCH] Item failed in ${batch.id}:`, err); | ||
| let customId = "unknown"; | ||
| try { | ||
| customId = JSON.parse(line).custom_id || "unknown"; | ||
| } catch { | ||
| // line was malformed JSON | ||
| } | ||
| errors.push({ | ||
| custom_id: customId, | ||
| error: err instanceof Error ? err.message : String(err), | ||
| }); | ||
| failedCount++; | ||
| } | ||
|
|
||
| // Periodic progress update | ||
| updateBatch(batch.id, { | ||
| requestCountsCompleted: completedCount, | ||
| requestCountsFailed: failedCount, | ||
| model: usedModel, | ||
| usage: { | ||
| input_tokens: totalInputTokens, | ||
| output_tokens: totalOutputTokens, | ||
| total_tokens: totalInputTokens + totalOutputTokens, | ||
| input_tokens_details: { cached_tokens: 0 }, | ||
| output_tokens_details: { reasoning_tokens: totalReasoningTokens }, | ||
| }, | ||
| }); | ||
|
hartmark marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // Finalize | ||
| await finalizeBatch(batch.id, results, errors); | ||
| } | ||
|
|
||
| async function finalizeBatch(batchId: string, results: any[], itemsWithErrors: any[]) { | ||
| const current = getBatch(batchId); | ||
| if (current?.status === "cancelling") { | ||
| updateBatch(batchId, { status: "cancelled", cancelledAt: Math.floor(Date.now() / 1000) }); | ||
| return; | ||
| } | ||
|
|
||
| updateBatch(batchId, { status: "finalizing", finalizingAt: Math.floor(Date.now() / 1000) }); | ||
|
|
||
| if (current?.inputFileId) { | ||
| updateFileStatus(current.inputFileId, "processed"); | ||
| } | ||
|
|
||
| let outputFileId: string | null = null; | ||
| const successes = results.filter( | ||
| (r) => r.response.status_code < 400 && !r.response.body?.error | ||
| ); | ||
| if (successes.length > 0) { | ||
| const outputContent = successes.map((r) => JSON.stringify(r)).join("\n"); | ||
| const file = createFile({ | ||
| bytes: Buffer.byteLength(outputContent), | ||
| filename: `batch_${batchId}_output.jsonl`, | ||
| purpose: "batch_output", | ||
| content: Buffer.from(outputContent), | ||
| apiKeyId: current?.apiKeyId, | ||
| status: "completed", | ||
| }); | ||
| outputFileId = file.id; | ||
| } | ||
|
|
||
| let errorFileId: string | null = null; | ||
| const failures = results.filter( | ||
| (r) => r.response.status_code >= 400 || r.response.body?.error | ||
| ); | ||
| const allFailures = [ | ||
| ...failures, | ||
| ...itemsWithErrors.map((e) => ({ | ||
| id: `batch_req_${uuidv4().replaceAll("-", "")}`, | ||
| custom_id: e.custom_id, | ||
| response: null, | ||
| error: { message: e.error, type: "batch_process_error" }, | ||
| })), | ||
| ]; | ||
|
|
||
| if (allFailures.length > 0) { | ||
| const errorContent = allFailures.map((e) => JSON.stringify(e)).join("\n"); | ||
| const file = createFile({ | ||
| bytes: Buffer.byteLength(errorContent), | ||
| filename: `batch_${batchId}_error.jsonl`, | ||
| purpose: "batch_output", | ||
| content: Buffer.from(errorContent), | ||
| apiKeyId: current?.apiKeyId, | ||
| status: "completed", | ||
| }); | ||
| errorFileId = file.id; | ||
| } | ||
|
|
||
| updateBatch(batchId, { | ||
| status: "completed", | ||
| completedAt: Math.floor(Date.now() / 1000), | ||
| outputFileId, | ||
| errorFileId, | ||
| }); | ||
| console.log(`[BATCH] Completed batch ${batchId}`); | ||
| } | ||
|
|
||
| async function cancelBatch(batch: any) { | ||
| updateBatch(batch.id, { | ||
| status: "cancelled", | ||
| cancelledAt: Math.floor(Date.now() / 1000), | ||
| }); | ||
| console.log(`[BATCH] Cancelled batch ${batch.id}`); | ||
| } | ||
|
|
||
| function failBatch(batchId: string, reason: string) { | ||
| updateBatch(batchId, { | ||
| status: "failed", | ||
| failedAt: Math.floor(Date.now() / 1000), | ||
| errors: [{ message: reason }], | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.