Skip to content

Commit 0d0b8e9

Browse files
aronnicoalbanese
andcommitted
chore(ai): document the new addToolResult interface
Co-authored-by: Nico Albanese <[email protected]>
1 parent 30e982a commit 0d0b8e9

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

content/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,59 @@ export default function Chat() {
296296
}
297297
```
298298

299+
### Error handling
300+
301+
Sometimes an error may occur during client-side tool execution. Use the `addToolResult` method with a `state` of `output-error` and `errorText` value instead of `output` record the error.
302+
303+
```tsx filename='app/page.tsx' highlight="19,36-41"
304+
'use client';
305+
306+
import { useChat } from '@ai-sdk/react';
307+
import {
308+
DefaultChatTransport,
309+
lastAssistantMessageIsCompleteWithToolCalls,
310+
} from 'ai';
311+
import { useState } from 'react';
312+
313+
export default function Chat() {
314+
const { messages, sendMessage, addToolResult } = useChat({
315+
transport: new DefaultChatTransport({
316+
api: '/api/chat',
317+
}),
318+
319+
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
320+
321+
// run client-side tools that are automatically executed:
322+
async onToolCall({ toolCall }) {
323+
// Check if it's a dynamic tool first for proper type narrowing
324+
if (toolCall.dynamic) {
325+
return;
326+
}
327+
328+
if (toolCall.toolName === 'getWeatherInformation') {
329+
try {
330+
const weather = await getWeatherInformation(toolCall.input);
331+
332+
// No await - avoids potential deadlocks
333+
addToolResult({
334+
tool: 'getWeatherInformation',
335+
toolCallId: toolCall.toolCallId,
336+
output: weather,
337+
});
338+
} catch (err) {
339+
addToolResult({
340+
tool: 'getWeatherInformation',
341+
toolCallId: toolCall.toolCallId,
342+
state: 'output-error',
343+
errorText: 'Unable to get the weather information',
344+
});
345+
}
346+
}
347+
},
348+
});
349+
}
350+
```
351+
299352
## Dynamic Tools
300353

301354
When using dynamic tools (tools with unknown types at compile time), the UI parts use a generic `dynamic-tool` type instead of specific tool types:

content/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ Allows you to easily create a conversational user interface for your chatbot app
419419
},
420420
{
421421
name: 'addToolResult',
422-
type: '(options: { tool: string; toolCallId: string; output: unknown }) => void',
422+
type: '(options: { tool: string; toolCallId: string; output: unknown } | { tool: string; toolCallId: string; state: "output-error", errorText: string }) => void',
423423
description:
424424
'Function to add a tool result to the chat. This will update the chat messages with the tool result. If sendAutomaticallyWhen is configured, it may trigger an automatic submission.',
425425
},

content/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,24 @@ const { messages, sendMessage, addToolResult } = useChat({
5454
// Handle tool calls in onToolCall
5555
onToolCall: async ({ toolCall }) => {
5656
if (toolCall.toolName === 'getLocation') {
57-
const result = await getLocationData();
58-
59-
// Important: Don't await inside onToolCall to avoid deadlocks
60-
addToolResult({
61-
tool: 'getLocation',
62-
toolCallId: toolCall.toolCallId,
63-
output: result,
64-
});
57+
try {
58+
const result = await getLocationData();
59+
60+
// Important: Don't await inside onToolCall to avoid deadlocks
61+
addToolResult({
62+
tool: 'getLocation',
63+
toolCallId: toolCall.toolCallId,
64+
output: result,
65+
});
66+
} catch (err) {
67+
// Important: Don't await inside onToolCall to avoid deadlocks
68+
addToolResult({
69+
tool: 'getLocation',
70+
toolCallId: toolCall.toolCallId,
71+
state: 'output-error',
72+
errorText: 'Failed to get location',
73+
});
74+
}
6575
}
6676
},
6777
});

0 commit comments

Comments
 (0)