ai 6.0.31 → 6.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (252) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.js +12 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +12 -2
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/internal/index.js +1 -1
  7. package/dist/internal/index.mjs +1 -1
  8. package/docs/00-introduction/index.mdx +76 -0
  9. package/docs/02-foundations/01-overview.mdx +43 -0
  10. package/docs/02-foundations/02-providers-and-models.mdx +163 -0
  11. package/docs/02-foundations/03-prompts.mdx +620 -0
  12. package/docs/02-foundations/04-tools.mdx +160 -0
  13. package/docs/02-foundations/05-streaming.mdx +62 -0
  14. package/docs/02-foundations/index.mdx +43 -0
  15. package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
  16. package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
  17. package/docs/02-getting-started/02-nextjs-app-router.mdx +556 -0
  18. package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
  19. package/docs/02-getting-started/04-svelte.mdx +627 -0
  20. package/docs/02-getting-started/05-nuxt.mdx +566 -0
  21. package/docs/02-getting-started/06-nodejs.mdx +512 -0
  22. package/docs/02-getting-started/07-expo.mdx +766 -0
  23. package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
  24. package/docs/02-getting-started/index.mdx +44 -0
  25. package/docs/03-agents/01-overview.mdx +96 -0
  26. package/docs/03-agents/02-building-agents.mdx +367 -0
  27. package/docs/03-agents/03-workflows.mdx +370 -0
  28. package/docs/03-agents/04-loop-control.mdx +350 -0
  29. package/docs/03-agents/05-configuring-call-options.mdx +286 -0
  30. package/docs/03-agents/index.mdx +40 -0
  31. package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
  32. package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
  33. package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
  34. package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
  35. package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
  36. package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
  37. package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
  38. package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
  39. package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
  40. package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
  41. package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
  42. package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
  43. package/docs/03-ai-sdk-core/40-middleware.mdx +480 -0
  44. package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
  45. package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
  46. package/docs/03-ai-sdk-core/55-testing.mdx +218 -0
  47. package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
  48. package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
  49. package/docs/03-ai-sdk-core/index.mdx +88 -0
  50. package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
  51. package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
  52. package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
  53. package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
  54. package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
  55. package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
  56. package/docs/04-ai-sdk-ui/05-completion.mdx +186 -0
  57. package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
  58. package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
  59. package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
  60. package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
  61. package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
  62. package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
  63. package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
  64. package/docs/04-ai-sdk-ui/index.mdx +64 -0
  65. package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
  66. package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
  67. package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
  68. package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
  69. package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
  70. package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +158 -0
  71. package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
  72. package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -0
  73. package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
  74. package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
  75. package/docs/05-ai-sdk-rsc/index.mdx +58 -0
  76. package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
  77. package/docs/06-advanced/02-stopping-streams.mdx +184 -0
  78. package/docs/06-advanced/03-backpressure.mdx +173 -0
  79. package/docs/06-advanced/04-caching.mdx +169 -0
  80. package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
  81. package/docs/06-advanced/06-rate-limiting.mdx +60 -0
  82. package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +213 -0
  83. package/docs/06-advanced/08-model-as-router.mdx +120 -0
  84. package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
  85. package/docs/06-advanced/09-sequential-generations.mdx +55 -0
  86. package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
  87. package/docs/06-advanced/index.mdx +11 -0
  88. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2142 -0
  89. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
  90. package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
  91. package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
  92. package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
  93. package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -0
  94. package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
  95. package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +227 -0
  96. package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
  97. package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
  98. package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
  99. package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
  100. package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
  101. package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
  102. package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
  103. package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
  104. package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
  105. package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -0
  106. package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
  107. package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
  108. package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
  109. package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +55 -0
  110. package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
  111. package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
  112. package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
  113. package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
  114. package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
  115. package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +182 -0
  116. package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -0
  117. package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
  118. package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
  119. package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
  120. package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +46 -0
  121. package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
  122. package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
  123. package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
  124. package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
  125. package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
  126. package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
  127. package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
  128. package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
  129. package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
  130. package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +43 -0
  131. package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
  132. package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
  133. package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
  134. package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
  135. package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
  136. package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -0
  137. package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
  138. package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +151 -0
  139. package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
  140. package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -0
  141. package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
  142. package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
  143. package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
  144. package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
  145. package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
  146. package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
  147. package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
  148. package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
  149. package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +48 -0
  150. package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -0
  151. package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
  152. package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
  153. package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
  154. package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
  155. package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
  156. package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
  157. package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +262 -0
  158. package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
  159. package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
  160. package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
  161. package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
  162. package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
  163. package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
  164. package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
  165. package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
  166. package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
  167. package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
  168. package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
  169. package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
  170. package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
  171. package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
  172. package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
  173. package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
  174. package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
  175. package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
  176. package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
  177. package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
  178. package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
  179. package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -0
  180. package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
  181. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
  182. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +25 -0
  183. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.mdx +26 -0
  184. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
  185. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
  186. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
  187. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +25 -0
  188. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
  189. package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
  190. package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
  191. package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
  192. package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
  193. package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
  194. package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
  195. package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
  196. package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +25 -0
  197. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
  198. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
  199. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
  200. package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +25 -0
  201. package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
  202. package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
  203. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +26 -0
  204. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
  205. package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
  206. package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
  207. package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
  208. package/docs/07-reference/index.mdx +34 -0
  209. package/docs/08-migration-guides/00-versioning.mdx +46 -0
  210. package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
  211. package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
  212. package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
  213. package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
  214. package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
  215. package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
  216. package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
  217. package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
  218. package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
  219. package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
  220. package/docs/08-migration-guides/index.mdx +22 -0
  221. package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
  222. package/docs/09-troubleshooting/02-client-side-function-calls-not-invoked.mdx +22 -0
  223. package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
  224. package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
  225. package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
  226. package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
  227. package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
  228. package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
  229. package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
  230. package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
  231. package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
  232. package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
  233. package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
  234. package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
  235. package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
  236. package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
  237. package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
  238. package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
  239. package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
  240. package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
  241. package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
  242. package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
  243. package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
  244. package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
  245. package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
  246. package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +72 -0
  247. package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
  248. package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
  249. package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
  250. package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
  251. package/docs/09-troubleshooting/index.mdx +11 -0
  252. package/package.json +8 -4
