`ModelMessage`

ModelMessage

ModelMessage는 AI SDK Core 함수에서 사용되는 기본 메시지 구조를 나타냅니다. AI SDK Core 함수의 messages 필드에서 사용할 수 있는 다양한 메시지 유형을 포함합니다. modelMessageSchema export로 ModelMessage의 Zod 스키마에 접근할 수 있습니다.

출처: 문서

본문

ModelMessage Types

SystemModelMessage

시스템 정보를 포함할 수 있는 시스템 메시지입니다.

type SystemModelMessage = {
  role: 'system';
  content: string;
};

systemModelMessageSchema export로 SystemModelMessage의 Zod 스키마에 접근할 수 있습니다.

시스템 지시문에는 시스템 메시지 대신 최상위 `instructions` 속성을 사용하세요. AI SDK 함수는 `allowSystemInMessages`가 `true`로 설정되지 않는 한 기본적으로 `prompt` 또는 `messages`의 시스템 메시지를 거부합니다. 선택(opt-in)하면 사용자가 시스템 메시지를 주입할 수 있는 경우 프롬프트 인젝션 위험이 생길 수 있습니다.

UserModelMessage

텍스트 또는 텍스트, 이미지, 파일의 조합을 포함할 수 있는 사용자 메시지입니다.

type UserModelMessage = {
  role: 'user';
  content: UserContent;
};

type UserContent = string | Array<TextPart | ImagePart | FilePart>;

userModelMessageSchema export로 UserModelMessage의 Zod 스키마에 접근할 수 있습니다.

AssistantModelMessage

텍스트, 툴 호출 또는 둘의 조합을 포함할 수 있는 어시스턴트 메시지입니다.

type AssistantModelMessage = {
  role: 'assistant';
  content: AssistantContent;
};

type AssistantContent = string | Array<TextPart | CustomPart | ToolCallPart>;

assistantModelMessageSchema export로 AssistantModelMessage의 Zod 스키마에 접근할 수 있습니다.

ToolModelMessage

하나 이상의 툴 호출 결과를 포함하는 툴 메시지입니다.

type ToolModelMessage = {
  role: 'tool';
  content: ToolContent;
};

type ToolContent = Array<ToolResultPart>;

toolModelMessageSchema export로 ToolModelMessage의 Zod 스키마에 접근할 수 있습니다.

ModelMessage Parts

TextPart

프롬프트의 텍스트 내용 파트를 나타냅니다. 텍스트 문자열을 포함합니다.

export interface TextPart {
  type: 'text';
  /**
   * The text content.
   */
  text: string;
}

ImagePart Deprecated

