ai 6.0.31 → 6.0.32

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 (250) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.js +1 -1
  3. package/dist/index.mjs +1 -1
  4. package/dist/internal/index.js +1 -1
  5. package/dist/internal/index.mjs +1 -1
  6. package/docs/00-introduction/index.mdx +76 -0
  7. package/docs/02-foundations/01-overview.mdx +43 -0
  8. package/docs/02-foundations/02-providers-and-models.mdx +163 -0
  9. package/docs/02-foundations/03-prompts.mdx +620 -0
  10. package/docs/02-foundations/04-tools.mdx +160 -0
  11. package/docs/02-foundations/05-streaming.mdx +62 -0
  12. package/docs/02-foundations/index.mdx +43 -0
  13. package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
  14. package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
  15. package/docs/02-getting-started/02-nextjs-app-router.mdx +556 -0
  16. package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
  17. package/docs/02-getting-started/04-svelte.mdx +627 -0
  18. package/docs/02-getting-started/05-nuxt.mdx +566 -0
  19. package/docs/02-getting-started/06-nodejs.mdx +512 -0
  20. package/docs/02-getting-started/07-expo.mdx +766 -0
  21. package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
  22. package/docs/02-getting-started/index.mdx +44 -0
  23. package/docs/03-agents/01-overview.mdx +96 -0
  24. package/docs/03-agents/02-building-agents.mdx +367 -0
  25. package/docs/03-agents/03-workflows.mdx +370 -0
  26. package/docs/03-agents/04-loop-control.mdx +350 -0
  27. package/docs/03-agents/05-configuring-call-options.mdx +286 -0
  28. package/docs/03-agents/index.mdx +40 -0
  29. package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
  30. package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
  31. package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
  32. package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
  33. package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
  34. package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
  35. package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
  36. package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
  37. package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
  38. package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
  39. package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
  40. package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
  41. package/docs/03-ai-sdk-core/40-middleware.mdx +480 -0
  42. package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
  43. package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
  44. package/docs/03-ai-sdk-core/55-testing.mdx +218 -0
  45. package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
  46. package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
  47. package/docs/03-ai-sdk-core/index.mdx +88 -0
  48. package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
  49. package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
  50. package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
  51. package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
  52. package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
  53. package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
  54. package/docs/04-ai-sdk-ui/05-completion.mdx +186 -0
  55. package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
  56. package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
  57. package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
  58. package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
  59. package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
  60. package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
  61. package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
  62. package/docs/04-ai-sdk-ui/index.mdx +64 -0
  63. package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
  64. package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
  65. package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
  66. package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
  67. package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
  68. package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +158 -0
  69. package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
  70. package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -0
  71. package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
  72. package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
  73. package/docs/05-ai-sdk-rsc/index.mdx +58 -0
  74. package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
  75. package/docs/06-advanced/02-stopping-streams.mdx +184 -0
  76. package/docs/06-advanced/03-backpressure.mdx +173 -0
  77. package/docs/06-advanced/04-caching.mdx +169 -0
  78. package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
  79. package/docs/06-advanced/06-rate-limiting.mdx +60 -0
  80. package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +213 -0
  81. package/docs/06-advanced/08-model-as-router.mdx +120 -0
  82. package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
  83. package/docs/06-advanced/09-sequential-generations.mdx +55 -0
  84. package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
  85. package/docs/06-advanced/index.mdx +11 -0
  86. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2142 -0
  87. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
  88. package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
  89. package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
  90. package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
  91. package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -0
  92. package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
  93. package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +227 -0
  94. package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
  95. package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
  96. package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
  97. package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
  98. package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
  99. package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
  100. package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
  101. package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
  102. package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
  103. package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -0
  104. package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
  105. package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
  106. package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
  107. package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +55 -0
  108. package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
  109. package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
  110. package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
  111. package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
  112. package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
  113. package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +182 -0
  114. package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -0
  115. package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
  116. package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
  117. package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
  118. package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +46 -0
  119. package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
  120. package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
  121. package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
  122. package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
  123. package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
  124. package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
  125. package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
  126. package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
  127. package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
  128. package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +43 -0
  129. package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
  130. package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
  131. package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
  132. package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
  133. package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
  134. package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -0
  135. package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
  136. package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +151 -0
  137. package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
  138. package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -0
  139. package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
  140. package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
  141. package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
  142. package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
  143. package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
  144. package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
  145. package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
  146. package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
  147. package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +48 -0
  148. package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -0
  149. package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
  150. package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
  151. package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
  152. package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
  153. package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
  154. package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
  155. package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +262 -0
  156. package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
  157. package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
  158. package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
  159. package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
  160. package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
  161. package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
  162. package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
  163. package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
  164. package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
  165. package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
  166. package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
  167. package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
  168. package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
  169. package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
  170. package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
  171. package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
  172. package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
  173. package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
  174. package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
  175. package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
  176. package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
  177. package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -0
  178. package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
  179. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
  180. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +25 -0
  181. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.mdx +26 -0
  182. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
  183. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
  184. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
  185. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +25 -0
  186. package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
  187. package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
  188. package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
  189. package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
  190. package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
  191. package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
  192. package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
  193. package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
  194. package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +25 -0
  195. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
  196. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
  197. package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
  198. package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +25 -0
  199. package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
  200. package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
  201. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +26 -0
  202. package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
  203. package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
  204. package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
  205. package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
  206. package/docs/07-reference/index.mdx +34 -0
  207. package/docs/08-migration-guides/00-versioning.mdx +46 -0
  208. package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
  209. package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
  210. package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
  211. package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
  212. package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
  213. package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
  214. package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
  215. package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
  216. package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
  217. package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
  218. package/docs/08-migration-guides/index.mdx +22 -0
  219. package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
  220. package/docs/09-troubleshooting/02-client-side-function-calls-not-invoked.mdx +22 -0
  221. package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
  222. package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
  223. package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
  224. package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
  225. package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
  226. package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
  227. package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
  228. package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
  229. package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
  230. package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
  231. package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
  232. package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
  233. package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
  234. package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
  235. package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
  236. package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
  237. package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
  238. package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
  239. package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
  240. package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
  241. package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
  242. package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
  243. package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
  244. package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +72 -0
  245. package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
  246. package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
  247. package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
  248. package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
  249. package/docs/09-troubleshooting/index.mdx +11 -0
  250. package/package.json +8 -4
