From d0697b99cb01556e29d73eddee060e6fb0261936 Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Thu, 13 Nov 2025 17:51:45 -0800 Subject: [PATCH 1/2] fix(langchain): add JS docs for tool emulator --- src/oss/langchain/middleware/built-in.mdx | 109 +++++++++++++++++++--- 1 file changed, 98 insertions(+), 11 deletions(-) diff --git a/src/oss/langchain/middleware/built-in.mdx b/src/oss/langchain/middleware/built-in.mdx index f08acb05f7..9c1a6aa08b 100644 --- a/src/oss/langchain/middleware/built-in.mdx +++ b/src/oss/langchain/middleware/built-in.mdx @@ -1340,29 +1340,48 @@ agent = create_agent( ::: :::js -This middleware is only available in Python. For JavaScript/TypeScript, consider creating mock tool implementations for testing. +```typescript +import { createAgent, toolEmulatorMiddleware } from "langchain"; + +const agent = createAgent({ + model: "gpt-4o", + tools: [getWeather, searchDatabase, sendEmail], + middleware: [ + toolEmulatorMiddleware(), // Emulate all tools + ], +}); +``` ::: -:::python +:::python - List of tool names (str) or BaseTool instances to emulate. If `None` (default), ALL tools will be emulated. If empty list, no tools will be emulated. + List of tool names (str) or BaseTool instances to emulate. If `None` (default), ALL tools will be emulated. If empty list `[]`, no tools will be emulated. If array with tool names/instances, only those tools will be emulated. - - Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'openai:gpt-4o-mini'`) or a `BaseChatModel` instance. See @[`init_chat_model`][init_chat_model(model)] for more information. + + Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'anthropic:claude-sonnet-4-5-20250929'`) or a `BaseChatModel` instance. See @[`init_chat_model`][init_chat_model(model)] for more information. +::: - +:::js + + List of tool names (string) or tool instances to emulate. If `undefined` (default), ALL tools will be emulated. If empty array `[]`, no tools will be emulated. If array with tool names/instances, only those tools will be emulated. + + + + Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'anthropic:claude-sonnet-4-5-20250929'`) or a `BaseChatModel` instance. + ::: -:::python + + The middleware uses an LLM to generate plausible responses for tool calls instead of executing the actual tools. - +:::python ```python from langchain.agents import create_agent from langchain.agents.middleware import LLMToolEmulator @@ -1380,7 +1399,7 @@ def send_email(to: str, subject: str, body: str) -> str: return "Email sent" -# Emulate all tools +# Emulate all tools (default behavior) agent = create_agent( model="gpt-4o", tools=[get_weather, send_email], @@ -1398,13 +1417,81 @@ agent2 = create_agent( agent3 = create_agent( model="gpt-4o", tools=[get_weather, send_email], - middleware=[LLMToolEmulator(model="claude-sonnet-4-5-20250929")], + middleware=[LLMToolEmulator(model="anthropic:claude-sonnet-4-5-20250929")], ) ``` +::: - +:::js +```typescript +import { createAgent, toolEmulatorMiddleware, tool } from "langchain"; +import * as z from "zod"; + +const getWeather = tool( + async ({ location }) => `Weather in ${location}`, + { + name: "get_weather", + description: "Get the current weather for a location", + schema: z.object({ location: z.string() }), + } +); + +const sendEmail = tool( + async ({ to, subject, body }) => "Email sent", + { + name: "send_email", + description: "Send an email", + schema: z.object({ + to: z.string(), + subject: z.string(), + body: z.string(), + }), + } +); + +// Emulate all tools (default behavior) +const agent = createAgent({ + model: "gpt-4o", + tools: [getWeather, sendEmail], + middleware: [toolEmulatorMiddleware()], +}); + +// Emulate specific tools by name +const agent2 = createAgent({ + model: "gpt-4o", + tools: [getWeather, sendEmail], + middleware: [ + toolEmulatorMiddleware({ + tools: ["get_weather"], + }), + ], +}); + +// Emulate specific tools by passing tool instances +const agent3 = createAgent({ + model: "gpt-4o", + tools: [getWeather, sendEmail], + middleware: [ + toolEmulatorMiddleware({ + tools: [getWeather], + }), + ], +}); + +// Use custom model +const agent4 = createAgent({ + model: "gpt-4o", + tools: [getWeather, sendEmail], + middleware: [ + toolEmulatorMiddleware({ + model: "anthropic:claude-sonnet-4-5-20250929", + }), + ], +}); +``` ::: + ### Context editing From 6cf11e394caa953c5fce76c8fefc561c81d472be Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Thu, 13 Nov 2025 19:10:37 -0800 Subject: [PATCH 2/2] cr --- src/oss/langchain/middleware/built-in.mdx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/oss/langchain/middleware/built-in.mdx b/src/oss/langchain/middleware/built-in.mdx index 9c1a6aa08b..1a7eb94b5f 100644 --- a/src/oss/langchain/middleware/built-in.mdx +++ b/src/oss/langchain/middleware/built-in.mdx @@ -1360,8 +1360,8 @@ const agent = createAgent({ List of tool names (str) or BaseTool instances to emulate. If `None` (default), ALL tools will be emulated. If empty list `[]`, no tools will be emulated. If array with tool names/instances, only those tools will be emulated. - - Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'anthropic:claude-sonnet-4-5-20250929'`) or a `BaseChatModel` instance. See @[`init_chat_model`][init_chat_model(model)] for more information. + + Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'anthropic:claude-sonnet-4-5-20250929'`) or a `BaseChatModel` instance. Defaults to the agent's model if not specified. See @[`init_chat_model`][init_chat_model(model)] for more information. ::: @@ -1370,8 +1370,8 @@ const agent = createAgent({ List of tool names (string) or tool instances to emulate. If `undefined` (default), ALL tools will be emulated. If empty array `[]`, no tools will be emulated. If array with tool names/instances, only those tools will be emulated. - - Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'anthropic:claude-sonnet-4-5-20250929'`) or a `BaseChatModel` instance. + + Model to use for generating emulated tool responses. Can be a model identifier string (e.g., `'anthropic:claude-sonnet-4-5-20250929'`) or a `BaseChatModel` instance. Defaults to the agent's model if not specified. ::: @@ -1413,8 +1413,8 @@ agent2 = create_agent( middleware=[LLMToolEmulator(tools=["get_weather"])], ) -# Use custom model -agent3 = create_agent( +# Use custom model for emulation +agent4 = create_agent( model="gpt-4o", tools=[get_weather, send_email], middleware=[LLMToolEmulator(model="anthropic:claude-sonnet-4-5-20250929")], @@ -1478,8 +1478,8 @@ const agent3 = createAgent({ ], }); -// Use custom model -const agent4 = createAgent({ +// Use custom model for emulation +const agent5 = createAgent({ model: "gpt-4o", tools: [getWeather, sendEmail], middleware: [