Class Citation

java.lang.Object
org.springframework.ai.anthropic.Citation

public final class Citation extends Object
Represents a citation reference in a Claude response. Citations indicate which parts of the provided documents were referenced when generating the response.

Citations are returned in the response metadata under the "citations" key and include:

  • The cited text from the document
  • The document index (which document was cited)
  • The document title (if provided)
  • Location information (character ranges, page numbers, or content block indices)

Citation Types

  • CHAR_LOCATION: For plain text documents, includes character start/end indices
  • PAGE_LOCATION: For PDF documents, includes page start/end numbers
  • CONTENT_BLOCK_LOCATION: For custom content documents, includes block start/end indices

Example Usage


 ChatResponse response = chatModel.call(prompt);

 List<Citation> citations = (List<Citation>) response.getMetadata().get("citations");

 for (Citation citation : citations) {
     System.out.println("Document: " + citation.getDocumentTitle());
     System.out.println("Location: " + citation.getLocationDescription());
     System.out.println("Text: " + citation.getCitedText());
 }
 
Since:
1.1.0
Author:
Soby Chacko
See Also:
  • Method Details

    • ofCharLocation

      public static Citation ofCharLocation(String citedText, int documentIndex, String documentTitle, int startCharIndex, int endCharIndex)
      Create a character location citation for plain text documents.
      Parameters:
      citedText - the text that was cited from the document
      documentIndex - the index of the document (0-based)
      documentTitle - the title of the document
      startCharIndex - the starting character index (0-based, inclusive)
      endCharIndex - the ending character index (exclusive)
      Returns:
      a new Citation with CHAR_LOCATION type
    • ofPageLocation

      public static Citation ofPageLocation(String citedText, int documentIndex, String documentTitle, int startPageNumber, int endPageNumber)
      Create a page location citation for PDF documents.
      Parameters:
      citedText - the text that was cited from the document
      documentIndex - the index of the document (0-based)
      documentTitle - the title of the document
      startPageNumber - the starting page number (1-based, inclusive)
      endPageNumber - the ending page number (exclusive)
      Returns:
      a new Citation with PAGE_LOCATION type
    • ofContentBlockLocation

      public static Citation ofContentBlockLocation(String citedText, int documentIndex, String documentTitle, int startBlockIndex, int endBlockIndex)
      Create a content block location citation for custom content documents.
      Parameters:
      citedText - the text that was cited from the document
      documentIndex - the index of the document (0-based)
      documentTitle - the title of the document
      startBlockIndex - the starting content block index (0-based, inclusive)
      endBlockIndex - the ending content block index (exclusive)
      Returns:
      a new Citation with CONTENT_BLOCK_LOCATION type
    • getType

      public Citation.LocationType getType()
    • getCitedText

      public String getCitedText()
    • getDocumentIndex

      public int getDocumentIndex()
    • getDocumentTitle

      public String getDocumentTitle()
    • getStartCharIndex

      public Integer getStartCharIndex()
    • getEndCharIndex

      public Integer getEndCharIndex()
    • getStartPageNumber

      public Integer getStartPageNumber()
    • getEndPageNumber

      public Integer getEndPageNumber()
    • getStartBlockIndex

      public Integer getStartBlockIndex()
    • getEndBlockIndex

      public Integer getEndBlockIndex()
    • getLocationDescription

      public String getLocationDescription()
      Get a human-readable location description.
    • toString

      public String toString()
      Overrides:
      toString in class Object