Generating Text
Learn how to generate text with the AI SDK.
Large language models (LLMs) can generate text in response to a prompt, which can contain instructions and information to process. For example, you can ask a model to come up with a recipe, draft an email, or summarize a document.
The AI SDK Core provides two functions to generate text and stream it from LLMs:
generateText: Generates text for a given prompt and model.streamText: Streams text from a given prompt and model.
Advanced LLM features such as tool calling and structured data generation are built on top of text generation.
generateText
You can generate text using the generateText 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.
You can use more advanced prompts to generate text with more complex instructions and content:
The result object of generateText contains several promises that resolve when all required data is available:
result.content: The content that was generated in the last step.result.text: The generated text.result.reasoning: The full reasoning that the model has generated in the last step.result.reasoningText: The reasoning text of the model (only available for some models).result.files: The files that were generated in the last step.result.sources: Sources that have been used as references in the last step (only available for some models).result.toolCalls: The tool calls that were made in the last step.result.toolResults: The results of the tool calls from the last step.result.finishReason: The reason the model finished generating text.result.usage: The usage of the model during the final step of text generation.result.totalUsage: The total usage across all steps (for multi-step generations).result.warnings: Warnings from the model provider (e.g. unsupported settings).result.request: Additional request information.result.response: Additional response information, including response messages and body.result.providerMetadata: Additional provider-specific metadata.result.steps: Details for all steps, useful for getting information about intermediate steps.result.experimental_output: The generated structured output using theexperimental_outputspecification.
Accessing response headers & body
Sometimes you need access to the full response from the model provider, e.g. to access some provider-specific headers or body content.
You can access the raw response headers and body using the response property:
streamText
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.
AI SDK Core provides the streamText function which simplifies streaming text from LLMs:
result.textStream is both a ReadableStream and an AsyncIterable.
streamText immediately starts streaming and suppresses errors to prevent
server crashes. Use the onError callback to log errors.
You can use streamText on its own or in combination with AI SDK
UI and AI SDK
RSC.
The result object contains several helper functions to make the integration into AI SDK UI easier:
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.result.pipeUIMessageStreamToResponse(): Writes UI Message stream delta output to a Node.js response-like object.result.toTextStreamResponse(): Creates a simple text stream HTTP response.result.pipeTextStreamToResponse(): Writes text delta output to a Node.js response-like object.
streamText is using backpressure and only generates tokens as they are
requested. You need to consume the stream in order for it to finish.
It also provides several promises that resolve when the stream is finished:
result.content: The content that was generated in the last step.result.text: The generated text.result.reasoning: The full reasoning that the model has generated.result.reasoningText: The reasoning text of the model (only available for some models).result.files: Files that have been generated by the model in the last step.result.sources: Sources that have been used as references in the last step (only available for some models).result.toolCalls: The tool calls that have been executed in the last step.result.toolResults: The tool results that have been generated in the last step.result.finishReason: The reason the model finished generating text.result.usage: The usage of the model during the final step of text generation.result.totalUsage: The total usage across all steps (for multi-step generations).result.warnings: Warnings from the model provider (e.g. unsupported settings).result.steps: Details for all steps, useful for getting information about intermediate steps.result.request: Additional request information from the last step.result.response: Additional response information from the last step.result.providerMetadata: Additional provider-specific metadata from the last step.
onError callback
streamText immediately starts streaming to enable sending data without waiting for the model.
Errors become part of the stream and are not thrown to prevent e.g. servers from crashing.
To log errors, you can provide an onError callback that is triggered when an error occurs.
onChunk callback
When using streamText, you can provide an onChunk callback that is triggered for each chunk of the stream.
It receives the following chunk types:
textreasoningsourcetool-calltool-input-starttool-input-deltatool-resultraw
onFinish callback
When using streamText, you can provide an onFinish callback that is triggered when the stream is finished (
API Reference
).
It contains the text, usage information, finish reason, messages, steps, total usage, and more:
fullStream property
You can read a stream with all events using the fullStream property.
This can be useful if you want to implement your own UI or handle the stream in a different way.
Here is an example of how to use the fullStream property:
Stream transformation
You can use the experimental_transform option to transform the stream.
This is useful for e.g. filtering, changing, or smoothing the text stream.
The transformations are applied before the callbacks are invoked and the promises are resolved.
If you e.g. have a transformation that changes all text to uppercase, the onFinish callback will receive the transformed text.
Smoothing streams
The AI SDK Core provides a smoothStream function that
can be used to smooth out text streaming.
Custom transformations
You can also implement your own custom transformations. The transformation function receives the tools that are available to the model, and returns a function that is used to transform the stream. Tools can either be generic or limited to the tools that you are using.
Here is an example of how to implement a custom transformation that converts all text to uppercase:
You can also stop the stream using the stopStream function.
This is e.g. useful if you want to stop the stream when model guardrails are violated, e.g. by generating inappropriate content.
When you invoke stopStream, it is important to simulate the step-finish and finish events to guarantee that a well-formed stream is returned
and all callbacks are invoked.
Multiple transformations
You can also provide multiple transformations. They are applied in the order they are provided.
Sources
Some providers such as Perplexity and Google Generative AI include sources in the response.
Currently sources are limited to web pages that ground the response.
You can access them using the sources property of the result.
Each url source contains the following properties:
id: The ID of the source.url: The URL of the source.title: The optional title of the source.providerMetadata: Provider metadata for the source.
When you use generateText, you can access the sources using the sources property:
When you use streamText, you can access the sources using the fullStream property:
The sources are also available in the result.sources promise.
Examples
You can see generateText and streamText in action using various frameworks in the following examples: