fix(Anthropic Chat Model Node): Fix LmChatAnthropic node to work when both thinking is enabled and tools used (#16010)

This commit is contained in:
Yiorgis Gozadinos
2025-06-05 13:33:51 +02:00
committed by GitHub
parent 0bea193814
commit e662998c67
7 changed files with 177 additions and 141 deletions

View File

@@ -75,8 +75,8 @@ export class ToolExecutor implements INodeType {
}
} else {
// Handle single tool
if (!toolName || toolName === (tool as Tool).name) {
const result = await executeTool(tool as Tool, parsedQuery);
if (!toolName || toolName === tool.name) {
const result = await executeTool(tool, parsedQuery);
resultData.push(result);
}
}

View File

@@ -1,12 +1,9 @@
import type { StructuredTool } from 'langchain/tools';
import type { Tool } from '@langchain/core/tools';
import { type IDataObject, type INodeExecutionData } from 'n8n-workflow';
import { convertObjectBySchema } from './convertToSchema';
export async function executeTool(
tool: StructuredTool,
query: string | object,
): Promise<INodeExecutionData> {
export async function executeTool(tool: Tool, query: string | object): Promise<INodeExecutionData> {
let convertedQuery: string | object = query;
if ('schema' in tool && tool.schema) {
convertedQuery = convertObjectBySchema(query, tool.schema);

View File

@@ -1,4 +1,4 @@
import type { StructuredTool } from '@langchain/core/tools';
import type { Tool } from '@langchain/core/tools';
import type { OpenAIClient } from '@langchain/openai';
import { zodToJsonSchema } from 'zod-to-json-schema';
@@ -6,13 +6,13 @@ import { zodToJsonSchema } from 'zod-to-json-schema';
// since these functions are not exported
/**
* Formats a `StructuredTool` instance into a format that is compatible
* Formats a `Tool` instance into a format that is compatible
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
* function to convert the schema of the `StructuredTool` into a JSON
* function to convert the schema of the tool into a JSON
* schema, which is then used as the parameters for the OpenAI function.
*/
export function formatToOpenAIFunction(
tool: StructuredTool,
tool: Tool,
): OpenAIClient.Chat.ChatCompletionCreateParams.Function {
return {
name: tool.name,
@@ -21,7 +21,7 @@ export function formatToOpenAIFunction(
};
}
export function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionTool {
export function formatToOpenAITool(tool: Tool): OpenAIClient.Chat.ChatCompletionTool {
const schema = zodToJsonSchema(tool.schema);
return {
type: 'function',
@@ -33,7 +33,7 @@ export function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.Chat
};
}
export function formatToOpenAIAssistantTool(tool: StructuredTool): OpenAIClient.Beta.AssistantTool {
export function formatToOpenAIAssistantTool(tool: Tool): OpenAIClient.Beta.AssistantTool {
return {
type: 'function',
function: {

View File

@@ -1,8 +1,8 @@
import { DynamicStructuredTool, type DynamicStructuredToolInput } from '@langchain/core/tools';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { CompatibilityCallToolResultSchema } from '@modelcontextprotocol/sdk/types.js';
import { Toolkit } from 'langchain/agents';
import { DynamicStructuredTool, type DynamicStructuredToolInput } from 'langchain/tools';
import {
createResultError,
createResultOk,
@@ -10,7 +10,7 @@ import {
type IExecuteFunctions,
type Result,
} from 'n8n-workflow';
import { type ZodTypeAny } from 'zod';
import { z } from 'zod';
import { convertJsonSchemaToZod } from '@utils/schemaParsing';
@@ -99,18 +99,24 @@ export const createCallTool =
export function mcpToolToDynamicTool(
tool: McpTool,
onCallTool: DynamicStructuredToolInput['func'],
) {
): DynamicStructuredTool<z.ZodObject<any, any, any, any>> {
const rawSchema = convertJsonSchemaToZod(tool.inputSchema);
// Ensure we always have an object schema for structured tools
const objectSchema =
rawSchema instanceof z.ZodObject ? rawSchema : z.object({ value: rawSchema });
return new DynamicStructuredTool({
name: tool.name,
description: tool.description ?? '',
schema: convertJsonSchemaToZod(tool.inputSchema),
schema: objectSchema,
func: onCallTool,
metadata: { isFromToolkit: true },
});
}
export class McpToolkit extends Toolkit {
constructor(public tools: Array<DynamicStructuredTool<ZodTypeAny>>) {
constructor(public tools: Array<DynamicStructuredTool<z.ZodObject<any, any, any, any>>>) {
super();
}
}

View File

@@ -1,5 +1,5 @@
import type { BaseMessage } from '@langchain/core/messages';
import type { StructuredTool } from '@langchain/core/tools';
import type { Tool } from '@langchain/core/tools';
import type { OpenAIClient } from '@langchain/openai';
import type { BufferWindowMemory } from 'langchain/memory';
import { zodToJsonSchema } from 'zod-to-json-schema';
@@ -8,13 +8,13 @@ import { zodToJsonSchema } from 'zod-to-json-schema';
// since these functions are not exported
/**
* Formats a `StructuredTool` instance into a format that is compatible
* Formats a `Tool` instance into a format that is compatible
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
* function to convert the schema of the `StructuredTool` into a JSON
* function to convert the schema of the tool into a JSON
* schema, which is then used as the parameters for the OpenAI function.
*/
export function formatToOpenAIFunction(
tool: StructuredTool,
tool: Tool,
): OpenAIClient.Chat.ChatCompletionCreateParams.Function {
return {
name: tool.name,
@@ -23,7 +23,7 @@ export function formatToOpenAIFunction(
};
}
export function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionTool {
export function formatToOpenAITool(tool: Tool): OpenAIClient.Chat.ChatCompletionTool {
const schema = zodToJsonSchema(tool.schema);
return {
type: 'function',
@@ -35,7 +35,7 @@ export function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.Chat
};
}
export function formatToOpenAIAssistantTool(tool: StructuredTool): OpenAIClient.Beta.AssistantTool {
export function formatToOpenAIAssistantTool(tool: Tool): OpenAIClient.Beta.AssistantTool {
return {
type: 'function',
function: {