Skip to content
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

fix(AI Agent Node): Improve Tools agent empty tool input message #9622

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/@n8n/nodes-langchain/nodes/agents/Agent/Agent.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ const agentTypeProperty: INodeProperties = {
name: 'agent',
type: 'options',
noDataExpression: true,
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
options: [
{
name: 'Tools Agent',
value: 'toolsAgent',
description:
'Utilized unified Tool calling interface to select the appropriate tools and argument for execution',
},
{
name: 'Conversational Agent',
value: 'conversationalAgent',
Expand Down Expand Up @@ -219,12 +226,6 @@ const agentTypeProperty: INodeProperties = {
value: 'sqlAgent',
description: 'Answers questions about data in an SQL database',
},
{
name: 'Tools Agent',
value: 'toolsAgent',
description:
'Utilized unified Tool calling interface to select the appropriate tools and argument for execution',
},
],
default: '',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { ZodObject } from 'zod';
import { z } from 'zod';
import type { BaseOutputParser, StructuredOutputParser } from '@langchain/core/output_parsers';
import { OutputFixingParser } from 'langchain/output_parsers';
import type { APIError } from 'openai/error';
import {
isChatInstance,
getPromptInputByType,
Expand All @@ -21,6 +22,12 @@ import {
} from '../../../../../utils/helpers';
import { SYSTEM_MESSAGE } from './prompt';

function isOpenAiApiError(error: unknown): error is APIError {
return (
!!error && typeof error === 'object' && 'type' in error && 'param' in error && 'code' in error
);
}

function getOutputParserSchema(outputParser: BaseOutputParser): ZodObject<any, any, any, any> {
const parserType = outputParser.lc_namespace[outputParser.lc_namespace.length - 1];
let schema: ZodObject<any, any, any, any>;
Expand Down Expand Up @@ -159,7 +166,7 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE
input,
system_message: options.systemMessage ?? SYSTEM_MESSAGE,
formatting_instructions:
'IMPORTANT: Always call `format_final_response` to format your final response!', //outputParser?.getFormatInstructions(),
'IMPORTANT: Always call `format_final_response` to format your final response!',
});

returnData.push({
Expand All @@ -173,12 +180,29 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE
),
});
} catch (error) {
let errorOverride = error;

// OpenAI doesn't allow empty tools array so we will provide a more user-friendly error message
if (
isOpenAiApiError(error) &&
error.type === 'invalid_request_error' &&
error.param?.includes('tools') &&
error.code?.includes('empty_array')
) {
errorOverride = new NodeOperationError(
this.getNode(),
"Please connect at least one tool. If you don't need any, try the conversational agent instead",
);
}
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: itemIndex } });
returnData.push({
json: { error: errorOverride?.message },
pairedItem: { item: itemIndex },
});
continue;
}

throw error;
throw errorOverride;
}
}

Expand Down
Loading