View Source Anthropic.Messages.Content.Image (anthropic_community v0.5.0)

Validates, processes, and converts images into Anthropic.Messages.Content.Image request content blocks for use in a user message, per the vision guide.

Features

  • Supports multiple image formats: JPEG, PNG, GIF, and WEBP.
  • Validates images against a set of predefined dimensions and aspect ratios.
  • Converts images to a base64 encoded string for easy transmission and storage.
  • Provides detailed error handling for various failure scenarios during image processing.

Usage

The primary entry point to the module is process_image/2, which takes an image input and an input type and returns a %Anthropic.Messages.Content.Image{} struct ready to be embedded directly in a message's content list:

{:ok, image_block} = Anthropic.Messages.Content.Image.process_image("/path/to/image.png", :path)

Anthropic.Messages.create(client,
  model: "claude-opus-4-8",
  max_tokens: 1024,
  messages: [
    %{role: "user", content: [image_block, %{type: "text", text: "What's in this image?"}]}
  ]
)

Supported Input Types

  • :binary - Direct binary data of the image.
  • :path - A file system path to the image.
  • :base64 - A base64 encoded string of the image.

Summary

Functions

Processes the given image input based on the specified input type and converts it into a %Anthropic.Messages.Content.Image{} content block.

Types

@type dimensions() :: {integer(), integer()}
@type image_input() :: binary() | String.t()
@type input_type() :: :binary | :path | :base64
@type mime_type() :: String.t()
@type process_output() :: {:ok, t()} | {:error, String.t()}
@type supported_size() :: {String.t(), dimensions()}
@type t() :: %Anthropic.Messages.Content.Image{
  cache_control: map() | nil,
  data: String.t(),
  media_type: String.t(),
  source_type: String.t()
}

Functions

Link to this function

process_image(image_input, input_type)

View Source
@spec process_image(image_input(), input_type()) :: process_output()

Processes the given image input based on the specified input type and converts it into a %Anthropic.Messages.Content.Image{} content block.

Parameters

  • image_input: The image input, which can be binary data, a file path, or a base64 encoded string.
  • input_type: A symbol indicating the type of the image_input (:binary, :path, :base64).

Returns

An {:ok, %Anthropic.Messages.Content.Image{}} tuple on success, or {:error, reason} on failure.