Split and Combine Batch ID800
🧩 Syntax:
// This single node replaces 'Split Batch Manually' and 'Combine Data'.
// It's robust and solves the "lost data" problem.
// 1. Get the single item output from the LLM node.
const llmOutputItem = $input.item;
let analysisResultsArray;
// 2. Safely parse the LLM output. The LangChain node can be inconsistent;
// sometimes it returns a parsed object, other times a raw text string.
// This code handles both possibilities gracefully.
if (llmOutputItem.json.analysisResults) {
// Case 1: The output is already a parsed object.
analysisResultsArray = llmOutputItem.json.analysisResults;
} else if (llmOutputItem.json.text) {
// Case 2: The output is a string that needs to be parsed.
try {
analysisResultsArray = JSON.parse(llmOutputItem.json.text).analysisResults;
} catch (e) {
throw new Error(`Failed to parse JSON from the LLM's text output. Error: ${e.message}`);
}
}
// 3. If we still don't have a valid array, we cannot proceed. Throw a clear error.
if (!Array.isArray(analysisResultsArray)) {
throw new Error(`Could not find or parse the 'analysisResults' array from the LLM output. Received data: ${JSON.stringify(llmOutputItem.json)}`);
}
// 4. Get the original batch data from the 'Generate Batch Prompt' node.
// This is the key step that solves the problem of the lost data.
const originalBatch = $('Generate Batch Prompt').first().json.fileBatch;
// 5. Loop through each analysis result and merge it with its corresponding original data.
const finalMergedItems = analysisResultsArray.map(llmResult => {
// Find the corresponding original file using the full filePath as a unique key.
const originalFileWrapper = originalBatch.find(f => f.originalItem.filePath === llmResult.filePath);
if (!originalFileWrapper) {
// This case handles if the LLM fails to return a matching filePath.
// We return an item that clearly indicates a merge failure for later debugging.
return {
json: {
...llmResult,
chunkingStrategy: 'merge_failed',
reasoning: `Combine Data: Could not find matching original file for path: ${llmResult.filePath}`
}
};
}
// Create the final, clean JSON object by combining the original data with the new analysis.
const finalJson = {
...originalFileWrapper.originalItem,
...llmResult
};
return { json: finalJson };
});
// 6. Return the complete list of successfully merged items to the next node.
return finalMergedItems;Balih00
Member