Class CitationDocument

java.lang.Object
org.springframework.ai.anthropic.api.CitationDocument

public final class CitationDocument extends Object
Builder class for creating citation-enabled documents. Provides a fluent API for constructing documents of different types that can be converted to ContentBlocks for the Anthropic API.

Citations allow Claude to reference specific parts of provided documents in its responses. When a citation document is included in a prompt, Claude can cite the source material, and citation metadata (character ranges, page numbers, or content blocks) is returned in the response.

Usage Examples

Plain Text Document:

CitationDocument document = CitationDocument.builder()
    .plainText("The Eiffel Tower was completed in 1889 in Paris, France.")
    .title("Eiffel Tower Facts")
    .build();

AnthropicChatOptions options = AnthropicChatOptions.builder()
    .model(AnthropicApi.ChatModel.CLAUDE_3_7_SONNET.getName())
    .citationDocuments(document)
    .build();

ChatResponse response = chatModel.call(new Prompt("When was the Eiffel Tower built?", options));

// Citations are available in response metadata
List<Citation> citations = (List<Citation>) response.getMetadata().get("citations");

PDF Document:

CitationDocument document = CitationDocument.builder()
    .pdfFile("path/to/document.pdf")
    .title("Technical Specification")
    .build();

// PDF citations include page numbers

Custom Content Blocks:

CitationDocument document = CitationDocument.builder()
    .customContent(
        "Fact 1: The Great Wall spans 21,196 kilometers.",
        "Fact 2: Construction began in the 7th century BC.",
        "Fact 3: It was built to protect Chinese states."
    )
    .title("Great Wall Facts")
    .build();

// Custom content citations reference specific content blocks
Since:
1.1.0
Author:
Soby Chacko
See Also:
  • Method Details

    • builder

      public static CitationDocument.Builder builder()
    • toContentBlock

      public AnthropicApi.ContentBlock toContentBlock()
      Convert this CitationDocument to a ContentBlock for API usage.
      Returns:
      ContentBlock configured for citations
    • isCitationsEnabled

      public boolean isCitationsEnabled()