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

A PDF or plain-text document content block, usable anywhere a message can include content — vision-capable models can read PDFs directly. Supports four source variants:

  • :base64 — inline PDF bytes, built via process_document/3.
  • :url — a hosted PDF, built via from_url/2.
  • :text — inline plain text, built via from_text/2.
  • :content — a pre-formatted string or list of content blocks (for citing structured content rather than a raw document), built via from_content/2.

Examples

{:ok, doc} = Anthropic.Messages.Content.Document.process_document("/path/to/report.pdf", :path)

Anthropic.Messages.create(client,
  model: "claude-opus-4-8",
  max_tokens: 1024,
  messages: [
    %{role: "user", content: [doc, %{type: "text", text: "Summarize this document."}]}
  ]
)

Summary

Functions

Builds a document content block from pre-formatted content — a string or a list of content-block maps — rather than a raw document. Useful for citing structured content that didn't come from an actual PDF/text file.

Builds a plain-text document content block from inline text.

Builds a document content block referencing a hosted PDF URL.

Processes a local PDF (binary data, a file path, or an already-base64-encoded string) into a base64 document content block.

Types

@type build_opts() :: [
  title: String.t(),
  context: String.t(),
  citations: map(),
  cache_control: map()
]
@type input_type() :: :binary | :path | :base64
@type process_output() :: {:ok, t()} | {:error, String.t()}
@type source_type() :: String.t()
@type t() :: %Anthropic.Messages.Content.Document{
  cache_control: map() | nil,
  citations: map() | nil,
  content: String.t() | [map()] | nil,
  context: String.t() | nil,
  data: String.t() | nil,
  media_type: String.t() | nil,
  source_type: source_type(),
  title: String.t() | nil,
  url: String.t() | nil
}

Functions

Link to this function

from_content(content, opts \\ [])

View Source
@spec from_content(String.t() | [map()], build_opts()) :: t()

Builds a document content block from pre-formatted content — a string or a list of content-block maps — rather than a raw document. Useful for citing structured content that didn't come from an actual PDF/text file.

Link to this function

from_text(text, opts \\ [])

View Source
@spec from_text(String.t(), build_opts()) :: t()

Builds a plain-text document content block from inline text.

Link to this function

from_url(url, opts \\ [])

View Source
@spec from_url(String.t(), build_opts()) :: t()

Builds a document content block referencing a hosted PDF URL.

Link to this function

process_document(input, input_type, opts \\ [])

View Source
@spec process_document(binary() | String.t(), input_type(), build_opts()) ::
  process_output()

Processes a local PDF (binary data, a file path, or an already-base64-encoded string) into a base64 document content block.

Options

  • :title — optional document title shown to the model.
  • :context — optional context text shown to the model alongside the document.
  • :citations — optional citations config map, e.g. %{enabled: true}.
  • :cache_control — optional Anthropic.CacheControl map.