`ImagePart`는 deprecated 되었습니다. 대신 `mediaType: 'image'`(또는 더 구체적인 `image/*` 하위 유형)와 함께 [`FilePart`](#filepart)를 사용하세요.

사용자 메시지의 이미지 파트를 나타냅니다.

/**
 * @deprecated Use `FilePart` with `mediaType: 'image'` instead.
 */
export interface ImagePart {
  type: 'image';

  /**
   * Image data. Can either be:
   * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
   * - URL: a URL that points to the image
   * - ProviderReference: a provider reference from `uploadFile`
   */
  image: DataContent | URL | ProviderReference;

  /**
   * Optional IANA media type of the image.
   * We recommend leaving this out as it will be detected automatically.
   */
  mediaType?: string;
}

FilePart

사용자 메시지의 파일 파트를 나타냅니다.

export interface FilePart {
  type: 'file';

  /**
   * File data. Use the tagged `FileData` shape.
   * Bare `DataContent`, `URL`, and `ProviderReference` shorthands are also supported.
   */
  data: FileData | DataContent | URL | ProviderReference;

  /**
   * Optional filename of the file.
   */
  filename?: string;

  /**
   * Either a full IANA media type (`type/subtype`, e.g. `image/png`) or just
   * the top-level IANA segment (e.g. `image`, `audio`, `video`, `text`).
   */
  mediaType: string;
}

export type FileData =
  // Raw bytes as a base64 string, Uint8Array, ArrayBuffer, or Buffer.
  | { type: 'data'; data: DataContent }
  // A URL that points to the file.
  | { type: 'url'; url: URL }
  // A provider reference from `uploadFile`.
  | { type: 'reference'; reference: ProviderReference }
  // Inline text content.
  | { type: 'text'; text: string };

CustomPart

프로바이더별 커스텀 내용 파트를 나타냅니다. kind 필드는 {provider}.{provider-type} 형식으로 내용 유형을 식별합니다.

export interface CustomPart {
  type: 'custom';

  /**
   * The kind of custom content, in the format `{provider}.{provider-type}`.
   */
  kind: `${string}.${string}`;

  /**
   * Additional provider-specific metadata.
   */
  providerOptions?: ProviderOptions;
}

ToolCallPart

일반적으로 AI 모델이 생성하는 프롬프트의 툴 호출 내용 파트를 나타냅니다.

export interface ToolCallPart {
  type: 'tool-call';

  /**
   * ID of the tool call. This ID is used to match the tool call with the tool result.
   */
  toolCallId: string;

  /**
   * Name of the tool that is being called.
   */
  toolName: string;

  /**
   * Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
   */
  args: unknown;
}

ToolResultPart

툴 메시지에서 툴 호출의 결과를 나타냅니다.

export interface ToolResultPart {
  type: 'tool-result';

  /**
   * ID of the tool call that this result is associated with.
   */
  toolCallId: string;

  /**
   * Name of the tool that generated this result.
   */
  toolName: string;

  /**
   * Result of the tool call. This is a JSON-serializable object.
   */
  output: LanguageModelV4ToolResultOutput;

  /**
  Additional provider-specific metadata. They are passed through
  to the provider from the AI SDK and enable provider-specific
  functionality that can be fully encapsulated in the provider.
  */
  providerOptions?: ProviderOptions;
}

LanguageModelV4ToolResultOutput

/**
 * Output of a tool result.
 */
export type ToolResultOutput =
  | {
      /**
       * Text tool output that should be directly sent to the API.
       */
      type: 'text';
      value: string;

      /**
       * Provider-specific options.
       */
      providerOptions?: ProviderOptions;
    }
  | {
      type: 'json';
      value: JSONValue;

      /**
       * Provider-specific options.
       */
      providerOptions?: ProviderOptions;
    }
  | {
      /**
       * Type when the user has denied the execution of the tool call.
       */
      type: 'execution-denied';

      /**
       * Optional reason for the execution denial.
       */
      reason?: string;

      /**
       * Provider-specific options.
       */
      providerOptions?: ProviderOptions;
    }
  | {
      type: 'error-text';
      value: string;

      /**
       * Provider-specific options.
       */
      providerOptions?: ProviderOptions;
    }
  | {
      type: 'error-json';
      value: JSONValue;

      /**
       * Provider-specific options.
       */
      providerOptions?: ProviderOptions;
    }
  | {
      type: 'content';
      value: Array<
        | {
            type: 'text';

            /**
Text content.
*/
            text: string;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * @deprecated Use image-data or file-data instead.
             */
            type: 'media';
            data: string;
            mediaType: string;
          }
        | {
            type: 'file-data';

            /**
Base-64 encoded media data.
*/
            data: string;

            /**
IANA media type.
@see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
            mediaType: string;

            /**
             * Optional filename of the file.
             */
            filename?: string;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            type: 'file-url';

            /**
             * URL of the file.
             */
            url: string;

            /**
             * IANA media type of the file.
             * Used by providers to determine how to handle the file (e.g. image vs document).
             * Optional; if omitted, the SDK will attempt to infer it from the URL file extension.
             */
            mediaType?: string;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * @deprecated Use file-reference instead.
             */
            type: 'file-id';

            /**
             * ID of the file.
             *
             * If you use multiple providers, you need to
             * specify the provider specific ids using
             * the Record option. The key is the provider
             * name, e.g. 'openai' or 'anthropic'.
             */
            fileId: string | Record<string, string>;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            type: 'file-reference';

            /**
             * Provider-specific references for the file.
             * The key is the provider name, e.g. 'openai' or 'anthropic'.
             */
            providerReference: ProviderReference;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * @deprecated Use file-data instead.
             * Images that are referenced using base64 encoded data.
             */
            type: 'image-data';

            /**
Base-64 encoded image data.
*/
            data: string;

            /**
IANA media type.
@see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
            mediaType: string;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * @deprecated Use file-url instead.
             * Images that are referenced using a URL.
             */
            type: 'image-url';

            /**
             * URL of the image.
             */
            url: string;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * @deprecated Use file-reference instead.
             * Images that are referenced using a provider file id.
             */
            type: 'image-file-id';

            /**
             * Image that is referenced using a provider file id.
             *
             * If you use multiple providers, you need to
             * specify the provider specific ids using
             * the Record option. The key is the provider
             * name, e.g. 'openai' or 'anthropic'.
             */
            fileId: string | Record<string, string>;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * @deprecated Use file-reference instead.
             * Images that are referenced using a provider reference.
             */
            type: 'image-file-reference';

            /**
             * Provider-specific references for the image file.
             * The key is the provider name, e.g. 'openai' or 'anthropic'.
             */
            providerReference: ProviderReference;

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
        | {
            /**
             * Custom content part. This can be used to implement
             * provider-specific content parts.
             */
            type: 'custom';

            /**
             * Provider-specific options.
             */
            providerOptions?: ProviderOptions;
          }
      >;
    };

더 알아보기 (Learn more)