Prompts
Learn about the Prompt structure used in the AI SDK.
Prompts are instructions that you give a large language model (LLM) to tell it what to do. It's like when you ask someone for directions; the clearer your question, the better the directions you'll get.
Many LLM providers offer complex interfaces for specifying prompts. They involve different roles and message types. While these interfaces are powerful, they can be hard to use and understand.
In order to simplify prompting, the AI SDK supports text, message, and system prompts.
Text Prompts
Text prompts are strings. They are ideal for simple generation use cases, e.g. repeatedly generating content for variants of the same prompt text.
You can set text prompts using the prompt property made available by AI SDK functions like streamText or generateObject.
You can structure the text in any way and inject variables, e.g. using a template literal.
You can also use template literals to provide dynamic data to your prompt.
System Prompts
System prompts are the initial set of instructions given to models that help guide and constrain the models' behaviors and responses.
You can set system prompts using the system property.
System prompts work with both the prompt and the messages properties.
System messages in prompt or messages are allowed by default with a warning; use the system property for system instructions, set allowSystemInMessages: true when you need to send existing message histories that contain system messages, or set allowSystemInMessages: false to reject them.
System messages in prompt or messages can create a prompt injection
attack risk, where users can override or set the system prompt by injecting
system messages. Ideally, provide system instructions through the system
option instead. Set allowSystemInMessages: true to suppress the warning, or
allowSystemInMessages: false to throw an error.
Message Prompts
A message prompt is an array of user, assistant, and tool messages.
They are great for chat interfaces and more complex, multi-modal prompts.
You can use the messages property to set message prompts.
Each message has a role and a content property. The content can either be text (for user and assistant messages), or an array of relevant parts (data) for that message type.
Instead of sending a text in the content property, you can send an array of parts that includes a mix of text and other content parts.
Not all language models support all message and content types. For example, some models might not be capable of handling multi-modal inputs or tool messages. Learn more about the capabilities of select models.
Provider Options
You can pass through additional provider-specific metadata to enable provider-specific functionality at 3 levels.
Function Call Level
Functions like streamText or generateText accept a providerOptions property.
Adding provider options at the function call level should be used when you do not need granular control over where the provider options are applied.
Message Level
For granular control over applying provider options at the message level, you can pass providerOptions to the message object:
Message Part Level
Certain provider-specific options require configuration at the message part level:
AI SDK UI hooks like useChat return
arrays of UIMessage objects, which do not support provider options. We
recommend using the
convertToModelMessages
function to convert UIMessage objects to
ModelMessage objects before
applying or appending message(s) or message parts with providerOptions.
User Messages
Text Parts
Text content is the most common type of content. It is a string that is passed to the model.
If you only need to send text content in a message, the content property can be a string,
but you can also use it to send multiple content parts.
Image Parts
User messages can include image parts. An image can be one of the following:
- base64-encoded image:
stringwith base-64 encoded content- data URL
string, e.g.data:image/png;base64,...
- binary image:
ArrayBufferUint8ArrayBuffer
- URL:
- http(s) URL
string, e.g.https://example.com/image.png URLobject, e.g.new URL('https://example.com/image.png')
- http(s) URL
Example: Binary image (Buffer)
Example: Base-64 encoded image (string)
Example: Image URL (string)
File Parts
Only a few providers and models currently support file parts: Google
Generative AI, Google
Vertex AI,
OpenAI (for wav and mp3 audio with
gpt-4o-audio-preview), Anthropic,
OpenAI (for pdf).
User messages can include file parts. A file can be one of the following:
- base64-encoded file:
stringwith base-64 encoded content- data URL
string, e.g.data:image/png;base64,...
- binary data:
ArrayBufferUint8ArrayBuffer
- URL:
- http(s) URL
string, e.g.https://example.com/some.pdf URLobject, e.g.new URL('https://example.com/some.pdf')
- http(s) URL
You need to specify the MIME type of the file you are sending.
Example: PDF file from Buffer
Example: mp3 audio file from Buffer
Custom Download Function (Experimental)
You can use custom download functions to implement throttling, retries, authentication, caching, and more.
The default download implementation automatically downloads files in parallel when they are not supported by the model.
Custom download function can be passed via the experimental_download property:
The experimental_download option is experimental and may change in future
releases.
Assistant Messages
Assistant messages are messages that have a role of assistant.
They are typically previous responses from the assistant
and can contain text, reasoning, and tool call parts.
Example: Assistant message with text content
Example: Assistant message with text content in array
Example: Assistant message with tool call content
Example: Assistant message with file content
This content part is for model-generated files. Only a few models support this, and only for file types that they can generate.
Tool messages
Tools (also known as function calling) are programs that you can provide an LLM to extend its built-in functionality. This can be anything from calling an external API to calling functions within your UI. Learn more about Tools in the next section.
For models that support tool calls, assistant messages can contain tool call parts, and tool messages can contain tool output parts. A single assistant message can call multiple tools, and a single tool message can contain multiple tool results.
Multi-modal Tool Results
Multi-part tool results are experimental and only supported by Anthropic.
Tool results can be multi-part and multi-modal, e.g. a text and an image.
You can use the experimental_content property on tool parts to specify multi-part tool results.
System Messages
System messages are messages that are sent to the model before the user messages to guide the assistant's behavior.
You can alternatively use the system property.