Prompt
The Prompt module provides several data structures to simplify creating and
combining prompts.
This module defines the complete structure of a conversation with a large language model, including messages, content parts, and provider-specific options. It supports rich content types like text, files, tool calls, and reasoning.
Example
import { Prompt } from "@effect/ai"
// Create a structured conversationconst conversation = Prompt.make([ { role: "system", content: "You are a helpful assistant specialized in mathematics." }, { role: "user", content: [{ type: "text", text: "What is the derivative of x²?" }] }, { role: "assistant", content: [{ type: "text", text: "The derivative of x² is 2x." }] }])Example
import { Prompt } from "@effect/ai"
// Merge multiple promptsconst systemPrompt = Prompt.make([{ role: "system", content: "You are a coding assistant."}])
const userPrompt = Prompt.make("Help me write a function")
const combined = Prompt.merge(systemPrompt, userPrompt)Combinators
Creates a new prompt from the specified prompt with the provided text content appended to the end of existing system message content.
If no system message exists in the specified prompt, the provided content will be used to create a system message.
Signature
declare const appendSystem: { (content: string): (self: Prompt) => Prompt; (self: Prompt, content: string): Prompt;}Example
import { Prompt } from "@effect/ai"
const systemPrompt = Prompt.make([{ role: "system", content: "You are an expert in programming."}])
const userPrompt = Prompt.make("Hello, world!")
const prompt = Prompt.merge(systemPrompt, userPrompt)
const replaced = Prompt.appendSystem( prompt, " You are a helpful assistant.")// result content: "You are an expert in programming. You are a helpful assistant."Merges a prompt with additional raw input by concatenating messages.
Creates a new prompt containing all messages from both the original prompt, and the provided raw input, maintaining the order of messages.
Signature
declare const merge: { (input: RawInput): (self: Prompt) => Prompt; (self: Prompt, input: RawInput): Prompt;}Example
import { Prompt } from "@effect/ai"
const systemPrompt = Prompt.make([{ role: "system", content: "You are a helpful assistant."}])
const merged = Prompt.merge(systemPrompt, "Hello, world!")Creates a new prompt from the specified prompt with the provided text content prepended to the start of existing system message content.
If no system message exists in the specified prompt, the provided content will be used to create a system message.
Signature
declare const prependSystem: { (content: string): (self: Prompt) => Prompt; (self: Prompt, content: string): Prompt;}Example
import { Prompt } from "@effect/ai"
const systemPrompt = Prompt.make([{ role: "system", content: "You are an expert in programming."}])
const userPrompt = Prompt.make("Hello, world!")
const prompt = Prompt.merge(systemPrompt, userPrompt)
const replaced = Prompt.prependSystem( prompt, "You are a helpful assistant. ")// result content: "You are a helpful assistant. You are an expert in programming."Creates a new prompt from the specified prompt with the system message set to the specified text content.
NOTE: This method will remove and replace any previous system message from the prompt.
Signature
declare const setSystem: { (content: string): (self: Prompt) => Prompt; (self: Prompt, content: string): Prompt;}Example
import { Prompt } from "@effect/ai"
const systemPrompt = Prompt.make([{ role: "system", content: "You are a helpful assistant."}])
const userPrompt = Prompt.make("Hello, world!")
const prompt = Prompt.merge(systemPrompt, userPrompt)
const replaced = Prompt.setSystem( prompt, "You are an expert in programming")Constructors
Constructs a new assistant message.
Signature
declare function assistantMessage(params: MessageConstructorParams<AssistantMessage>): AssistantMessageAn empty prompt with no messages.
Signature
declare const empty: PromptExample
import { Prompt } from "@effect/ai"
const emptyPrompt = Prompt.emptyconsole.log(emptyPrompt.content) // []Constructs a new file part.
Signature
declare function filePart(params: PartConstructorParams<FilePart>): FilePartCreates a Prompt from an array of messages.
Signature
declare function fromMessages(messages: readonly Array<Message>): PromptExample
import { Prompt } from "@effect/ai"
const messages: ReadonlyArray<Prompt.Message> = [ Prompt.makeMessage("system", { content: "You are a coding assistant." }), Prompt.makeMessage("user", { content: [Prompt.makePart("text", { text: "Help me with TypeScript" })] })]
const prompt = Prompt.fromMessages(messages)Creates a Prompt from the response parts of a previous interaction with a large language model.
Converts streaming or non-streaming AI response parts into a structured prompt, typically for use in conversation history or further processing.
Provider-specific metadata attached to response parts is preserved as the provider-specific options of the corresponding prompt parts.
Signature
declare function fromResponseParts(parts: readonly Array<AnyPart>): PromptExample
import { Either } from "effect"import { Prompt, Response } from "@effect/ai"
const responseParts: ReadonlyArray<Response.AnyPart> = [ Response.makePart("text", { text: "Hello there!" }), Response.makePart("tool-call", { id: "call_1", name: "get_time", params: {}, providerExecuted: false }), Response.makePart("tool-result", { id: "call_1", name: "get_time", isFailure: false, result: "10:30 AM", encodedResult: "10:30 AM", providerExecuted: false })]
const prompt = Prompt.fromResponseParts(responseParts)// Creates an assistant message with the response contentCreates a Prompt from an input.
This is the primary constructor for creating prompts, supporting multiple input formats for convenience and flexibility.
Signature
declare function make(input: RawInput): PromptExample
import { Prompt } from "@effect/ai"
// From string - creates a user messageconst textPrompt = Prompt.make("Hello, how are you?")
// From messages arrayconst structuredPrompt = Prompt.make([ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: [{ type: "text", text: "Hi!" }] }])
// From existing promptdeclare const existingPrompt: Prompt.Promptconst copiedPrompt = Prompt.make(existingPrompt)Creates a new message with the specified role.
Signature
declare function makeMessage<Role extends "user" | "assistant" | "system" | "tool">(role: Role, params: Omit<Extract<SystemMessage, { role: Role;}> | Extract<UserMessage, { role: Role;}> | Extract<AssistantMessage, { role: Role;}> | Extract<ToolMessage, { role: Role;}>, "role" | "options" | "~effect/ai/Prompt/Message"> & { readonly options?: Extract<SystemMessage, { role: Role; }> | Extract<UserMessage, { role: Role; }> | Extract<AssistantMessage, { role: Role; }> | Extract<ToolMessage, { role: Role; }>["options"];}): Extract<SystemMessage, { role: Role;}> | Extract<UserMessage, { role: Role;}> | Extract<AssistantMessage, { role: Role;}> | Extract<ToolMessage, { role: Role;}>Example
import { Prompt } from "@effect/ai"
const textPart = Prompt.makePart("text", { text: "Hello, world!"})
const filePart = Prompt.makeMessage("user", { content: [textPart]})Creates a new content part of the specified type.
Signature
declare function makePart<Type extends "text" | "reasoning" | "file" | "tool-call" | "tool-result">(type: Type, params: Omit<Extract<TextPart, { type: Type;}> | Extract<ReasoningPart, { type: Type;}> | Extract<FilePart, { type: Type;}> | Extract<ToolCallPart, { type: Type;}> | Extract<ToolResultPart, { type: Type;}>, "type" | "~effect/ai/Prompt/Part" | "options"> & { readonly options?: Extract<TextPart, { type: Type; }> | Extract<ReasoningPart, { type: Type; }> | Extract<FilePart, { type: Type; }> | Extract<ToolCallPart, { type: Type; }> | Extract<ToolResultPart, { type: Type; }>["options"];}): Extract<TextPart, { type: Type;}> | Extract<ReasoningPart, { type: Type;}> | Extract<FilePart, { type: Type;}> | Extract<ToolCallPart, { type: Type;}> | Extract<ToolResultPart, { type: Type;}>Example
import { Prompt } from "@effect/ai"
const textPart = Prompt.makePart("text", { text: "Hello, world!"})
const filePart = Prompt.makePart("file", { mediaType: "image/png", fileName: "screenshot.png", data: new Uint8Array([1, 2, 3])})Constructs a new reasoning part.
Signature
declare function reasoningPart(params: PartConstructorParams<ReasoningPart>): ReasoningPartConstructs a new system message.
Signature
declare function systemMessage(params: MessageConstructorParams<SystemMessage>): SystemMessageConstructs a new text part.
Signature
declare function textPart(params: PartConstructorParams<TextPart>): TextPartConstructs a new tool call part.
Signature
declare function toolCallPart(params: PartConstructorParams<ToolCallPart>): ToolCallPartConstructs a new tool message.
Signature
declare function toolMessage(params: MessageConstructorParams<ToolMessage>): ToolMessageConstructs a new tool result part.
Signature
declare function toolResultPart(params: PartConstructorParams<ToolResultPart>): ToolResultPartConstructs a new user message.
Signature
declare function userMessage(params: MessageConstructorParams<UserMessage>): UserMessageGuards
Type guard to check if a value is a Message.
Signature
declare function isMessage(u: unknown): u is MessageType guard to check if a value is a Part.
Signature
declare function isPart(u: unknown): u is PartType guard to check if a value is a Prompt.
Signature
declare function isPrompt(u: unknown): u is PromptModels
AssistantMessage
interfaceMessage representing large language model assistant responses.
Signature
interface AssistantMessage extends BaseMessage<"assistant", AssistantMessageOptions> { readonly content: readonly Array<AssistantMessagePart>;}Example
import { Prompt } from "@effect/ai"
const assistantMessage: Prompt.AssistantMessage = Prompt.makeMessage("assistant", { content: [ Prompt.makePart("text", { text: "The user is asking about the weather. I should use the weather tool." }), Prompt.makePart("tool-call", { id: "call_123", name: "get_weather", params: { city: "San Francisco" }, providerExecuted: false }), Prompt.makePart("tool-result", { id: "call_123", name: "get_weather", isFailure: false, result: { temperature: 72, condition: "sunny" }, providerExecuted: false }), Prompt.makePart("text", { text: "The weather in San Francisco is currently 72°F and sunny." }) ]})AssistantMessageEncoded
interfaceEncoded representation of assistant messages for serialization.
Signature
interface AssistantMessageEncoded extends BaseMessageEncoded<"assistant", AssistantMessageOptions> { readonly content: string | readonly Array<AssistantMessagePartEncoded>;}AssistantMessagePart
typeUnion type of content parts allowed in assistant messages.
Signature
type AssistantMessagePart = TextPart | FilePart | ReasoningPart | ToolCallPart | ToolResultPartUnion type of encoded content parts for assistant messages.
Signature
type AssistantMessagePartEncoded = TextPartEncoded | FilePartEncoded | ReasoningPartEncoded | ToolCallPartEncoded | ToolResultPartEncodedBaseMessage
interfaceBase interface for all message types.
Provides common structure including role and provider options.
Signature
interface BaseMessage<Role extends string, Options extends ProviderOptions> { readonly "~effect/ai/Prompt/Message": "~effect/ai/Prompt/Message"; readonly options: Options; readonly role: Role;}BaseMessageEncoded
interfaceBase interface for encoded message types.
Signature
interface BaseMessageEncoded<Role extends string, Options extends ProviderOptions> { readonly options?: Options; readonly role: Role;}Base interface for all content parts.
Provides common structure including type and provider options.
Signature
interface BasePart<Type extends string, Options extends ProviderOptions> { readonly "~effect/ai/Prompt/Part": "~effect/ai/Prompt/Part"; readonly options: Options; readonly type: Type;}BasePartEncoded
interfaceBase interface for encoded content parts.
Signature
interface BasePartEncoded<Type extends string, Options extends ProviderOptions> { readonly options?: Options; readonly type: Type;}Content part representing a file attachment. Files can be provided as base64 strings of data, byte arrays, or URLs.
Supports various file types including images, documents, and binary data.
Signature
interface FilePart extends BasePart<"file", FilePartOptions> { readonly data: string | Uint8Array<ArrayBufferLike> | URL; readonly fileName?: string; readonly mediaType: string;}Example
import { Prompt } from "@effect/ai"
const imagePart: Prompt.FilePart = Prompt.makePart("file", { mediaType: "image/jpeg", fileName: "photo.jpg", data: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."})
const documentPart: Prompt.FilePart = Prompt.makePart("file", { mediaType: "application/pdf", fileName: "report.pdf", data: new Uint8Array([1, 2, 3])})FilePartEncoded
interfaceEncoded representation of file parts for serialization.
Signature
interface FilePartEncoded extends BasePartEncoded<"file", FilePartOptions> { readonly data: string | Uint8Array<ArrayBufferLike> | URL; readonly fileName?: string; readonly mediaType: string;}A type representing all possible message types in a conversation.
Signature
type Message = SystemMessage | UserMessage | AssistantMessage | ToolMessageMessageEncoded
typeA type representing all possible encoded message types for serialization.
Signature
type MessageEncoded = SystemMessageEncoded | UserMessageEncoded | AssistantMessageEncoded | ToolMessageEncodedUnion type representing all possible content parts within messages.
Parts are the building blocks of message content, supporting text, files, reasoning, tool calls, and tool results.
Signature
type Part = TextPart | ReasoningPart | FilePart | ToolCallPart | ToolResultPartPartEncoded
typeEncoded representation of a Part.
Signature
type PartEncoded = TextPartEncoded | ReasoningPartEncoded | FilePartEncoded | ToolCallPartEncoded | ToolResultPartEncodedA Prompt contains a sequence of messages that form the context of a conversation with a large language model.
Signature
interface Prompt extends Pipeable { readonly "~@effect/ai/Prompt": "~@effect/ai/Prompt"; readonly content: readonly Array<Message>;}PromptEncoded
interfaceEncoded representation of prompts for serialization.
Signature
interface PromptEncoded { readonly content: readonly Array<MessageEncoded>;}Schema for provider-specific options which can be attached to both content parts and messages, enabling provider-specific behavior.
Provider-specific options are namespaced by provider and have the structure:
Signature
declare const ProviderOptions: $Record<Key, Constraint>Example
{ "<provider-specific-key>": { // Provider-specific options }}ProviderOptions
typeSignature
type ProviderOptions = typeof ProviderOptions.TypeRaw input types that can be converted into a Prompt.
Supports various input formats for convenience, including simple strings, message arrays, response parts, and existing prompts.
Signature
type RawInput = string | Iterable<MessageEncoded> | PromptExample
import { Prompt } from "@effect/ai"
// String input - creates a user messageconst stringInput: Prompt.RawInput = "Hello, world!"
// Message array inputconst messagesInput: Prompt.RawInput = [ { role: "system", content: "You are helpful." }, { role: "user", content: [{ type: "text", text: "Hi!" }] }]
// Existing promptdeclare const existingPrompt: Prompt.Promptconst promptInput: Prompt.RawInput = existingPromptReasoningPart
interfaceContent part representing reasoning or chain-of-thought.
Signature
interface ReasoningPart extends BasePart<"reasoning", ReasoningPartOptions> { readonly text: string;}Example
import { Prompt } from "@effect/ai"
const reasoningPart: Prompt.ReasoningPart = Prompt.makePart("reasoning", { text: "Let me think step by step: First I need to understand the user's question...",})ReasoningPartEncoded
interfaceEncoded representation of reasoning parts for serialization.
Signature
interface ReasoningPartEncoded extends BasePartEncoded<"reasoning", ReasoningPartOptions> { readonly text: string;}SystemMessage
interfaceMessage representing system instructions or context.
Signature
interface SystemMessage extends BaseMessage<"system", SystemMessageOptions> { readonly content: string;}Example
import { Prompt } from "@effect/ai"
const systemMessage: Prompt.SystemMessage = Prompt.makeMessage("system", { content: "You are a helpful assistant specialized in mathematics. " + "Always show your work step by step."})SystemMessageEncoded
interfaceEncoded representation of system messages for serialization.
Signature
interface SystemMessageEncoded extends BaseMessageEncoded<"system", SystemMessageOptions> { readonly content: string;}Content part representing plain text.
The most basic content type used for textual information in messages.
Signature
interface TextPart extends BasePart<"text", TextPartOptions> { readonly text: string;}Example
import { Prompt } from "@effect/ai"
const textPart: Prompt.TextPart = Prompt.makePart("text", { text: "Hello, how can I help you today?",})TextPartEncoded
interfaceEncoded representation of text parts for serialization.
Signature
interface TextPartEncoded extends BasePartEncoded<"text", TextPartOptions> { readonly text: string;}ToolCallPart
interfaceContent part representing a tool call request.
Signature
interface ToolCallPart extends BasePart<"tool-call", ToolCallPartOptions> { readonly id: string; readonly name: string; readonly params: unknown; readonly providerExecuted: boolean;}Example
import { Prompt } from "@effect/ai"
const toolCallPart: Prompt.ToolCallPart = Prompt.makePart("tool-call", { id: "call_123", name: "get_weather", params: { city: "San Francisco", units: "celsius" }, providerExecuted: false,})ToolCallPartEncoded
interfaceEncoded representation of tool call parts for serialization.
Signature
interface ToolCallPartEncoded extends BasePartEncoded<"tool-call", ToolCallPartOptions> { readonly id: string; readonly name: string; readonly params: unknown; readonly providerExecuted?: boolean;}ToolMessage
interfaceMessage representing tool execution results.
Signature
interface ToolMessage extends BaseMessage<"tool", ToolMessageOptions> { readonly content: readonly Array<ToolResultPart>;}Example
import { Prompt } from "@effect/ai"
const toolMessage: Prompt.ToolMessage = Prompt.makeMessage("tool", { content: [ Prompt.makePart("tool-result", { id: "call_123", name: "search_web", isFailure: false, result: { query: "TypeScript best practices", results: [ { title: "TypeScript Handbook", url: "https://..." }, { title: "Effective TypeScript", url: "https://..." } ] }, providerExecuted: false }) ]})ToolMessageEncoded
interfaceEncoded representation of tool messages for serialization.
Signature
interface ToolMessageEncoded extends BaseMessageEncoded<"tool", ToolMessageOptions> { readonly content: readonly Array<ToolResultPartEncoded>;}ToolMessagePart
typeUnion type of content parts allowed in tool messages.
Signature
type ToolMessagePart = ToolResultPartUnion type of encoded content parts for tool messages.
Signature
type ToolMessagePartEncoded = ToolResultPartEncodedToolResultPart
interfaceContent part representing the result of a tool call.
Signature
interface ToolResultPart extends BasePart<"tool-result", ToolResultPartOptions> { readonly id: string; readonly isFailure: boolean; readonly name: string; readonly providerExecuted: boolean; readonly result: unknown;}Example
import { Prompt } from "@effect/ai"
const toolResultPart: Prompt.ToolResultPart = Prompt.makePart("tool-result", { id: "call_123", name: "get_weather", isFailure: false, result: { temperature: 22, condition: "sunny", humidity: 65 }, providerExecuted: false})ToolResultPartEncoded
interfaceEncoded representation of tool result parts for serialization.
Signature
interface ToolResultPartEncoded extends BasePartEncoded<"tool-result", ToolResultPartOptions> { readonly id: string; readonly isFailure: boolean; readonly name: string; readonly providerExecuted: boolean; readonly result: unknown;}UserMessage
interfaceMessage representing user input or questions.
Signature
interface UserMessage extends BaseMessage<"user", UserMessageOptions> { readonly content: readonly Array<UserMessagePart>;}Example
import { Prompt } from "@effect/ai"
const textUserMessage: Prompt.UserMessage = Prompt.makeMessage("user", { content: [ Prompt.makePart("text", { text: "Can you analyze this image for me?" }) ]})
const multimodalUserMessage: Prompt.UserMessage = Prompt.makeMessage("user", { content: [ Prompt.makePart("text", { text: "What do you see in this image?" }), Prompt.makePart("file", { mediaType: "image/jpeg", fileName: "vacation.jpg", data: "data:image/jpeg;base64,..." }) ]})UserMessageEncoded
interfaceEncoded representation of user messages for serialization.
Signature
interface UserMessageEncoded extends BaseMessageEncoded<"user", UserMessageOptions> { readonly content: string | readonly Array<UserMessagePartEncoded>;}UserMessagePart
typeUnion type of content parts allowed in user messages.
Signature
type UserMessagePart = TextPart | FilePartUnion type of encoded content parts for user messages.
Signature
type UserMessagePartEncoded = TextPartEncoded | FilePartEncodedProviderOptions
AssistantMessageOptions
interfaceRepresents provider-specific options that can be associated with a
AssistantMessage through module augmentation.
Signature
interface AssistantMessageOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}FilePartOptions
interfaceRepresents provider-specific options that can be associated with a
FilePart through module augmentation.
Signature
interface FilePartOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}ReasoningPartOptions
interfaceRepresents provider-specific options that can be associated with a
ReasoningPart through module augmentation.
Signature
interface ReasoningPartOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}SystemMessageOptions
interfaceRepresents provider-specific options that can be associated with a
SystemMessage through module augmentation.
Signature
interface SystemMessageOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}TextPartOptions
interfaceRepresents provider-specific options that can be associated with a
TextPart through module augmentation.
Signature
interface TextPartOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}ToolCallPartOptions
interfaceRepresents provider-specific options that can be associated with a
ToolCallPart through module augmentation.
Signature
interface ToolCallPartOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}ToolMessageOptions
interfaceRepresents provider-specific options that can be associated with a
ToolMessage through module augmentation.
Signature
interface ToolMessageOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}ToolResultPartOptions
interfaceRepresents provider-specific options that can be associated with a
ToolResultPart through module augmentation.
Signature
interface ToolResultPartOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}UserMessageOptions
interfaceRepresents provider-specific options that can be associated with a
UserMessage through module augmentation.
Signature
interface UserMessageOptions extends ProviderOptions { [key: string]: unknown; [key: number]: unknown; [key: symbol]: unknown;}Schemas
Schema for validation and encoding of assistant messages.
Signature
declare const AssistantMessage: anySchema for validation and encoding of file parts.
Signature
declare const FilePart: anySchema for parsing a Prompt from JSON strings.
Signature
declare const FromJson: anySchema for validation and encoding of messages.
Signature
declare const Message: anySchema for decoding message content (i.e. an array containing a single
TextPart) from a string.
Signature
declare const MessageContentFromString: Schema.Schema<Arr.NonEmptyReadonlyArray<TextPart>, string>Schema for validation and encoding of prompts.
Signature
declare const Prompt: anyDescribes a schema that represents a Prompt instance.
Signature
declare class PromptFromSelf extends {} { constructor(_: never);}Schema for validation and encoding of reasoning parts.
Signature
declare const ReasoningPart: anySchema for validation and encoding of system messages.
Signature
declare const SystemMessage: anySchema for validation and encoding of text parts.
Signature
declare const TextPart: anySchema for validation and encoding of tool call parts.
Signature
declare const ToolCallPart: anySchema for validation and encoding of tool messages.
Signature
declare const ToolMessage: anySchema for validation and encoding of tool result parts.
Signature
declare const ToolResultPart: anySchema for validation and encoding of user messages.
Signature
declare const UserMessage: anyType Ids
Unique identifier for Message instances.
Signature
declare const MessageTypeId: "~effect/ai/Prompt/Message"MessageTypeId
typeType-level representation of the Message identifier.
Signature
type MessageTypeId = typeof MessageTypeIdUnique identifier for Part instances.
Signature
declare const PartTypeId: "~effect/ai/Prompt/Part"PartTypeId
typeType-level representation of the Part identifier.
Signature
type PartTypeId = typeof PartTypeIdUnique identifier for Prompt instances.
Signature
declare const TypeId: "~@effect/ai/Prompt"Type-level representation of the Prompt identifier.
Signature
type TypeId = typeof TypeIdUtility Types
A utility type for specifying the parameters required to construct a specific message for a prompt.
Signature
type MessageConstructorParams<M extends Message> = Omit<M, MessageTypeId | "role" | "options"> & { readonly options?: Part["options"];}A utility type for specifying the parameters required to construct a specific part of a prompt.
Signature
type PartConstructorParams<P extends Part> = Omit<P, PartTypeId | "type" | "options"> & { readonly options?: Part["options"];}