fix(AI Agent Node): Prevent adding empty binary message (#14871)

This commit is contained in:
jeanpaul
2025-04-24 15:59:38 +02:00
committed by GitHub
parent 11379bf656
commit 897338bd24
2 changed files with 32 additions and 1 deletions

View File

@@ -365,7 +365,11 @@ export async function prepareMessages(
const hasBinaryData = ctx.getInputData()?.[itemIndex]?.binary !== undefined; const hasBinaryData = ctx.getInputData()?.[itemIndex]?.binary !== undefined;
if (hasBinaryData && options.passthroughBinaryImages) { if (hasBinaryData && options.passthroughBinaryImages) {
const binaryMessage = await extractBinaryMessages(ctx, itemIndex); const binaryMessage = await extractBinaryMessages(ctx, itemIndex);
messages.push(binaryMessage); if (binaryMessage.content.length !== 0) {
messages.push(binaryMessage);
} else {
ctx.logger.debug('Not attaching binary message, since its content was empty');
}
} }
// We add the agent scratchpad last, so that the agent will not run in loops // We add the agent scratchpad last, so that the agent will not run in loops

View File

@@ -233,6 +233,33 @@ describe('prepareMessages', () => {
expect(hasHumanMessage).toBe(false); expect(hasHumanMessage).toBe(false);
}); });
it('should not include a binary message if no image data is present', async () => {
const fakeItem = {
json: {},
binary: {
img1: {
mimeType: 'application/pdf',
data: 'data:application/pdf;base64,sampledata',
},
},
};
mockContext.getInputData.mockReturnValue([fakeItem]);
mockContext.logger = {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
const messages = await prepareMessages(mockContext, 0, {
systemMessage: 'Test system',
passthroughBinaryImages: true,
});
const hasHumanMessage = messages.some((m) => m instanceof HumanMessage);
expect(hasHumanMessage).toBe(false);
expect(mockContext.logger.debug).toHaveBeenCalledTimes(1);
});
it('should not include system_message in prompt templates if not provided after version 1.9', async () => { it('should not include system_message in prompt templates if not provided after version 1.9', async () => {
const fakeItem = { json: {} }; const fakeItem = { json: {} };
const mockNode = mock<INode>(); const mockNode = mock<INode>();