Skip to content

Commit a6b8b31

Browse files
fix(langchain): add JS docs for tool emulator (#1416)
Adding docs as we already ported the middleware to JS.
1 parent cdcde49 commit a6b8b31

File tree

1 file changed

+100
-13
lines changed

1 file changed

+100
-13
lines changed

src/oss/langchain/middleware/built-in.mdx

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,29 +1471,48 @@ agent = create_agent(
14711471
:::
14721472

14731473
:::js
1474-
This middleware is only available in Python. For JavaScript/TypeScript, consider creating mock tool implementations for testing.
1474+
```typescript
1475+
import { createAgent, toolEmulatorMiddleware } from "langchain";
1476+
1477+
const agent = createAgent({
1478+
model: "gpt-4o",
1479+
tools: [getWeather, searchDatabase, sendEmail],
1480+
middleware: [
1481+
toolEmulatorMiddleware(), // Emulate all tools
1482+
],
1483+
});
1484+
```
14751485
:::
14761486

1477-
:::python
14781487
<Accordion title="Configuration options">
14791488

1489+
:::python
14801490
<ParamField body="tools" type="list[str | BaseTool]">
1481-
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.
1491+
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.
1492+
</ParamField>
1493+
1494+
<ParamField body="model" type="string | BaseChatModel">
1495+
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.
14821496
</ParamField>
1497+
:::
14831498

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

1488-
</Accordion>
1504+
<ParamField body="model" type="string | BaseChatModel">
1505+
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.
1506+
</ParamField>
14891507
:::
14901508

1491-
:::python
1509+
</Accordion>
1510+
14921511
<Accordion title="Full example">
14931512

14941513
The middleware uses an LLM to generate plausible responses for tool calls instead of executing the actual tools.
14951514

1496-
1515+
:::python
14971516
```python
14981517
from langchain.agents import create_agent
14991518
from langchain.agents.middleware import LLMToolEmulator
@@ -1511,7 +1530,7 @@ def send_email(to: str, subject: str, body: str) -> str:
15111530
return "Email sent"
15121531

15131532

1514-
# Emulate all tools
1533+
# Emulate all tools (default behavior)
15151534
agent = create_agent(
15161535
model="gpt-4o",
15171536
tools=[get_weather, send_email],
@@ -1525,17 +1544,85 @@ agent2 = create_agent(
15251544
middleware=[LLMToolEmulator(tools=["get_weather"])],
15261545
)
15271546

1528-
# Use custom model
1529-
agent3 = create_agent(
1547+
# Use custom model for emulation
1548+
agent4 = create_agent(
15301549
model="gpt-4o",
15311550
tools=[get_weather, send_email],
1532-
middleware=[LLMToolEmulator(model="claude-sonnet-4-5-20250929")],
1551+
middleware=[LLMToolEmulator(model="anthropic:claude-sonnet-4-5-20250929")],
15331552
)
15341553
```
1554+
:::
15351555

1536-
</Accordion>
1556+
:::js
1557+
```typescript
1558+
import { createAgent, toolEmulatorMiddleware, tool } from "langchain";
1559+
import * as z from "zod";
1560+
1561+
const getWeather = tool(
1562+
async ({ location }) => `Weather in ${location}`,
1563+
{
1564+
name: "get_weather",
1565+
description: "Get the current weather for a location",
1566+
schema: z.object({ location: z.string() }),
1567+
}
1568+
);
1569+
1570+
const sendEmail = tool(
1571+
async ({ to, subject, body }) => "Email sent",
1572+
{
1573+
name: "send_email",
1574+
description: "Send an email",
1575+
schema: z.object({
1576+
to: z.string(),
1577+
subject: z.string(),
1578+
body: z.string(),
1579+
}),
1580+
}
1581+
);
1582+
1583+
// Emulate all tools (default behavior)
1584+
const agent = createAgent({
1585+
model: "gpt-4o",
1586+
tools: [getWeather, sendEmail],
1587+
middleware: [toolEmulatorMiddleware()],
1588+
});
1589+
1590+
// Emulate specific tools by name
1591+
const agent2 = createAgent({
1592+
model: "gpt-4o",
1593+
tools: [getWeather, sendEmail],
1594+
middleware: [
1595+
toolEmulatorMiddleware({
1596+
tools: ["get_weather"],
1597+
}),
1598+
],
1599+
});
1600+
1601+
// Emulate specific tools by passing tool instances
1602+
const agent3 = createAgent({
1603+
model: "gpt-4o",
1604+
tools: [getWeather, sendEmail],
1605+
middleware: [
1606+
toolEmulatorMiddleware({
1607+
tools: [getWeather],
1608+
}),
1609+
],
1610+
});
1611+
1612+
// Use custom model for emulation
1613+
const agent5 = createAgent({
1614+
model: "gpt-4o",
1615+
tools: [getWeather, sendEmail],
1616+
middleware: [
1617+
toolEmulatorMiddleware({
1618+
model: "anthropic:claude-sonnet-4-5-20250929",
1619+
}),
1620+
],
1621+
});
1622+
```
15371623
:::
15381624

1625+
</Accordion>
15391626

15401627
### Context editing
15411628

0 commit comments

Comments
 (0)