@@ -0,0 +1,446 @@
1
+ ---
2
+ title: useChat
3
+ description: API reference for the useChat hook.
4
+ ---
5
+
6
+ # `useChat()`
7
+
8
+ Allows you to easily create a conversational user interface for your chatbot application. It enables the streaming of chat messages from your AI provider, manages the chat state, and updates the UI automatically as new messages are received.
9
+
10
+ <Note>
11
+ The `useChat` API has been significantly updated in AI SDK 5.0. It now uses a
12
+ transport-based architecture and no longer manages input state internally. See
13
+ the [migration
14
+ guide](/docs/migration-guides/migration-guide-5-0#usechat-changes) for
15
+ details.
16
+ </Note>
17
+
18
+ ## Import
19
+
20
+ <Tabs items={['React', 'Svelte', 'Vue']}>
21
+ <Tab>
22
+ <Snippet
23
+ text="import { useChat } from '@ai-sdk/react'"
24
+ dark
25
+ prompt={false}
26
+ />
27
+ </Tab>
28
+ <Tab>
29
+ <Snippet text="import { Chat } from '@ai-sdk/svelte'" dark prompt={false} />
30
+ </Tab>
31
+ <Tab>
32
+ <Snippet text="import { Chat } from '@ai-sdk/vue'" dark prompt={false} />
33
+ </Tab>
34
+ </Tabs>
35
+
36
+ ## API Signature
37
+
38
+ ### Parameters
39
+
40
+ <PropertiesTable
41
+ content={[
42
+ {
43
+ name: 'chat',
44
+ type: 'Chat<UIMessage>',
45
+ isOptional: true,
46
+ description:
47
+ 'An existing Chat instance to use. If provided, other parameters are ignored.',
48
+ },
49
+ {
50
+ name: 'transport',
51
+ type: 'ChatTransport',
52
+ isOptional: true,
53
+ description:
54
+ 'The transport to use for sending messages. Defaults to DefaultChatTransport with `/api/chat` endpoint.',
55
+ properties: [
56
+ {
57
+ type: 'DefaultChatTransport',
58
+ parameters: [
59
+ {
60
+ name: 'api',
61
+ type: "string = '/api/chat'",
62
+ isOptional: true,
63
+ description: 'The API endpoint for chat requests.',
64
+ },
65
+ {
66
+ name: 'credentials',
67
+ type: 'RequestCredentials',
68
+ isOptional: true,
69
+ description: 'The credentials mode for fetch requests.',
70
+ },
71
+ {
72
+ name: 'headers',
73
+ type: 'Record<string, string> | Headers',
74
+ isOptional: true,
75
+ description: 'HTTP headers to send with requests.',
76
+ },
77
+ {
78
+ name: 'body',
79
+ type: 'object',
80
+ isOptional: true,
81
+ description: 'Extra body object to send with requests.',
82
+ },
83
+ {
84
+ name: 'prepareSendMessagesRequest',
85
+ type: 'PrepareSendMessagesRequest',
86
+ isOptional: true,
87
+ description:
88
+ 'A function to customize the request before chat API calls.',
89
+ properties: [
90
+ {
91
+ type: 'PrepareSendMessagesRequest',
92
+ parameters: [
93
+ {
94
+ name: 'options',
95
+ type: 'PrepareSendMessageRequestOptions',
96
+ description: 'Options for preparing the request',
97
+ properties: [
98
+ {
99
+ type: 'PrepareSendMessageRequestOptions',
100
+ parameters: [
101
+ {
102
+ name: 'id',
103
+ type: 'string',
104
+ description: 'The chat ID',
105
+ },
106
+ {
107
+ name: 'messages',
108
+ type: 'UIMessage[]',
109
+ description: 'Current messages in the chat',
110
+ },
111
+ {
112
+ name: 'requestMetadata',
113
+ type: 'unknown',
114
+ description: 'The request metadata',
115
+ },
116
+ {
117
+ name: 'body',
118
+ type: 'Record<string, any> | undefined',
119
+ description: 'The request body',
120
+ },
121
+ {
122
+ name: 'credentials',
123
+ type: 'RequestCredentials | undefined',
124
+ description: 'The request credentials',
125
+ },
126
+ {
127
+ name: 'headers',
128
+ type: 'HeadersInit | undefined',
129
+ description: 'The request headers',
130
+ },
131
+ {
132
+ name: 'api',
133
+ type: 'string',
134
+ description: `The API endpoint to use for the request. If not specified, it defaults to the transport’s API endpoint: /api/chat.`,
135
+ },
136
+ {
137
+ name: 'trigger',
138
+ type: "'submit-message' | 'regenerate-message'",
139
+ description: 'The trigger for the request',
140
+ },
141
+ {
142
+ name: 'messageId',
143
+ type: 'string | undefined',
144
+ description: 'The message ID if applicable',
145
+ },
146
+ ],
147
+ },
148
+ ],
149
+ },
150
+ ],
151
+ },
152
+ ],
153
+ },
154
+ {
155
+ name: 'prepareReconnectToStreamRequest',
156
+ type: 'PrepareReconnectToStreamRequest',
157
+ isOptional: true,
158
+ description:
159
+ 'A function to customize the request before reconnect API call.',
160
+ properties: [
161
+ {
162
+ type: 'PrepareReconnectToStreamRequest',
163
+ parameters: [
164
+ {
165
+ name: 'options',
166
+ type: 'PrepareReconnectToStreamRequestOptions',
167
+ description:
168
+ 'Options for preparing the reconnect request',
169
+ properties: [
170
+ {
171
+ type: 'PrepareReconnectToStreamRequestOptions',
172
+ parameters: [
173
+ {
174
+ name: 'id',
175
+ type: 'string',
176
+ description: 'The chat ID',
177
+ },
178
+ {
179
+ name: 'requestMetadata',
180
+ type: 'unknown',
181
+ description: 'The request metadata',
182
+ },
183
+ {
184
+ name: 'body',
185
+ type: 'Record<string, any> | undefined',
186
+ description: 'The request body',
187
+ },
188
+ {
189
+ name: 'credentials',
190
+ type: 'RequestCredentials | undefined',
191
+ description: 'The request credentials',
192
+ },
193
+ {
194
+ name: 'headers',
195
+ type: 'HeadersInit | undefined',
196
+ description: 'The request headers',
197
+ },
198
+ {
199
+ name: 'api',
200
+ type: 'string',
201
+ description: `The API endpoint to use for the request. If not specified, it defaults to the transport’s API endpoint combined with the chat ID: /api/chat/{chatId}/stream.`,
202
+ },
203
+ ],
204
+ },
205
+ ],
206
+ },
207
+ ],
208
+ },
209
+ ],
210
+ },
211
+ ],
212
+ },
213
+ ],
214
+ },
215
+ {
216
+ name: 'id',
217
+ type: 'string',
218
+ isOptional: true,
219
+ description:
220
+ 'A unique identifier for the chat. If not provided, a random one will be generated.',
221
+ },
222
+ {
223
+ name: 'messages',
224
+ type: 'UIMessage[]',
225
+ isOptional: true,
226
+ description: 'Initial chat messages to populate the conversation with.',
227
+ },
228
+ {
229
+ name: 'onToolCall',
230
+ type: '({toolCall: ToolCall}) => void | Promise<void>',
231
+ isOptional: true,
232
+ description:
233
+ 'Optional callback function that is invoked when a tool call is received. You must call addToolOutput to provide the tool result.',
234
+ },
235
+ {
236
+ name: 'sendAutomaticallyWhen',
237
+ type: '(options: { messages: UIMessage[] }) => boolean | PromiseLike<boolean>',
238
+ isOptional: true,
239
+ description:
240
+ 'When provided, this function will be called when the stream is finished or a tool call is added to determine if the current messages should be resubmitted. You can use the lastAssistantMessageIsCompleteWithToolCalls helper for common scenarios.',
241
+ },
242
+ {
243
+ name: 'onFinish',
244
+ type: '(options: OnFinishOptions) => void',
245
+ isOptional: true,
246
+ description: 'Called when the assistant response has finished streaming.',
247
+ properties: [
248
+ {
249
+ type: 'OnFinishOptions',
250
+ parameters: [
251
+ {
252
+ name: 'message',
253
+ type: 'UIMessage',
254
+ description: 'The response message.',
255
+ },
256
+ {
257
+ name: 'messages',
258
+ type: 'UIMessage[]',
259
+ description: 'All messages including the response message',
260
+ },
261
+ {
262
+ name: 'isAbort',
263
+ type: 'boolean',
264
+ description:
265
+ 'True when the request has been aborted by the client.',
266
+ },
267
+ {
268
+ name: 'isDisconnect',
269
+ type: 'boolean',
270
+ description:
271
+ 'True if the server has been disconnected, e.g. because of a network error.',
272
+ },
273
+ {
274
+ name: 'isError',
275
+ type: 'boolean',
276
+ description: `True if errors during streaming caused the response to stop early.`,
277
+ },
278
+ {
279
+ name: 'finishReason',
280
+ type: "'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'other'",
281
+ isOptional: true,
282
+ description:
283
+ 'The reason why the model finished generating the response. Undefined if the finish reason was not provided by the model.',
284
+ },
285
+ ],
286
+ },
287
+ ],
288
+ },
289
+ {
290
+ name: 'onError',
291
+ type: '(error: Error) => void',
292
+ isOptional: true,
293
+ description:
294
+ 'Callback function to be called when an error is encountered.',
295
+ },
296
+ {
297
+ name: 'onData',
298
+ type: '(dataPart: DataUIPart) => void',
299
+ isOptional: true,
300
+ description:
301
+ 'Optional callback function that is called when a data part is received.',
302
+ },
303
+ {
304
+ name: 'experimental_throttle',
305
+ type: 'number',
306
+ isOptional: true,
307
+ description:
308
+ 'Custom throttle wait in ms for the chat messages and data updates. Default is undefined, which disables throttling.',
309
+ },
310
+ {
311
+ name: 'resume',
312
+ type: 'boolean',
313
+ isOptional: true,
314
+ description:
315
+ 'Whether to resume an ongoing chat generation stream. Defaults to false.',
316
+ },
317
+ ]}
318
+ />
319
+
320
+ ### Returns
321
+
322
+ <PropertiesTable
323
+ content={[
324
+ {
325
+ name: 'id',
326
+ type: 'string',
327
+ description: 'The id of the chat.',
328
+ },
329
+ {
330
+ name: 'messages',
331
+ type: 'UIMessage[]',
332
+ description: 'The current array of chat messages.',
333
+ properties: [
334
+ {
335
+ type: 'UIMessage',
336
+ parameters: [
337
+ {
338
+ name: 'id',
339
+ type: 'string',
340
+ description: 'A unique identifier for the message.',
341
+ },
342
+ {
343
+ name: 'role',
344
+ type: "'system' | 'user' | 'assistant'",
345
+ description: 'The role of the message.',
346
+ },
347
+ {
348
+ name: 'parts',
349
+ type: 'UIMessagePart[]',
350
+ description:
351
+ 'The parts of the message. Use this for rendering the message in the UI.',
352
+ },
353
+ {
354
+ name: 'metadata',
355
+ type: 'unknown',
356
+ isOptional: true,
357
+ description: 'The metadata of the message.',
358
+ },
359
+ ],
360
+ },
361
+ ],
362
+ },
363
+ {
364
+ name: 'status',
365
+ type: "'submitted' | 'streaming' | 'ready' | 'error'",
366
+ description:
367
+ 'The current status of the chat: "ready" (idle), "submitted" (request sent), "streaming" (receiving response), or "error" (request failed).',
368
+ },
369
+ {
370
+ name: 'error',
371
+ type: 'Error | undefined',
372
+ description: 'The error object if an error occurred.',
373
+ },
374
+ {
375
+ name: 'sendMessage',
376
+ type: '(message: CreateUIMessage | string, options?: ChatRequestOptions) => void',
377
+ description:
378
+ 'Function to send a new message to the chat. This will trigger an API call to generate the assistant response.',
379
+ properties: [
380
+ {
381
+ type: 'ChatRequestOptions',
382
+ parameters: [
383
+ {
384
+ name: 'headers',
385
+ type: 'Record<string, string> | Headers',
386
+ description:
387
+ 'Additional headers that should be to be passed to the API endpoint.',
388
+ },
389
+ {
390
+ name: 'body',
391
+ type: 'object',
392
+ description:
393
+ 'Additional body JSON properties that should be sent to the API endpoint.',
394
+ },
395
+ {
396
+ name: 'metadata',
397
+ type: 'JSONValue',
398
+ description: 'Additional data to be sent to the API endpoint.',
399
+ },
400
+ ],
401
+ },
402
+ ],
403
+ },
404
+ {
405
+ name: 'regenerate',
406
+ type: '(options?: { messageId?: string }) => void',
407
+ description:
408
+ 'Function to regenerate the last assistant message or a specific message. If no messageId is provided, regenerates the last assistant message.',
409
+ },
410
+ {
411
+ name: 'stop',
412
+ type: '() => void',
413
+ description:
414
+ 'Function to abort the current streaming response from the assistant.',
415
+ },
416
+ {
417
+ name: 'clearError',
418
+ type: '() => void',
419
+ description: 'Clears the error state.',
420
+ },
421
+ {
422
+ name: 'resumeStream',
423
+ type: '() => void',
424
+ description:
425
+ 'Function to resume an interrupted streaming response. Useful when a network error occurs during streaming.',
426
+ },
427
+ {
428
+ name: 'addToolOutput',
429
+ type: '(options: { tool: string; toolCallId: string; output: unknown } | { tool: string; toolCallId: string; state: "output-error", errorText: string }) => void',
430
+ description:
431
+ '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.',
432
+ },
433
+ {
434
+ name: 'setMessages',
435
+ type: '(messages: UIMessage[] | ((messages: UIMessage[]) => UIMessage[])) => void',
436
+ description:
437
+ 'Function to update the messages state locally without triggering an API call. Useful for optimistic updates.',
438
+ },
439
+ ]}
440
+ />
441
+
442
+ ## Learn more
443
+
444
+ - [Chatbot](/docs/ai-sdk-ui/chatbot)
445
+ - [Chatbot with Tools](/docs/ai-sdk-ui/chatbot-with-tool-calling)
446
+ - [UIMessage](/docs/reference/ai-sdk-core/ui-message)
@@ -0,0 +1,179 @@
1
+ ---
2
+ title: useCompletion
3
+ description: API reference for the useCompletion hook.
4
+ ---
5
+
6
+ # `useCompletion()`
7
+
8
+ Allows you to create text completion based capabilities for your application. It enables the streaming of text completions from your AI provider, manages the state for chat input, and updates the UI automatically as new messages are received.
9
+
10
+ ## Import
11
+
12
+ <Tabs items={['React', 'Svelte', 'Vue']}>
13
+ <Tab>
14
+ <Snippet
15
+ text="import { useCompletion } from '@ai-sdk/react'"
16
+ dark
17
+ prompt={false}
18
+ />
19
+ </Tab>
20
+ <Tab>
21
+ <Snippet
22
+ text="import { Completion } from '@ai-sdk/svelte'"
23
+ dark
24
+ prompt={false}
25
+ />
26
+ </Tab>
27
+ <Tab>
28
+ <Snippet
29
+ text="import { useCompletion } from '@ai-sdk/vue'"
30
+ dark
31
+ prompt={false}
32
+ />
33
+ </Tab>
34
+
35
+ </Tabs>
36
+
37
+ ## API Signature
38
+
39
+ ### Parameters
40
+
41
+ <PropertiesTable
42
+ content={[
43
+ {
44
+ name: 'api',
45
+ type: "string = '/api/completion'",
46
+ description:
47
+ 'The API endpoint that is called to generate text. It can be a relative path (starting with `/`) or an absolute URL.',
48
+ },
49
+ {
50
+ name: 'id',
51
+ type: 'string',
52
+ description:
53
+ 'An unique identifier for the completion. If not provided, a random one will be generated. When provided, the `useCompletion` hook with the same `id` will have shared states across components. This is useful when you have multiple components showing the same chat stream',
54
+ },
55
+ {
56
+ name: 'initialInput',
57
+ type: 'string',
58
+ description: 'An optional string for the initial prompt input.',
59
+ },
60
+ {
61
+ name: 'initialCompletion',
62
+ type: 'string',
63
+ description: 'An optional string for the initial completion result.',
64
+ },
65
+ {
66
+ name: 'onFinish',
67
+ type: '(prompt: string, completion: string) => void',
68
+ description:
69
+ 'An optional callback function that is called when the completion stream ends.',
70
+ },
71
+ {
72
+ name: 'onError',
73
+ type: '(error: Error) => void',
74
+ description:
75
+ 'An optional callback that will be called when the chat stream encounters an error.',
76
+ },
77
+ {
78
+ name: 'headers',
79
+ type: 'Record<string, string> | Headers',
80
+ description:
81
+ 'An optional object of headers to be passed to the API endpoint.',
82
+ },
83
+ {
84
+ name: 'body',
85
+ type: 'any',
86
+ description:
87
+ 'An optional, additional body object to be passed to the API endpoint.',
88
+ },
89
+ {
90
+ name: 'credentials',
91
+ type: "'omit' | 'same-origin' | 'include'",
92
+ description:
93
+ 'An optional literal that sets the mode of credentials to be used on the request. Defaults to same-origin.',
94
+ },
95
+ {
96
+ name: 'streamProtocol',
97
+ type: "'text' | 'data'",
98
+ isOptional: true,
99
+ description:
100
+ 'An optional literal that sets the type of stream to be used. Defaults to `data`. If set to `text`, the stream will be treated as a text stream.',
101
+ },
102
+ {
103
+ name: 'fetch',
104
+ type: 'FetchFunction',
105
+ isOptional: true,
106
+ description:
107
+ 'Optional. A custom fetch function to be used for the API call. Defaults to the global fetch function.',
108
+ },
109
+ {
110
+ name: 'experimental_throttle',
111
+ type: 'number',
112
+ isOptional: true,
113
+ description:
114
+ 'React only. Custom throttle wait time in milliseconds for the completion and data updates. When specified, throttles how often the UI updates during streaming. Default is undefined, which disables throttling.',
115
+ },
116
+
117
+ ]}
118
+ />
119
+
120
+ ### Returns
121
+
122
+ <PropertiesTable
123
+ content={[
124
+ {
125
+ name: 'completion',
126
+ type: 'string',
127
+ description: 'The current text completion.',
128
+ },
129
+ {
130
+ name: 'complete',
131
+ type: '(prompt: string, options: { headers, body }) => void',
132
+ description:
133
+ 'Function to execute text completion based on the provided prompt.',
134
+ },
135
+ {
136
+ name: 'error',
137
+ type: 'undefined | Error',
138
+ description: 'The error thrown during the completion process, if any.',
139
+ },
140
+ {
141
+ name: 'setCompletion',
142
+ type: '(completion: string) => void',
143
+ description: 'Function to update the `completion` state.',
144
+ },
145
+ {
146
+ name: 'stop',
147
+ type: '() => void',
148
+ description: 'Function to abort the current API request.',
149
+ },
150
+ {
151
+ name: 'input',
152
+ type: 'string',
153
+ description: 'The current value of the input field.',
154
+ },
155
+ {
156
+ name: 'setInput',
157
+ type: 'React.Dispatch<React.SetStateAction<string>>',
158
+ description: 'The current value of the input field.',
159
+ },
160
+ {
161
+ name: 'handleInputChange',
162
+ type: '(event: any) => void',
163
+ description:
164
+ "Handler for the `onChange` event of the input field to control the input's value.",
165
+ },
166
+ {
167
+ name: 'handleSubmit',
168
+ type: '(event?: { preventDefault?: () => void }) => void',
169
+ description:
170
+ 'Form submission handler that automatically resets the input field and appends a user message.',
171
+ },
172
+ {
173
+ name: 'isLoading',
174
+ type: 'boolean',
175
+ description:
176
+ 'Boolean flag indicating whether a fetch operation is currently in progress.',
177
+ },
178
+ ]}
179
+ />