@@ -0,0 +1,682 @@
1
+ ---
2
+ title: Chatbot Tool Usage
3
+ description: Learn how to use tools with the useChat hook.
4
+ ---
5
+
6
+ # Chatbot Tool Usage
7
+
8
+ With [`useChat`](/docs/reference/ai-sdk-ui/use-chat) and [`streamText`](/docs/reference/ai-sdk-core/stream-text), you can use tools in your chatbot application.
9
+ The AI SDK supports three types of tools in this context:
10
+
11
+ 1. Automatically executed server-side tools
12
+ 2. Automatically executed client-side tools
13
+ 3. Tools that require user interaction, such as confirmation dialogs
14
+
15
+ The flow is as follows:
16
+
17
+ 1. The user enters a message in the chat UI.
18
+ 1. The message is sent to the API route.
19
+ 1. In your server side route, the language model generates tool calls during the `streamText` call.
20
+ 1. All tool calls are forwarded to the client.
21
+ 1. Server-side tools are executed using their `execute` method and their results are forwarded to the client.
22
+ 1. Client-side tools that should be automatically executed are handled with the `onToolCall` callback.
23
+ You must call `addToolOutput` to provide the tool result.
24
+ 1. Client-side tool that require user interactions can be displayed in the UI.
25
+ The tool calls and results are available as tool invocation parts in the `parts` property of the last assistant message.
26
+ 1. When the user interaction is done, `addToolOutput` can be used to add the tool result to the chat.
27
+ 1. The chat can be configured to automatically submit when all tool results are available using `sendAutomaticallyWhen`.
28
+ This triggers another iteration of this flow.
29
+
30
+ The tool calls and tool executions are integrated into the assistant message as typed tool parts.
31
+ A tool part is at first a tool call, and then it becomes a tool result when the tool is executed.
32
+ The tool result contains all information about the tool call as well as the result of the tool execution.
33
+
34
+ <Note>
35
+ Tool result submission can be configured using the `sendAutomaticallyWhen`
36
+ option. You can use the `lastAssistantMessageIsCompleteWithToolCalls` helper
37
+ to automatically submit when all tool results are available. This simplifies
38
+ the client-side code while still allowing full control when needed.
39
+ </Note>
40
+
41
+ ## Example
42
+
43
+ In this example, we'll use three tools:
44
+
45
+ - `getWeatherInformation`: An automatically executed server-side tool that returns the weather in a given city.
46
+ - `askForConfirmation`: A user-interaction client-side tool that asks the user for confirmation.
47
+ - `getLocation`: An automatically executed client-side tool that returns a random city.
48
+
49
+ ### API route
50
+
51
+ ```tsx filename='app/api/chat/route.ts'
52
+ import { convertToModelMessages, streamText, UIMessage } from 'ai';
53
+ __PROVIDER_IMPORT__;
54
+ import { z } from 'zod';
55
+
56
+ // Allow streaming responses up to 30 seconds
57
+ export const maxDuration = 30;
58
+
59
+ export async function POST(req: Request) {
60
+ const { messages }: { messages: UIMessage[] } = await req.json();
61
+
62
+ const result = streamText({
63
+ model: __MODEL__,
64
+ messages: await convertToModelMessages(messages),
65
+ tools: {
66
+ // server-side tool with execute function:
67
+ getWeatherInformation: {
68
+ description: 'show the weather in a given city to the user',
69
+ inputSchema: z.object({ city: z.string() }),
70
+ execute: async ({}: { city: string }) => {
71
+ const weatherOptions = ['sunny', 'cloudy', 'rainy', 'snowy', 'windy'];
72
+ return weatherOptions[
73
+ Math.floor(Math.random() * weatherOptions.length)
74
+ ];
75
+ },
76
+ },
77
+ // client-side tool that starts user interaction:
78
+ askForConfirmation: {
79
+ description: 'Ask the user for confirmation.',
80
+ inputSchema: z.object({
81
+ message: z.string().describe('The message to ask for confirmation.'),
82
+ }),
83
+ },
84
+ // client-side tool that is automatically executed on the client:
85
+ getLocation: {
86
+ description:
87
+ 'Get the user location. Always ask for confirmation before using this tool.',
88
+ inputSchema: z.object({}),
89
+ },
90
+ },
91
+ });
92
+
93
+ return result.toUIMessageStreamResponse();
94
+ }
95
+ ```
96
+
97
+ ### Client-side page
98
+
99
+ The client-side page uses the `useChat` hook to create a chatbot application with real-time message streaming.
100
+ Tool calls are displayed in the chat UI as typed tool parts.
101
+ Please make sure to render the messages using the `parts` property of the message.
102
+
103
+ There are three things worth mentioning:
104
+
105
+ 1. The [`onToolCall`](/docs/reference/ai-sdk-ui/use-chat#on-tool-call) callback is used to handle client-side tools that should be automatically executed.
106
+ In this example, the `getLocation` tool is a client-side tool that returns a random city.
107
+ You call `addToolOutput` to provide the result (without `await` to avoid potential deadlocks).
108
+
109
+ <Note>
110
+ Always check `if (toolCall.dynamic)` first in your `onToolCall` handler.
111
+ Without this check, TypeScript will throw an error like: `Type 'string' is
112
+ not assignable to type '"toolName1" | "toolName2"'` when you try to use
113
+ `toolCall.toolName` in `addToolOutput`.
114
+ </Note>
115
+
116
+ 2. The [`sendAutomaticallyWhen`](/docs/reference/ai-sdk-ui/use-chat#send-automatically-when) option with `lastAssistantMessageIsCompleteWithToolCalls` helper automatically submits when all tool results are available.
117
+
118
+ 3. The `parts` array of assistant messages contains tool parts with typed names like `tool-askForConfirmation`.
119
+ The client-side tool `askForConfirmation` is displayed in the UI.
120
+ It asks the user for confirmation and displays the result once the user confirms or denies the execution.
121
+ The result is added to the chat using `addToolOutput` with the `tool` parameter for type safety.
122
+
123
+ ```tsx filename='app/page.tsx' highlight="2,6,10,14-20"
124
+ 'use client';
125
+
126
+ import { useChat } from '@ai-sdk/react';
127
+ import {
128
+ DefaultChatTransport,
129
+ lastAssistantMessageIsCompleteWithToolCalls,
130
+ } from 'ai';
131
+ import { useState } from 'react';
132
+
133
+ export default function Chat() {
134
+ const { messages, sendMessage, addToolOutput } = useChat({
135
+ transport: new DefaultChatTransport({
136
+ api: '/api/chat',
137
+ }),
138
+
139
+ sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
140
+
141
+ // run client-side tools that are automatically executed:
142
+ async onToolCall({ toolCall }) {
143
+ // Check if it's a dynamic tool first for proper type narrowing
144
+ if (toolCall.dynamic) {
145
+ return;
146
+ }
147
+
148
+ if (toolCall.toolName === 'getLocation') {
149
+ const cities = ['New York', 'Los Angeles', 'Chicago', 'San Francisco'];
150
+
151
+ // No await - avoids potential deadlocks
152
+ addToolOutput({
153
+ tool: 'getLocation',
154
+ toolCallId: toolCall.toolCallId,
155
+ output: cities[Math.floor(Math.random() * cities.length)],
156
+ });
157
+ }
158
+ },
159
+ });
160
+ const [input, setInput] = useState('');
161
+
162
+ return (
163
+ <>
164
+ {messages?.map(message => (
165
+ <div key={message.id}>
166
+ <strong>{`${message.role}: `}</strong>
167
+ {message.parts.map(part => {
168
+ switch (part.type) {
169
+ // render text parts as simple text:
170
+ case 'text':
171
+ return part.text;
172
+
173
+ // for tool parts, use the typed tool part names:
174
+ case 'tool-askForConfirmation': {
175
+ const callId = part.toolCallId;
176
+
177
+ switch (part.state) {
178
+ case 'input-streaming':
179
+ return (
180
+ <div key={callId}>Loading confirmation request...</div>
181
+ );
182
+ case 'input-available':
183
+ return (
184
+ <div key={callId}>
185
+ {part.input.message}
186
+ <div>
187
+ <button
188
+ onClick={() =>
189
+ addToolOutput({
190
+ tool: 'askForConfirmation',
191
+ toolCallId: callId,
192
+ output: 'Yes, confirmed.',
193
+ })
194
+ }
195
+ >
196
+ Yes
197
+ </button>
198
+ <button
199
+ onClick={() =>
200
+ addToolOutput({
201
+ tool: 'askForConfirmation',
202
+ toolCallId: callId,
203
+ output: 'No, denied',
204
+ })
205
+ }
206
+ >
207
+ No
208
+ </button>
209
+ </div>
210
+ </div>
211
+ );
212
+ case 'output-available':
213
+ return (
214
+ <div key={callId}>
215
+ Location access allowed: {part.output}
216
+ </div>
217
+ );
218
+ case 'output-error':
219
+ return <div key={callId}>Error: {part.errorText}</div>;
220
+ }
221
+ break;
222
+ }
223
+
224
+ case 'tool-getLocation': {
225
+ const callId = part.toolCallId;
226
+
227
+ switch (part.state) {
228
+ case 'input-streaming':
229
+ return (
230
+ <div key={callId}>Preparing location request...</div>
231
+ );
232
+ case 'input-available':
233
+ return <div key={callId}>Getting location...</div>;
234
+ case 'output-available':
235
+ return <div key={callId}>Location: {part.output}</div>;
236
+ case 'output-error':
237
+ return (
238
+ <div key={callId}>
239
+ Error getting location: {part.errorText}
240
+ </div>
241
+ );
242
+ }
243
+ break;
244
+ }
245
+
246
+ case 'tool-getWeatherInformation': {
247
+ const callId = part.toolCallId;
248
+
249
+ switch (part.state) {
250
+ // example of pre-rendering streaming tool inputs:
251
+ case 'input-streaming':
252
+ return (
253
+ <pre key={callId}>{JSON.stringify(part, null, 2)}</pre>
254
+ );
255
+ case 'input-available':
256
+ return (
257
+ <div key={callId}>
258
+ Getting weather information for {part.input.city}...
259
+ </div>
260
+ );
261
+ case 'output-available':
262
+ return (
263
+ <div key={callId}>
264
+ Weather in {part.input.city}: {part.output}
265
+ </div>
266
+ );
267
+ case 'output-error':
268
+ return (
269
+ <div key={callId}>
270
+ Error getting weather for {part.input.city}:{' '}
271
+ {part.errorText}
272
+ </div>
273
+ );
274
+ }
275
+ break;
276
+ }
277
+ }
278
+ })}
279
+ <br />
280
+ </div>
281
+ ))}
282
+
283
+ <form
284
+ onSubmit={e => {
285
+ e.preventDefault();
286
+ if (input.trim()) {
287
+ sendMessage({ text: input });
288
+ setInput('');
289
+ }
290
+ }}
291
+ >
292
+ <input value={input} onChange={e => setInput(e.target.value)} />
293
+ </form>
294
+ </>
295
+ );
296
+ }
297
+ ```
298
+
299
+ ### Error handling
300
+
301
+ Sometimes an error may occur during client-side tool execution. Use the `addToolOutput` 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, addToolOutput } = 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
+ addToolOutput({
334
+ tool: 'getWeatherInformation',
335
+ toolCallId: toolCall.toolCallId,
336
+ output: weather,
337
+ });
338
+ } catch (err) {
339
+ addToolOutput({
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
+
352
+ ## Tool Execution Approval
353
+
354
+ Tool execution approval lets you require user confirmation before a server-side tool runs. Unlike [client-side tools](#example) that execute in the browser, tools with approval still execute on the server—but only after the user approves.
355
+
356
+ Use tool execution approval when you want to:
357
+
358
+ - Confirm sensitive operations (payments, deletions, external API calls)
359
+ - Let users review tool inputs before execution
360
+ - Add human oversight to automated workflows
361
+
362
+ For tools that need to run in the browser (updating UI state, accessing browser APIs), use client-side tools instead.
363
+
364
+ ### Server Setup
365
+
366
+ Enable approval by setting `needsApproval` on your tool. See [Tool Execution Approval](/docs/ai-sdk-core/tools-and-tool-calling#tool-execution-approval) for configuration options including dynamic approval based on input.
367
+
368
+ ```tsx filename='app/api/chat/route.ts'
369
+ import { streamText, tool } from 'ai';
370
+ __PROVIDER_IMPORT__;
371
+ import { z } from 'zod';
372
+
373
+ export async function POST(req: Request) {
374
+ const { messages } = await req.json();
375
+
376
+ const result = streamText({
377
+ model: __MODEL__,
378
+ messages,
379
+ tools: {
380
+ getWeather: tool({
381
+ description: 'Get the weather in a location',
382
+ inputSchema: z.object({
383
+ city: z.string(),
384
+ }),
385
+ needsApproval: true,
386
+ execute: async ({ city }) => {
387
+ const weather = await fetchWeather(city);
388
+ return weather;
389
+ },
390
+ }),
391
+ },
392
+ });
393
+
394
+ return result.toUIMessageStreamResponse();
395
+ }
396
+ ```
397
+
398
+ ### Client-Side Approval UI
399
+
400
+ When a tool requires approval, the tool part state is `approval-requested`. Use `addToolApprovalResponse` to approve or deny:
401
+
402
+ ```tsx filename='app/page.tsx'
403
+ 'use client';
404
+
405
+ import { useChat } from '@ai-sdk/react';
406
+
407
+ export default function Chat() {
408
+ const { messages, addToolApprovalResponse } = useChat();
409
+
410
+ return (
411
+ <>
412
+ {messages.map(message => (
413
+ <div key={message.id}>
414
+ {message.parts.map(part => {
415
+ if (part.type === 'tool-getWeather') {
416
+ switch (part.state) {
417
+ case 'approval-requested':
418
+ return (
419
+ <div key={part.toolCallId}>
420
+ <p>Get weather for {part.input.city}?</p>
421
+ <button
422
+ onClick={() =>
423
+ addToolApprovalResponse({
424
+ id: part.approval.id,
425
+ approved: true,
426
+ })
427
+ }
428
+ >
429
+ Approve
430
+ </button>
431
+ <button
432
+ onClick={() =>
433
+ addToolApprovalResponse({
434
+ id: part.approval.id,
435
+ approved: false,
436
+ })
437
+ }
438
+ >
439
+ Deny
440
+ </button>
441
+ </div>
442
+ );
443
+ case 'output-available':
444
+ return (
445
+ <div key={part.toolCallId}>
446
+ Weather in {part.input.city}: {part.output}
447
+ </div>
448
+ );
449
+ }
450
+ }
451
+ // Handle other part types...
452
+ })}
453
+ </div>
454
+ ))}
455
+ </>
456
+ );
457
+ }
458
+ ```
459
+
460
+ ### Auto-Submit After Approval
461
+
462
+ <Note>
463
+ If nothing happens after you approve a tool execution, make sure you either
464
+ call `sendMessage` manually or configure `sendAutomaticallyWhen` on the
465
+ `useChat` hook.
466
+ </Note>
467
+
468
+ Use `lastAssistantMessageIsCompleteWithApprovalResponses` to automatically continue the conversation after approvals:
469
+
470
+ ```tsx
471
+ import { useChat } from '@ai-sdk/react';
472
+ import { lastAssistantMessageIsCompleteWithApprovalResponses } from 'ai';
473
+
474
+ const { messages, addToolApprovalResponse } = useChat({
475
+ sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
476
+ });
477
+ ```
478
+
479
+ ## Dynamic Tools
480
+
481
+ 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:
482
+
483
+ ```tsx filename='app/page.tsx'
484
+ {
485
+ message.parts.map((part, index) => {
486
+ switch (part.type) {
487
+ // Static tools with specific (`tool-${toolName}`) types
488
+ case 'tool-getWeatherInformation':
489
+ return <WeatherDisplay part={part} />;
490
+
491
+ // Dynamic tools use generic `dynamic-tool` type
492
+ case 'dynamic-tool':
493
+ return (
494
+ <div key={index}>
495
+ <h4>Tool: {part.toolName}</h4>
496
+ {part.state === 'input-streaming' && (
497
+ <pre>{JSON.stringify(part.input, null, 2)}</pre>
498
+ )}
499
+ {part.state === 'output-available' && (
500
+ <pre>{JSON.stringify(part.output, null, 2)}</pre>
501
+ )}
502
+ {part.state === 'output-error' && (
503
+ <div>Error: {part.errorText}</div>
504
+ )}
505
+ </div>
506
+ );
507
+ }
508
+ });
509
+ }
510
+ ```
511
+
512
+ Dynamic tools are useful when integrating with:
513
+
514
+ - MCP (Model Context Protocol) tools without schemas
515
+ - User-defined functions loaded at runtime
516
+ - External tool providers
517
+
518
+ ## Tool call streaming
519
+
520
+ Tool call streaming is **enabled by default** in AI SDK 5.0, allowing you to stream tool calls while they are being generated. This provides a better user experience by showing tool inputs as they are generated in real-time.
521
+
522
+ ```tsx filename='app/api/chat/route.ts'
523
+ export async function POST(req: Request) {
524
+ const { messages }: { messages: UIMessage[] } = await req.json();
525
+
526
+ const result = streamText({
527
+ model: __MODEL__,
528
+ messages: await convertToModelMessages(messages),
529
+ // toolCallStreaming is enabled by default in v5
530
+ // ...
531
+ });
532
+
533
+ return result.toUIMessageStreamResponse();
534
+ }
535
+ ```
536
+
537
+ With tool call streaming enabled, partial tool calls are streamed as part of the data stream.
538
+ They are available through the `useChat` hook.
539
+ The typed tool parts of assistant messages will also contain partial tool calls.
540
+ You can use the `state` property of the tool part to render the correct UI.
541
+
542
+ ```tsx filename='app/page.tsx' highlight="9,10"
543
+ export default function Chat() {
544
+ // ...
545
+ return (
546
+ <>
547
+ {messages?.map(message => (
548
+ <div key={message.id}>
549
+ {message.parts.map(part => {
550
+ switch (part.type) {
551
+ case 'tool-askForConfirmation':
552
+ case 'tool-getLocation':
553
+ case 'tool-getWeatherInformation':
554
+ switch (part.state) {
555
+ case 'input-streaming':
556
+ return <pre>{JSON.stringify(part.input, null, 2)}</pre>;
557
+ case 'input-available':
558
+ return <pre>{JSON.stringify(part.input, null, 2)}</pre>;
559
+ case 'output-available':
560
+ return <pre>{JSON.stringify(part.output, null, 2)}</pre>;
561
+ case 'output-error':
562
+ return <div>Error: {part.errorText}</div>;
563
+ }
564
+ }
565
+ })}
566
+ </div>
567
+ ))}
568
+ </>
569
+ );
570
+ }
571
+ ```
572
+
573
+ ## Step start parts
574
+
575
+ When you are using multi-step tool calls, the AI SDK will add step start parts to the assistant messages.
576
+ If you want to display boundaries between tool calls, you can use the `step-start` parts as follows:
577
+
578
+ ```tsx filename='app/page.tsx'
579
+ // ...
580
+ // where you render the message parts:
581
+ message.parts.map((part, index) => {
582
+ switch (part.type) {
583
+ case 'step-start':
584
+ // show step boundaries as horizontal lines:
585
+ return index > 0 ? (
586
+ <div key={index} className="text-gray-500">
587
+ <hr className="my-2 border-gray-300" />
588
+ </div>
589
+ ) : null;
590
+ case 'text':
591
+ // ...
592
+ case 'tool-askForConfirmation':
593
+ case 'tool-getLocation':
594
+ case 'tool-getWeatherInformation':
595
+ // ...
596
+ }
597
+ });
598
+ // ...
599
+ ```
600
+
601
+ ## Server-side Multi-Step Calls
602
+
603
+ You can also use multi-step calls on the server-side with `streamText`.
604
+ This works when all invoked tools have an `execute` function on the server side.
605
+
606
+ ```tsx filename='app/api/chat/route.ts' highlight="15-21,24"
607
+ import { convertToModelMessages, streamText, UIMessage, stepCountIs } from 'ai';
608
+ __PROVIDER_IMPORT__;
609
+ import { z } from 'zod';
610
+
611
+ export async function POST(req: Request) {
612
+ const { messages }: { messages: UIMessage[] } = await req.json();
613
+
614
+ const result = streamText({
615
+ model: __MODEL__,
616
+ messages: await convertToModelMessages(messages),
617
+ tools: {
618
+ getWeatherInformation: {
619
+ description: 'show the weather in a given city to the user',
620
+ inputSchema: z.object({ city: z.string() }),
621
+ // tool has execute function:
622
+ execute: async ({}: { city: string }) => {
623
+ const weatherOptions = ['sunny', 'cloudy', 'rainy', 'snowy', 'windy'];
624
+ return weatherOptions[
625
+ Math.floor(Math.random() * weatherOptions.length)
626
+ ];
627
+ },
628
+ },
629
+ },
630
+ stopWhen: stepCountIs(5),
631
+ });
632
+
633
+ return result.toUIMessageStreamResponse();
634
+ }
635
+ ```
636
+
637
+ ## Errors
638
+
639
+ Language models can make errors when calling tools.
640
+ By default, these errors are masked for security reasons, and show up as "An error occurred" in the UI.
641
+
642
+ To surface the errors, you can use the `onError` function when calling `toUIMessageResponse`.
643
+
644
+ ```tsx
645
+ export function errorHandler(error: unknown) {
646
+ if (error == null) {
647
+ return 'unknown error';
648
+ }
649
+
650
+ if (typeof error === 'string') {
651
+ return error;
652
+ }
653
+
654
+ if (error instanceof Error) {
655
+ return error.message;
656
+ }
657
+
658
+ return JSON.stringify(error);
659
+ }
660
+ ```
661
+
662
+ ```tsx
663
+ const result = streamText({
664
+ // ...
665
+ });
666
+
667
+ return result.toUIMessageStreamResponse({
668
+ onError: errorHandler,
669
+ });
670
+ ```
671
+
672
+ In case you are using `createUIMessageResponse`, you can use the `onError` function when calling `toUIMessageResponse`:
673
+
674
+ ```tsx
675
+ const response = createUIMessageResponse({
676
+ // ...
677
+ async execute(dataStream) {
678
+ // ...
679
+ },
680
+ onError: error => `Custom error: ${error.message}`,
681
+ });
682
+ ```