Skip to content

Instantly share code, notes, and snippets.

@SnekCode
Created July 21, 2025 01:23
Show Gist options
  • Select an option

  • Save SnekCode/f6aab6c3a04019b4fb434f3ba4078171 to your computer and use it in GitHub Desktop.

Select an option

Save SnekCode/f6aab6c3a04019b4fb434f3ba4078171 to your computer and use it in GitHub Desktop.
{
"name": "Batch Process Prompts with OpenAI API",
"nodes": [
{
"parameters": {
"content": "### This workflow automates sending batched prompts to OpenAI. It submits multiple prompts at once and retrieves the results.\n\n#### How to use\n\nCall this workflow with array of `requests`\n\n```json\n[\n {\n \"requests\": [\n {\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Write me a short story\"\n },\n {\n \"role\": \"user\",\n \"content\": \"Write me a short story\"\n }\n ],\n \"custom_id\": \"1\",\n \"model\": \"gpt-3.5-turbo-0125\",\n \"method\": \"POST\",\n \"url\": \"/v1/chat/completions\",\n \"max_tokens\": 1000\n },\n {\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Write me a short story\"\n },\n {\n \"role\": \"user\",\n \"content\": \"Write me a short story\"\n }\n ],\n \"custom_id\": \"2\",\n \"model\": \"gpt-3.5-turbo-0125\",\n \"method\": \"POST\",\n \"url\": \"/v1/chat/completions\",\n \"max_tokens\": 1000\n }\n ],\n \"url\": \"/v1/chat/completions\",\n \"completion_window\": \"24h\"\n }\n]\n```\n",
"height": 520,
"width": 1060
},
"id": "2da2f766-9081-4cdb-bc67-a706d242d911",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
-1232,
288
],
"typeVersion": 1
},
{
"parameters": {
"content": "## Submit batch request to OpenAI's API",
"height": 300
},
"id": "3b9c11ca-e28f-45ad-a771-8adafd974a86",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-376,
836
],
"typeVersion": 1
},
{
"parameters": {
"content": "## Create jsonl from requests",
"height": 300,
"width": 304
},
"id": "729d5f5b-800e-4934-b673-7de8b96e279a",
"name": "Sticky Note6",
"type": "n8n-nodes-base.stickyNote",
"position": [
-632,
836
],
"typeVersion": 1
},
{
"parameters": {},
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
-1200,
1488
],
"id": "ecd33107-dcee-4012-8920-181c7a053793",
"name": "When clicking ‘Execute workflow’"
},
{
"parameters": {
"jsCode": "/** @type {import('n8n-workflow').INodeExecutionData[]} */\nconst items = (() => {\n // 1) Flatten all incoming requests (handles both [ {...}, {...} ] or [ [ {...} ] ] shapes)\n const rawRequests = $input\n .all()\n .flatMap(i => i.json.requests || [])\n .flat();\n \n // 2) Re‑shape each into your desired JSONL record\n const records = rawRequests.map((req) => ({\n custom_id: req.custom_id,\n method: req.method,\n url: req.url,\n body: {\n model: req.model,\n messages: req.messages,\n max_tokens: req.max_tokens,\n },\n }));\n \n // 3) Build the NDJSON string\n const jsonl = records.map(r => JSON.stringify(r)).join('\\n') + '\\n';\n \n // 4) Compute byte size and base64‑encode\n // @ts-ignore\n const sizeInBytes = Buffer.byteLength(jsonl, 'utf8');\n const sizeString = `${sizeInBytes} B`;\n // @ts-ignore\n const base64 = Buffer.from(jsonl, 'utf8').toString('base64');\n \n // 5) Package into the n8n binary format\n return [\n {\n json: {}, // or pass along any other JSON data you need downstream\n binary: {\n file: {\n data: base64,\n fileName: 'data.jsonl',\n mimeType: 'application/json',\n fileSize: sizeString,\n },\n },\n },\n ];\n})();\n\nreturn items;\n"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-528,
976
],
"id": "e8080562-0c8d-4c9a-a5b6-4a958d1220fd",
"name": "Create jsonl binary"
},
{
"parameters": {
"method": "POST",
"url": "https://api.openai.com/v1/files",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "openAiApi",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{}
]
},
"sendBody": true,
"contentType": "multipart-form-data",
"bodyParameters": {
"parameters": [
{
"name": "purpose",
"value": "batch"
},
{
"parameterType": "formBinaryData",
"name": "=file",
"inputDataFieldName": "=file"
}
]
},
"options": {}
},
"id": "21d88c6b-bf22-427d-838f-b59610c6406b",
"name": "Upload Batch FIle",
"type": "n8n-nodes-base.httpRequest",
"position": [
-304,
976
],
"typeVersion": 4.2,
"credentials": {
"anthropicApi": {
"id": "u9TC4yONSxjRgWfk",
"name": "Anthropic account"
},
"openAiApi": {
"id": "yfs0da1X2cMYo6Oy",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"content": "## Manual Example",
"height": 384,
"width": 256
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-1280,
1264
],
"id": "57272050-9018-42fc-96b2-429bf59960ac",
"name": "Sticky Note2"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "3a63e8fe-3477-4b7e-b162-3edc8a4dd4ae",
"leftValue": "={{ $json.requests }}",
"rightValue": "",
"operator": {
"type": "array",
"operation": "notEmpty",
"singleValue": true
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
-752,
1072
],
"id": "e6c72af6-bb17-4202-a90b-c7bc02350a4a",
"name": "If",
"alwaysOutputData": false
},
{
"parameters": {
"errorMessage": "messages not passed in"
},
"type": "n8n-nodes-base.stopAndError",
"typeVersion": 1,
"position": [
-528,
1168
],
"id": "e2b7851a-0feb-4a93-9f06-4f77cf3effeb",
"name": "Stop and Error"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "c320cfd1-6baf-46bf-9a28-3f0a7aaf3b1a",
"name": "user_prompt",
"value": "=Write me a short story",
"type": "string"
}
]
},
"includeOtherFields": true,
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-976,
1392
],
"id": "80fa0840-0870-48dc-ac92-70da7364b950",
"name": "User Prompt"
},
{
"parameters": {
"aggregate": "aggregateAllItemData",
"destinationFieldName": "requests",
"options": {}
},
"id": "7f487337-5e13-48da-8c3d-ddebd569f960",
"name": "Construct 'requests' array",
"type": "n8n-nodes-base.aggregate",
"position": [
-304,
1488
],
"typeVersion": 1
},
{
"parameters": {},
"id": "6a1d39f2-53d6-49dc-883b-a21a2de016cf",
"name": "Join two example requests into array",
"type": "n8n-nodes-base.merge",
"position": [
-528,
1488
],
"typeVersion": 3.1
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "6c677096-1454-4e85-b96d-64c8a225c5c5",
"name": "messages",
"value": "={{ \n [\n {\n role: \"user\",\n content: $json.user_prompt\n },\n {\n role: \"user\",\n content: $json.user_prompt\n }\n]\n}}",
"type": "array"
},
{
"id": "69ccc25b-5dd0-40ac-97cf-4a4b17631e16",
"name": "custom_id",
"value": "=1",
"type": "string"
},
{
"id": "2f2f5c35-9fcb-4c6b-87ec-3f5924dcd5eb",
"name": "model",
"value": "gpt-3.5-turbo-0125",
"type": "string"
},
{
"id": "f0511736-4ee6-40e6-864d-1233c1615971",
"name": "method",
"value": "POST",
"type": "string"
},
{
"id": "c2210498-a4a3-4c01-927c-4ccf5af869ed",
"name": "url",
"value": "/v1/chat/completions",
"type": "string"
},
{
"id": "d87defad-7e19-4e69-8612-727dcc115db1",
"name": "max_tokens",
"value": 1000,
"type": "number"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-752,
1392
],
"id": "44017470-bd79-4c01-b11d-3a2ce167e334",
"name": "Make Request Objects"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "c320cfd1-6baf-46bf-9a28-3f0a7aaf3b1a",
"name": "user_prompt",
"value": "=Write me a short story",
"type": "string"
}
]
},
"includeOtherFields": true,
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-976,
1584
],
"id": "817c9dd5-fe6d-4843-9cfb-cb9ee7b63e87",
"name": "User Prompt1"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "6c677096-1454-4e85-b96d-64c8a225c5c5",
"name": "messages",
"value": "={{ \n [\n {\n role: \"user\",\n content: $json.user_prompt\n },\n {\n role: \"user\",\n content: $json.user_prompt\n }\n]\n}}",
"type": "array"
},
{
"id": "69ccc25b-5dd0-40ac-97cf-4a4b17631e16",
"name": "custom_id",
"value": "=2",
"type": "string"
},
{
"id": "2f2f5c35-9fcb-4c6b-87ec-3f5924dcd5eb",
"name": "model",
"value": "gpt-3.5-turbo-0125",
"type": "string"
},
{
"id": "f0511736-4ee6-40e6-864d-1233c1615971",
"name": "method",
"value": "POST",
"type": "string"
},
{
"id": "c2210498-a4a3-4c01-927c-4ccf5af869ed",
"name": "url",
"value": "/v1/chat/completions",
"type": "string"
},
{
"id": "d87defad-7e19-4e69-8612-727dcc115db1",
"name": "max_tokens",
"value": 1000,
"type": "number"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-752,
1584
],
"id": "a69a8a16-afd3-497a-a1b2-a11a9ea8a6aa",
"name": "Make Request Objects1"
},
{
"parameters": {
"workflowInputs": {
"values": [
{
"name": "requests",
"type": "array"
},
{
"name": "url"
},
{
"name": "completion_window"
}
]
}
},
"id": "541d63bd-1f63-46af-995d-0a89ee3fba48",
"name": "OpenAI Batch",
"type": "n8n-nodes-base.executeWorkflowTrigger",
"position": [
-1200,
1072
],
"typeVersion": 1.1
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "fbba9259-20aa-4aff-aef1-3175140bf0d0",
"name": "requests",
"value": "={{ $('OpenAI Batch').item.json.requests }}",
"type": "array"
},
{
"id": "cdd5c39f-61b9-47ff-914b-6e027d3bf78b",
"name": "url",
"value": "={{ $('OpenAI Batch').item.json.url }}",
"type": "string"
},
{
"id": "9f74d39e-712d-4858-b296-6f3a8493f404",
"name": "completion_window",
"value": "={{ $json.completion_window }}",
"type": "string"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-976,
1072
],
"id": "959bde04-fe48-4f57-a8f7-ed6af337b76a",
"name": "Batch Workflow Obj"
},
{
"parameters": {
"rules": {
"values": [
{
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "d22eb449-7e34-4ad8-aa82-329f9123ec89",
"leftValue": "={{ $json.output_file_id }}",
"rightValue": "batch.completed",
"operator": {
"type": "string",
"operation": "exists",
"singleValue": true
}
}
],
"combinator": "and"
},
"renameOutput": true,
"outputKey": "batch.completed"
},
{
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"leftValue": "={{ $json.error_file_id }}",
"rightValue": "batch.failed",
"operator": {
"type": "string",
"operation": "exists",
"singleValue": true
},
"id": "e0df9173-b1a0-48c4-9164-f0a89995c6f0"
}
],
"combinator": "and"
},
"renameOutput": true,
"outputKey": "batch.failed"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.switch",
"typeVersion": 3.2,
"position": [
-752,
1904
],
"id": "fe259ad9-fb6f-4944-80cd-ab8038855f14",
"name": "Switch"
},
{
"parameters": {
"jsCode": "/**\n * @type {import('n8n-workflow').INodeExecutionData[]}\n */\nconst output = [];\n\n // 1. Grab the base64 payload from the incoming binary property\n // @ts-ignore\n const base64 = $input.first().binary.data.data\n // 2. Decode back to a JSONL string\n // @ts-ignore\n const jsonl = Buffer.from(base64, 'base64').toString('utf8');\n \n // 3. Split into lines (filtering out any empty trailing line)\n const lines = jsonl.split(/\\r?\\n/).filter(line => line.trim() !== '');\n \n // 4. Parse each line and push as its own item\n for (const line of lines) {\n output.push({\n json: JSON.parse(line),\n });\n }\n\nreturn output;\n"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-304,
1904
],
"id": "c3261326-3f26-4063-9d60-34c536b653e2",
"name": "Code"
},
{
"parameters": {
"url": "=https://api.openai.com/v1/files/{{ $json.error_file_id }}/content",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "openAiApi",
"options": {}
},
"id": "73a0a3e2-735c-437e-8653-2d135cb9d389",
"name": "Get Error File",
"type": "n8n-nodes-base.httpRequest",
"position": [
-528,
2000
],
"typeVersion": 4.2,
"credentials": {
"anthropicApi": {
"id": "u9TC4yONSxjRgWfk",
"name": "Anthropic account"
},
"openAiApi": {
"id": "yfs0da1X2cMYo6Oy",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"url": "=https://api.openai.com/v1/files/{{ $json.output_file_id }}/content",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "openAiApi",
"options": {}
},
"id": "9479f3ee-2f27-4d33-a96f-e3119d7af981",
"name": "Get Success File",
"type": "n8n-nodes-base.httpRequest",
"position": [
-528,
1808
],
"typeVersion": 4.2,
"credentials": {
"anthropicApi": {
"id": "u9TC4yONSxjRgWfk",
"name": "Anthropic account"
},
"openAiApi": {
"id": "yfs0da1X2cMYo6Oy",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"url": "=https://api.openai.com/v1/batches/{{ $json.body.data.id }}",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "openAiApi",
"options": {}
},
"id": "57bf0376-1dfa-41a9-9b46-f1891329ce7e",
"name": "Get Batch Info",
"type": "n8n-nodes-base.httpRequest",
"position": [
-976,
1904
],
"typeVersion": 4.2,
"credentials": {
"anthropicApi": {
"id": "u9TC4yONSxjRgWfk",
"name": "Anthropic account"
},
"openAiApi": {
"id": "yfs0da1X2cMYo6Oy",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"content": "## Start the Open AI Batch",
"height": 300
},
"id": "74e94573-647a-4148-9a15-4b21605274f0",
"name": "Sticky Note4",
"type": "n8n-nodes-base.stickyNote",
"position": [
-152,
836
],
"typeVersion": 1
},
{
"parameters": {
"method": "POST",
"url": "https://api.openai.com/v1/batches",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "openAiApi",
"sendBody": true,
"bodyParameters": {
"parameters": [
{
"name": "input_file_id",
"value": "={{ $json.id }}"
},
{
"name": "endpoint",
"value": "={{ $('Batch Workflow Obj').item.json.url }}"
},
{
"name": "completion_window",
"value": "={{ $('Batch Workflow Obj').item.json.completion_window }}"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-80,
976
],
"id": "c8d5cffb-73a8-4b72-9bf9-6a77d449224a",
"name": "Start OpenAI Batch",
"credentials": {
"openAiApi": {
"id": "yfs0da1X2cMYo6Oy",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"workflowId": {
"__rl": true,
"value": "sArPfjmJHTNf0W0H",
"mode": "list",
"cachedResultName": "Batch Process Prompts with OpenAI API"
},
"workflowInputs": {
"mappingMode": "defineBelow",
"value": {
"url": "/v1/chat/completions",
"requests": "={{ $json.requests }}",
"completion_window": "24h"
},
"matchingColumns": [],
"schema": [
{
"id": "requests",
"displayName": "requests",
"required": false,
"defaultMatch": false,
"display": true,
"canBeUsedToMatch": true,
"type": "array"
},
{
"id": "url",
"displayName": "url",
"required": false,
"defaultMatch": false,
"display": true,
"canBeUsedToMatch": true,
"type": "string"
},
{
"id": "completion_window",
"displayName": "completion_window",
"required": false,
"defaultMatch": false,
"display": true,
"canBeUsedToMatch": true,
"type": "string"
}
],
"attemptToConvertTypes": false,
"convertFieldsToString": true
},
"options": {}
},
"type": "n8n-nodes-base.executeWorkflow",
"typeVersion": 1.2,
"position": [
-80,
1488
],
"id": "813a0766-a652-4c18-8099-2d5359e32656",
"name": "Execute Workflow"
},
{
"parameters": {
"content": "## Add a webhook here!\n\nMake sure its a POST"
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-1264,
1888
],
"id": "d1474428-7eea-4743-b85d-caad7d34a99e",
"name": "Sticky Note1"
}
],
"pinData": {},
"connections": {
"When clicking ‘Execute workflow’": {
"main": [
[
{
"node": "User Prompt",
"type": "main",
"index": 0
},
{
"node": "User Prompt1",
"type": "main",
"index": 0
}
]
]
},
"Create jsonl binary": {
"main": [
[
{
"node": "Upload Batch FIle",
"type": "main",
"index": 0
}
]
]
},
"Upload Batch FIle": {
"main": [
[
{
"node": "Start OpenAI Batch",
"type": "main",
"index": 0
}
]
]
},
"If": {
"main": [
[
{
"node": "Create jsonl binary",
"type": "main",
"index": 0
}
],
[
{
"node": "Stop and Error",
"type": "main",
"index": 0
}
]
]
},
"User Prompt": {
"main": [
[
{
"node": "Make Request Objects",
"type": "main",
"index": 0
}
]
]
},
"Join two example requests into array": {
"main": [
[
{
"node": "Construct 'requests' array",
"type": "main",
"index": 0
}
]
]
},
"Make Request Objects": {
"main": [
[
{
"node": "Join two example requests into array",
"type": "main",
"index": 0
}
]
]
},
"User Prompt1": {
"main": [
[
{
"node": "Make Request Objects1",
"type": "main",
"index": 0
}
]
]
},
"Construct 'requests' array": {
"main": [
[
{
"node": "Execute Workflow",
"type": "main",
"index": 0
}
]
]
},
"Make Request Objects1": {
"main": [
[
{
"node": "Join two example requests into array",
"type": "main",
"index": 1
}
]
]
},
"OpenAI Batch": {
"main": [
[
{
"node": "Batch Workflow Obj",
"type": "main",
"index": 0
}
]
]
},
"Batch Workflow Obj": {
"main": [
[
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"Switch": {
"main": [
[
{
"node": "Get Success File",
"type": "main",
"index": 0
}
],
[
{
"node": "Get Error File",
"type": "main",
"index": 0
}
]
]
},
"Get Error File": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
}
]
]
},
"Get Success File": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
}
]
]
},
"Get Batch Info": {
"main": [
[
{
"node": "Switch",
"type": "main",
"index": 0
}
]
]
},
"Start OpenAI Batch": {
"main": [
[]
]
},
"Code": {
"main": [
[]
]
}
},
"active": true,
"settings": {
"executionOrder": "v1"
},
"versionId": "d7040e73-1dc8-4ca4-90d1-f0b5eb88a296",
"meta": {
"templateId": "3409",
"templateCredsSetupCompleted": true,
"instanceId": "7e291d42472d0a62fb5db1fe075ca3c4d7f11b88deb452e7f508d711b084ce15"
},
"id": "sArPfjmJHTNf0W0H",
"tags": [
{
"createdAt": "2025-07-20T20:46:54.536Z",
"updatedAt": "2025-07-20T20:46:54.536Z",
"id": "KNOpvGK2yn8fPMXC",
"name": "AI Workflows"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment