anthropic/types/message

Core message types for the Anthropic Messages API

This module defines the fundamental types for working with Claude’s API, including messages, content blocks, and their JSON serialization.

Types

Content block in a message - represents different types of content

pub type ContentBlock {
  TextBlock(text: String)
  ImageBlock(source: ImageSource)
  ToolUseBlock(id: String, name: String, input: String)
  ToolResultBlock(
    tool_use_id: String,
    content: String,
    is_error: option.Option(Bool),
  )
}

Constructors

  • TextBlock(text: String)

    Plain text content

  • ImageBlock(source: ImageSource)

    Image content with source data

  • ToolUseBlock(id: String, name: String, input: String)

    Tool use request from the assistant

    Arguments

    id

    Unique identifier for this tool use

    name

    Name of the tool being called

    input

    JSON string of input arguments for the tool

  • ToolResultBlock(
      tool_use_id: String,
      content: String,
      is_error: option.Option(Bool),
    )

    Result of a tool execution

    Arguments

    tool_use_id

    ID of the tool use this is responding to

    content

    Content of the tool result (can be text or error)

    is_error

    Whether this result represents an error

Image source for image content blocks

pub type ImageSource {
  ImageSource(
    source_type: ImageSourceType,
    media_type: String,
    data: String,
  )
}

Constructors

  • ImageSource(
      source_type: ImageSourceType,
      media_type: String,
      data: String,
    )

    Arguments

    source_type

    Type of image source (currently only base64)

    media_type

    MIME type of the image (e.g., “image/jpeg”, “image/png”, “image/gif”, “image/webp”)

    data

    Base64 encoded image data

Source type for images

pub type ImageSourceType {
  Base64
}

Constructors

  • Base64

    Base64 encoded image data

A message in a conversation

pub type Message {
  Message(role: Role, content: List(ContentBlock))
}

Constructors

  • Message(role: Role, content: List(ContentBlock))

    Arguments

    role

    Role of the message sender

    content

    List of content blocks in the message

Role of a message in a conversation

pub type Role {
  User
  Assistant
}

Constructors

  • User

    Message from the user

  • Assistant

    Message from the assistant (Claude)

Values

pub fn assistant_message(text_content: String) -> Message

Create an assistant message with text content

pub fn content_block_to_json(block: ContentBlock) -> json.Json

Encode a ContentBlock to JSON

pub fn content_block_to_json_string(
  block: ContentBlock,
) -> String

Convert a content block to a JSON string

pub fn content_block_type(block: ContentBlock) -> String

Get the type of a content block as a string

pub fn get_tool_uses(message: Message) -> List(ContentBlock)

Get all tool use blocks from a message

pub fn has_tool_use(message: Message) -> Bool

Check if a message contains tool use blocks

pub fn image(
  media_type: String,
  base64_data: String,
) -> ContentBlock

Create an image content block from base64 data

pub fn image_source_from_dict(
  dict: dict.Dict(String, String),
) -> Result(ImageSource, String)

Create an ImageSource from a dict (for JSON decoding)

pub fn image_source_to_json(source: ImageSource) -> json.Json

Encode an ImageSource to JSON

pub fn message(
  role: Role,
  content: List(ContentBlock),
) -> Message

Create a message with the given role and content blocks

pub fn message_text(message: Message) -> String

Get the text content from a message (concatenated)

pub fn message_to_json(message: Message) -> json.Json

Encode a Message to JSON

pub fn message_to_json_string(message: Message) -> String

Convert a message to a JSON string

pub fn messages_to_json(messages: List(Message)) -> json.Json

Encode a list of Messages to JSON

pub fn role_from_string(str: String) -> Result(Role, String)

Convert a string to Role

pub fn role_to_json(role: Role) -> json.Json

Encode a Role to JSON string value

pub fn role_to_string(role: Role) -> String

Convert a Role to string

pub fn text(content: String) -> ContentBlock

Create a text content block

pub fn tool_error(
  tool_use_id: String,
  error_message: String,
) -> ContentBlock

Create a tool error result content block

pub fn tool_result(
  tool_use_id: String,
  content: String,
) -> ContentBlock

Create a tool result content block

pub fn tool_use(
  id: String,
  name: String,
  input: String,
) -> ContentBlock

Create a tool use content block

pub fn user_message(text_content: String) -> Message

Create a user message with text content

Search Document