Skip to content

Commit 001503f

Browse files
mike12345567konstantintieber
authored andcommitted
fix(Agent Node): Thinking model issue - undefined .map error (#22046)
1 parent 00941cd commit 001503f

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

packages/@n8n/nodes-langchain/nodes/agents/Agent/agents/ToolsAgent/common.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,35 @@ export function handleAgentFinishOutput(
206206
if (agentFinishSteps.returnValues) {
207207
const isMultiOutput = Array.isArray(agentFinishSteps.returnValues?.output);
208208
if (isMultiOutput) {
209-
// If all items in the multi-output array are of type 'text', merge them into a single string
210209
const multiOutputSteps = agentFinishSteps.returnValues.output as Array<{
211210
index: number;
212211
type: string;
213-
text: string;
212+
text?: string;
213+
thinking?: string;
214214
}>;
215-
const isTextOnly = multiOutputSteps.every((output) => 'text' in output);
216-
if (isTextOnly) {
217-
agentFinishSteps.returnValues.output = multiOutputSteps
218-
.map((output) => output.text)
215+
216+
// Filter out thinking blocks and join text blocks
217+
const textOutputs = multiOutputSteps
218+
.filter((output) => output.type === 'text' && output.text)
219+
.map((output) => output.text)
220+
.join('\n')
221+
.trim();
222+
223+
if (textOutputs) {
224+
agentFinishSteps.returnValues.output = textOutputs;
225+
} else {
226+
const thinkingOutputs = multiOutputSteps
227+
.filter((output) => output.type === 'thinking' && output.thinking)
228+
.map((output) => output.thinking)
219229
.join('\n')
220230
.trim();
231+
232+
if (thinkingOutputs) {
233+
agentFinishSteps.returnValues.output = thinkingOutputs;
234+
} else {
235+
// no output was found
236+
agentFinishSteps.returnValues.output = '';
237+
}
221238
}
222239
return agentFinishSteps;
223240
}

packages/@n8n/nodes-langchain/nodes/agents/Agent/test/ToolsAgent/commons.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,49 @@ describe('handleAgentFinishOutput', () => {
831831

832832
expect(result).toEqual(steps);
833833
});
834+
835+
it('should filter out thinking blocks and return only text blocks', () => {
836+
const steps: AgentFinish = {
837+
returnValues: {
838+
output: [
839+
{ index: 0, type: 'thinking', thinking: 'Internal reasoning...' },
840+
{ index: 1, type: 'text', text: 'User-facing output' },
841+
],
842+
},
843+
log: '',
844+
};
845+
846+
const result = handleAgentFinishOutput(steps) as AgentFinish;
847+
848+
expect(result.returnValues.output).toBe('User-facing output');
849+
});
850+
851+
it('should return thinking content when no text blocks exist', () => {
852+
const steps: AgentFinish = {
853+
returnValues: {
854+
output: [
855+
{ index: 0, type: 'thinking', thinking: 'Only thinking content' },
856+
{ index: 1, type: 'thinking', thinking: 'More thinking' },
857+
],
858+
},
859+
log: '',
860+
};
861+
862+
const result = handleAgentFinishOutput(steps) as AgentFinish;
863+
864+
expect(result.returnValues.output).toBe('Only thinking content\nMore thinking');
865+
});
866+
867+
it('should return empty string when no text or thinking blocks exist', () => {
868+
const steps: AgentFinish = {
869+
returnValues: {
870+
output: [{ index: 0, type: 'unknown' }],
871+
},
872+
log: '',
873+
};
874+
875+
const result = handleAgentFinishOutput(steps) as AgentFinish;
876+
877+
expect(result.returnValues.output).toBe('');
878+
});
834879
});

0 commit comments

Comments
 (0)