Skip to content
Merged
Changes from all 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
113 changes: 100 additions & 13 deletions src/oss/langchain/middleware/built-in.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
<Accordion title="Configuration options">

:::python
<ParamField body="tools" type="list[str | BaseTool]">
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.
</ParamField>

<ParamField body="model" type="string | BaseChatModel">
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.
</ParamField>
:::

<ParamField body="model" type="string | BaseChatModel" default="anthropic:claude-3-5-sonnet-latest">
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.
:::js
<ParamField body="tools" type="(string | ClientTool | ServerTool)[]">
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.
</ParamField>

</Accordion>
<ParamField body="model" type="string | BaseChatModel">
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.
</ParamField>
:::

:::python
</Accordion>

<Accordion title="Full example">

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
Expand All @@ -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],
Expand All @@ -1394,17 +1413,85 @@ 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="claude-sonnet-4-5-20250929")],
middleware=[LLMToolEmulator(model="anthropic:claude-sonnet-4-5-20250929")],
)
```
:::

</Accordion>
:::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 for emulation
const agent5 = createAgent({
model: "gpt-4o",
tools: [getWeather, sendEmail],
middleware: [
toolEmulatorMiddleware({
model: "anthropic:claude-sonnet-4-5-20250929",
}),
],
});
```
:::

</Accordion>

### Context editing

Expand Down
Loading