Core function message input
Constructing prompts for generateText or streamText
AI Tools · Type Alias
CoreMessage is the union used to describe system, user, assistant, and tool messages for AI SDK Core functions. Its role determines the allowed content structure, including multimodal user content and tool results.
CoreMessage is the union of message shapes accepted by AI SDK Core operations. Each value identifies its role and carries role-specific content, allowing prompts to represent system instructions, user input, assistant output, or tool results.
type CoreMessage = CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage;| Name | Type | Description |
|---|---|---|
CoreMessage | CoreSystemMessage | CoreUserMessage | CoreAssistantMessage | CoreToolMessage | An ordered conversation message with one of the documented system, user, assistant, or tool roles. |
A discriminated union type for AI SDK Core conversation messages.
import { generateText, type CoreMessage } from 'ai';
import { openai } from '@ai-sdk/openai';
const messages: CoreMessage[] = [
{ role: 'system', content: 'Answer concisely.' },
{ role: 'user', content: 'What is 2 + 2?' },
];
const { text } = await generateText({
model: openai('gpt-4o-mini'),
messages,
});
console.log(text);The message array is explicitly typed as CoreMessage[] and passed to a core text-generation function.
Constructing prompts for generateText or streamText
Representing image or file prompts
Preserving model tool calls and their results
CoreMessage is a union of role-specific message types: system, user, assistant, and tool. The role determines the content shape and meaning.
Follow-up: How would you represent an image prompt? Use a user message whose content is a UserContent array containing the appropriate image part alongside any text part.
A system message has role 'system' and string content, while a user message has role 'user' and UserContent. UserContent may be plain text or an array containing text, image, and file parts.
Follow-up: Why use structured parts instead of one string? Structured parts allow multimodal prompts while preserving the type and data of each content element.
A tool message has role 'tool' and ToolContent, represented as an array of tool-result parts. An assistant message can contain generated text, tool calls, or both.
Follow-up: What should be validated before sending these values? Validate the role-specific structure with the exported coreMessageSchema when runtime input is not trusted.
CoreSystemMessageCoreUserMessageCoreAssistantMessageCoreToolMessagecoreMessageSchemaTextPartImagePartFilePart