ai 6.0.30 → 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 +13 -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 +7 -3
@@ -0,0 +1,600 @@
1
+ ---
2
+ title: Generating Text
3
+ description: Learn how to generate text with the AI SDK.
4
+ ---
5
+
6
+ # Generating and Streaming Text
7
+
8
+ Large language models (LLMs) can generate text in response to a prompt, which can contain instructions and information to process.
9
+ For example, you can ask a model to come up with a recipe, draft an email, or summarize a document.
10
+
11
+ The AI SDK Core provides two functions to generate text and stream it from LLMs:
12
+
13
+ - [`generateText`](#generatetext): Generates text for a given prompt and model.
14
+ - [`streamText`](#streamtext): Streams text from a given prompt and model.
15
+
16
+ Advanced LLM features such as [tool calling](./tools-and-tool-calling) and [structured data generation](./generating-structured-data) are built on top of text generation.
17
+
18
+ ## `generateText`
19
+
20
+ You can generate text using the [`generateText`](/docs/reference/ai-sdk-core/generate-text) function. This function is ideal for non-interactive use cases where you need to write text (e.g. drafting email or summarizing web pages) and for agents that use tools.
21
+
22
+ ```tsx
23
+ import { generateText } from 'ai';
24
+ __PROVIDER_IMPORT__;
25
+
26
+ const { text } = await generateText({
27
+ model: __MODEL__,
28
+ prompt: 'Write a vegetarian lasagna recipe for 4 people.',
29
+ });
30
+ ```
31
+
32
+ You can use more [advanced prompts](./prompts) to generate text with more complex instructions and content:
33
+
34
+ ```tsx
35
+ import { generateText } from 'ai';
36
+ __PROVIDER_IMPORT__;
37
+
38
+ const { text } = await generateText({
39
+ model: __MODEL__,
40
+ system:
41
+ 'You are a professional writer. ' +
42
+ 'You write simple, clear, and concise content.',
43
+ prompt: `Summarize the following article in 3-5 sentences: ${article}`,
44
+ });
45
+ ```
46
+
47
+ The result object of `generateText` contains several promises that resolve when all required data is available:
48
+
49
+ - `result.content`: The content that was generated in the last step.
50
+ - `result.text`: The generated text.
51
+ - `result.reasoning`: The full reasoning that the model has generated in the last step.
52
+ - `result.reasoningText`: The reasoning text of the model (only available for some models).
53
+ - `result.files`: The files that were generated in the last step.
54
+ - `result.sources`: Sources that have been used as references in the last step (only available for some models).
55
+ - `result.toolCalls`: The tool calls that were made in the last step.
56
+ - `result.toolResults`: The results of the tool calls from the last step.
57
+ - `result.finishReason`: The reason the model finished generating text.
58
+ - `result.rawFinishReason`: The raw reason why the generation finished (from the provider).
59
+ - `result.usage`: The usage of the model during the final step of text generation.
60
+ - `result.totalUsage`: The total usage across all steps (for multi-step generations).
61
+ - `result.warnings`: Warnings from the model provider (e.g. unsupported settings).
62
+ - `result.request`: Additional request information.
63
+ - `result.response`: Additional response information, including response messages and body.
64
+ - `result.providerMetadata`: Additional provider-specific metadata.
65
+ - `result.steps`: Details for all steps, useful for getting information about intermediate steps.
66
+ - `result.output`: The generated structured output using the `output` specification.
67
+
68
+ ### Accessing response headers & body
69
+
70
+ Sometimes you need access to the full response from the model provider,
71
+ e.g. to access some provider-specific headers or body content.
72
+
73
+ You can access the raw response headers and body using the `response` property:
74
+
75
+ ```ts
76
+ import { generateText } from 'ai';
77
+
78
+ const result = await generateText({
79
+ // ...
80
+ });
81
+
82
+ console.log(JSON.stringify(result.response.headers, null, 2));
83
+ console.log(JSON.stringify(result.response.body, null, 2));
84
+ ```
85
+
86
+ ### `onFinish` callback
87
+
88
+ When using `generateText`, you can provide an `onFinish` callback that is triggered after the last step is finished (
89
+ [API Reference](/docs/reference/ai-sdk-core/generate-text#on-finish)
90
+ ).
91
+ It contains the text, usage information, finish reason, messages, steps, total usage, and more:
92
+
93
+ ```tsx highlight="6-8"
94
+ import { generateText } from 'ai';
95
+ __PROVIDER_IMPORT__;
96
+
97
+ const result = await generateText({
98
+ model: __MODEL__,
99
+ prompt: 'Invent a new holiday and describe its traditions.',
100
+ onFinish({ text, finishReason, usage, response, steps, totalUsage }) {
101
+ // your own logic, e.g. for saving the chat history or recording usage
102
+
103
+ const messages = response.messages; // messages that were generated
104
+ },
105
+ });
106
+ ```
107
+
108
+ ## `streamText`
109
+
110
+ Depending on your model and prompt, it can take a large language model (LLM) up to a minute to finish generating its response. This delay can be unacceptable for interactive use cases such as chatbots or real-time applications, where users expect immediate responses.
111
+
112
+ AI SDK Core provides the [`streamText`](/docs/reference/ai-sdk-core/stream-text) function which simplifies streaming text from LLMs:
113
+
114
+ ```ts
115
+ import { streamText } from 'ai';
116
+ __PROVIDER_IMPORT__;
117
+
118
+ const result = streamText({
119
+ model: __MODEL__,
120
+ prompt: 'Invent a new holiday and describe its traditions.',
121
+ });
122
+
123
+ // example: use textStream as an async iterable
124
+ for await (const textPart of result.textStream) {
125
+ console.log(textPart);
126
+ }
127
+ ```
128
+
129
+ <Note>
130
+ `result.textStream` is both a `ReadableStream` and an `AsyncIterable`.
131
+ </Note>
132
+
133
+ <Note type="warning">
134
+ `streamText` immediately starts streaming and suppresses errors to prevent
135
+ server crashes. Use the `onError` callback to log errors.
136
+ </Note>
137
+
138
+ You can use `streamText` on its own or in combination with [AI SDK
139
+ UI](/examples/next-pages/basics/streaming-text-generation) and [AI SDK
140
+ RSC](/examples/next-app/basics/streaming-text-generation).
141
+ The result object contains several helper functions to make the integration into [AI SDK UI](/docs/ai-sdk-ui) easier:
142
+
143
+ - `result.toUIMessageStreamResponse()`: Creates a UI Message stream HTTP response (with tool calls etc.) that can be used in a Next.js App Router API route.
144
+ - `result.pipeUIMessageStreamToResponse()`: Writes UI Message stream delta output to a Node.js response-like object.
145
+ - `result.toTextStreamResponse()`: Creates a simple text stream HTTP response.
146
+ - `result.pipeTextStreamToResponse()`: Writes text delta output to a Node.js response-like object.
147
+
148
+ <Note>
149
+ `streamText` is using backpressure and only generates tokens as they are
150
+ requested. You need to consume the stream in order for it to finish.
151
+ </Note>
152
+
153
+ It also provides several promises that resolve when the stream is finished:
154
+
155
+ - `result.content`: The content that was generated in the last step.
156
+ - `result.text`: The generated text.
157
+ - `result.reasoning`: The full reasoning that the model has generated.
158
+ - `result.reasoningText`: The reasoning text of the model (only available for some models).
159
+ - `result.files`: Files that have been generated by the model in the last step.
160
+ - `result.sources`: Sources that have been used as references in the last step (only available for some models).
161
+ - `result.toolCalls`: The tool calls that have been executed in the last step.
162
+ - `result.toolResults`: The tool results that have been generated in the last step.
163
+ - `result.finishReason`: The reason the model finished generating text.
164
+ - `result.rawFinishReason`: The raw reason why the generation finished (from the provider).
165
+ - `result.usage`: The usage of the model during the final step of text generation.
166
+ - `result.totalUsage`: The total usage across all steps (for multi-step generations).
167
+ - `result.warnings`: Warnings from the model provider (e.g. unsupported settings).
168
+ - `result.steps`: Details for all steps, useful for getting information about intermediate steps.
169
+ - `result.request`: Additional request information from the last step.
170
+ - `result.response`: Additional response information from the last step.
171
+ - `result.providerMetadata`: Additional provider-specific metadata from the last step.
172
+
173
+ ### `onError` callback
174
+
175
+ `streamText` immediately starts streaming to enable sending data without waiting for the model.
176
+ Errors become part of the stream and are not thrown to prevent e.g. servers from crashing.
177
+
178
+ To log errors, you can provide an `onError` callback that is triggered when an error occurs.
179
+
180
+ ```tsx highlight="6-8"
181
+ import { streamText } from 'ai';
182
+ __PROVIDER_IMPORT__;
183
+
184
+ const result = streamText({
185
+ model: __MODEL__,
186
+ prompt: 'Invent a new holiday and describe its traditions.',
187
+ onError({ error }) {
188
+ console.error(error); // your error logging logic here
189
+ },
190
+ });
191
+ ```
192
+
193
+ ### `onChunk` callback
194
+
195
+ When using `streamText`, you can provide an `onChunk` callback that is triggered for each chunk of the stream.
196
+
197
+ It receives the following chunk types:
198
+
199
+ - `text`
200
+ - `reasoning`
201
+ - `source`
202
+ - `tool-call`
203
+ - `tool-input-start`
204
+ - `tool-input-delta`
205
+ - `tool-result`
206
+ - `raw`
207
+
208
+ ```tsx highlight="6-11"
209
+ import { streamText } from 'ai';
210
+ __PROVIDER_IMPORT__;
211
+
212
+ const result = streamText({
213
+ model: __MODEL__,
214
+ prompt: 'Invent a new holiday and describe its traditions.',
215
+ onChunk({ chunk }) {
216
+ // implement your own logic here, e.g.:
217
+ if (chunk.type === 'text') {
218
+ console.log(chunk.text);
219
+ }
220
+ },
221
+ });
222
+ ```
223
+
224
+ ### `onFinish` callback
225
+
226
+ When using `streamText`, you can provide an `onFinish` callback that is triggered when the stream is finished (
227
+ [API Reference](/docs/reference/ai-sdk-core/stream-text#on-finish)
228
+ ).
229
+ It contains the text, usage information, finish reason, messages, steps, total usage, and more:
230
+
231
+ ```tsx highlight="6-8"
232
+ import { streamText } from 'ai';
233
+ __PROVIDER_IMPORT__;
234
+
235
+ const result = streamText({
236
+ model: __MODEL__,
237
+ prompt: 'Invent a new holiday and describe its traditions.',
238
+ onFinish({ text, finishReason, usage, response, steps, totalUsage }) {
239
+ // your own logic, e.g. for saving the chat history or recording usage
240
+
241
+ const messages = response.messages; // messages that were generated
242
+ },
243
+ });
244
+ ```
245
+
246
+ ### `fullStream` property
247
+
248
+ You can read a stream with all events using the `fullStream` property.
249
+ This can be useful if you want to implement your own UI or handle the stream in a different way.
250
+ Here is an example of how to use the `fullStream` property:
251
+
252
+ ```tsx
253
+ import { streamText } from 'ai';
254
+ __PROVIDER_IMPORT__;
255
+ import { z } from 'zod';
256
+
257
+ const result = streamText({
258
+ model: __MODEL__,
259
+ tools: {
260
+ cityAttractions: {
261
+ inputSchema: z.object({ city: z.string() }),
262
+ execute: async ({ city }) => ({
263
+ attractions: ['attraction1', 'attraction2', 'attraction3'],
264
+ }),
265
+ },
266
+ },
267
+ prompt: 'What are some San Francisco tourist attractions?',
268
+ });
269
+
270
+ for await (const part of result.fullStream) {
271
+ switch (part.type) {
272
+ case 'start': {
273
+ // handle start of stream
274
+ break;
275
+ }
276
+ case 'start-step': {
277
+ // handle start of step
278
+ break;
279
+ }
280
+ case 'text-start': {
281
+ // handle text start
282
+ break;
283
+ }
284
+ case 'text-delta': {
285
+ // handle text delta here
286
+ break;
287
+ }
288
+ case 'text-end': {
289
+ // handle text end
290
+ break;
291
+ }
292
+ case 'reasoning-start': {
293
+ // handle reasoning start
294
+ break;
295
+ }
296
+ case 'reasoning-delta': {
297
+ // handle reasoning delta here
298
+ break;
299
+ }
300
+ case 'reasoning-end': {
301
+ // handle reasoning end
302
+ break;
303
+ }
304
+ case 'source': {
305
+ // handle source here
306
+ break;
307
+ }
308
+ case 'file': {
309
+ // handle file here
310
+ break;
311
+ }
312
+ case 'tool-call': {
313
+ switch (part.toolName) {
314
+ case 'cityAttractions': {
315
+ // handle tool call here
316
+ break;
317
+ }
318
+ }
319
+ break;
320
+ }
321
+ case 'tool-input-start': {
322
+ // handle tool input start
323
+ break;
324
+ }
325
+ case 'tool-input-delta': {
326
+ // handle tool input delta
327
+ break;
328
+ }
329
+ case 'tool-input-end': {
330
+ // handle tool input end
331
+ break;
332
+ }
333
+ case 'tool-result': {
334
+ switch (part.toolName) {
335
+ case 'cityAttractions': {
336
+ // handle tool result here
337
+ break;
338
+ }
339
+ }
340
+ break;
341
+ }
342
+ case 'tool-error': {
343
+ // handle tool error
344
+ break;
345
+ }
346
+ case 'finish-step': {
347
+ // handle finish step
348
+ break;
349
+ }
350
+ case 'finish': {
351
+ // handle finish here
352
+ break;
353
+ }
354
+ case 'error': {
355
+ // handle error here
356
+ break;
357
+ }
358
+ case 'raw': {
359
+ // handle raw value
360
+ break;
361
+ }
362
+ }
363
+ }
364
+ ```
365
+
366
+ ### Stream transformation
367
+
368
+ You can use the `experimental_transform` option to transform the stream.
369
+ This is useful for e.g. filtering, changing, or smoothing the text stream.
370
+
371
+ The transformations are applied before the callbacks are invoked and the promises are resolved.
372
+ If you e.g. have a transformation that changes all text to uppercase, the `onFinish` callback will receive the transformed text.
373
+
374
+ #### Smoothing streams
375
+
376
+ The AI SDK Core provides a [`smoothStream` function](/docs/reference/ai-sdk-core/smooth-stream) that
377
+ can be used to smooth out text and reasoning streaming.
378
+
379
+ ```tsx highlight="6"
380
+ import { smoothStream, streamText } from 'ai';
381
+
382
+ const result = streamText({
383
+ model,
384
+ prompt,
385
+ experimental_transform: smoothStream(),
386
+ });
387
+ ```
388
+
389
+ #### Custom transformations
390
+
391
+ You can also implement your own custom transformations.
392
+ The transformation function receives the tools that are available to the model,
393
+ and returns a function that is used to transform the stream.
394
+ Tools can either be generic or limited to the tools that you are using.
395
+
396
+ Here is an example of how to implement a custom transformation that converts
397
+ all text to uppercase:
398
+
399
+ ```ts
400
+ const upperCaseTransform =
401
+ <TOOLS extends ToolSet>() =>
402
+ (options: { tools: TOOLS; stopStream: () => void }) =>
403
+ new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({
404
+ transform(chunk, controller) {
405
+ controller.enqueue(
406
+ // for text chunks, convert the text to uppercase:
407
+ chunk.type === 'text'
408
+ ? { ...chunk, text: chunk.text.toUpperCase() }
409
+ : chunk,
410
+ );
411
+ },
412
+ });
413
+ ```
414
+
415
+ You can also stop the stream using the `stopStream` function.
416
+ This is e.g. useful if you want to stop the stream when model guardrails are violated, e.g. by generating inappropriate content.
417
+
418
+ When you invoke `stopStream`, it is important to simulate the `step-finish` and `finish` events to guarantee that a well-formed stream is returned
419
+ and all callbacks are invoked.
420
+
421
+ ```ts
422
+ const stopWordTransform =
423
+ <TOOLS extends ToolSet>() =>
424
+ ({ stopStream }: { stopStream: () => void }) =>
425
+ new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({
426
+ // note: this is a simplified transformation for testing;
427
+ // in a real-world version more there would need to be
428
+ // stream buffering and scanning to correctly emit prior text
429
+ // and to detect all STOP occurrences.
430
+ transform(chunk, controller) {
431
+ if (chunk.type !== 'text') {
432
+ controller.enqueue(chunk);
433
+ return;
434
+ }
435
+
436
+ if (chunk.text.includes('STOP')) {
437
+ // stop the stream
438
+ stopStream();
439
+
440
+ // simulate the finish-step event
441
+ controller.enqueue({
442
+ type: 'finish-step',
443
+ finishReason: 'stop',
444
+ logprobs: undefined,
445
+ usage: {
446
+ completionTokens: NaN,
447
+ promptTokens: NaN,
448
+ totalTokens: NaN,
449
+ },
450
+ request: {},
451
+ response: {
452
+ id: 'response-id',
453
+ modelId: 'mock-model-id',
454
+ timestamp: new Date(0),
455
+ },
456
+ warnings: [],
457
+ isContinued: false,
458
+ });
459
+
460
+ // simulate the finish event
461
+ controller.enqueue({
462
+ type: 'finish',
463
+ finishReason: 'stop',
464
+ logprobs: undefined,
465
+ usage: {
466
+ completionTokens: NaN,
467
+ promptTokens: NaN,
468
+ totalTokens: NaN,
469
+ },
470
+ response: {
471
+ id: 'response-id',
472
+ modelId: 'mock-model-id',
473
+ timestamp: new Date(0),
474
+ },
475
+ });
476
+
477
+ return;
478
+ }
479
+
480
+ controller.enqueue(chunk);
481
+ },
482
+ });
483
+ ```
484
+
485
+ #### Multiple transformations
486
+
487
+ You can also provide multiple transformations. They are applied in the order they are provided.
488
+
489
+ ```tsx highlight="4"
490
+ const result = streamText({
491
+ model,
492
+ prompt,
493
+ experimental_transform: [firstTransform, secondTransform],
494
+ });
495
+ ```
496
+
497
+ ## Sources
498
+
499
+ Some providers such as [Perplexity](/providers/ai-sdk-providers/perplexity#sources) and
500
+ [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai#sources) include sources in the response.
501
+
502
+ Currently sources are limited to web pages that ground the response.
503
+ You can access them using the `sources` property of the result.
504
+
505
+ Each `url` source contains the following properties:
506
+
507
+ - `id`: The ID of the source.
508
+ - `url`: The URL of the source.
509
+ - `title`: The optional title of the source.
510
+ - `providerMetadata`: Provider metadata for the source.
511
+
512
+ When you use `generateText`, you can access the sources using the `sources` property:
513
+
514
+ ```ts
515
+ const result = await generateText({
516
+ model: 'google/gemini-2.5-flash',
517
+ tools: {
518
+ google_search: google.tools.googleSearch({}),
519
+ },
520
+ prompt: 'List the top 5 San Francisco news from the past week.',
521
+ });
522
+
523
+ for (const source of result.sources) {
524
+ if (source.sourceType === 'url') {
525
+ console.log('ID:', source.id);
526
+ console.log('Title:', source.title);
527
+ console.log('URL:', source.url);
528
+ console.log('Provider metadata:', source.providerMetadata);
529
+ console.log();
530
+ }
531
+ }
532
+ ```
533
+
534
+ When you use `streamText`, you can access the sources using the `fullStream` property:
535
+
536
+ ```tsx
537
+ const result = streamText({
538
+ model: 'google/gemini-2.5-flash',
539
+ tools: {
540
+ google_search: google.tools.googleSearch({}),
541
+ },
542
+ prompt: 'List the top 5 San Francisco news from the past week.',
543
+ });
544
+
545
+ for await (const part of result.fullStream) {
546
+ if (part.type === 'source' && part.sourceType === 'url') {
547
+ console.log('ID:', part.id);
548
+ console.log('Title:', part.title);
549
+ console.log('URL:', part.url);
550
+ console.log('Provider metadata:', part.providerMetadata);
551
+ console.log();
552
+ }
553
+ }
554
+ ```
555
+
556
+ The sources are also available in the `result.sources` promise.
557
+
558
+ ## Examples
559
+
560
+ You can see `generateText` and `streamText` in action using various frameworks in the following examples:
561
+
562
+ ### `generateText`
563
+
564
+ <ExampleLinks
565
+ examples={[
566
+ {
567
+ title: 'Learn to generate text in Node.js',
568
+ link: '/examples/node/generating-text/generate-text',
569
+ },
570
+ {
571
+ title:
572
+ 'Learn to generate text in Next.js with Route Handlers (AI SDK UI)',
573
+ link: '/examples/next-pages/basics/generating-text',
574
+ },
575
+ {
576
+ title:
577
+ 'Learn to generate text in Next.js with Server Actions (AI SDK RSC)',
578
+ link: '/examples/next-app/basics/generating-text',
579
+ },
580
+ ]}
581
+ />
582
+
583
+ ### `streamText`
584
+
585
+ <ExampleLinks
586
+ examples={[
587
+ {
588
+ title: 'Learn to stream text in Node.js',
589
+ link: '/examples/node/generating-text/stream-text',
590
+ },
591
+ {
592
+ title: 'Learn to stream text in Next.js with Route Handlers (AI SDK UI)',
593
+ link: '/examples/next-pages/basics/streaming-text-generation',
594
+ },
595
+ {
596
+ title: 'Learn to stream text in Next.js with Server Actions (AI SDK RSC)',
597
+ link: '/examples/next-app/basics/streaming-text-generation',
598
+ },
599
+ ]}
600
+ />