Skip to content

Conversation

TomeHirata
Copy link
Collaborator

@TomeHirata TomeHirata commented Aug 27, 2025

Add support for Anthropic Citation API through new types (dspy.Document, dspy.Citation). The design strategy is the same as dspy.Tool & dspy.ToolCalls.

Example

import dspy
from dspy.signatures import Signature

class AnswerWithSources(Signature):
    '''Answer questions using provided documents with citations.'''
    documents: list[dspy.Document] = dspy.InputField()
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()
    citations: dspy.Citations = dspy.OutputField()

# Create documents to provide as sources
docs = [
    dspy.Document(
        data="The Earth orbits the Sun in an elliptical path.",
        title="Basic Astronomy Facts"
    ),
    dspy.Document(
        data="Water boils at 100°C at standard atmospheric pressure.",
        title="Physics Fundamentals",
    )
]

# Use with a model that supports citations like Claude
lm = dspy.LM("anthropic/claude-opus-4-1-20250805")
predictor = dspy.Predict(AnswerWithSources, lm=lm)
result = predictor(documents=docs, question="What temperature does water boil?")

for citation in result.citations.citations:
    print(citation.format())

References

@TomeHirata TomeHirata requested a review from Copilot August 27, 2025 09:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for Anthropic's Citation API through two new types: dspy.Document for providing source content to language models and dspy.Citations for handling citation responses. The implementation enables citation-enabled responses from models like Claude, allowing developers to track which parts of the generated text reference specific source documents.

Key changes:

  • Added Document type for representing citable source content with metadata
  • Added Citations type for handling citation responses with character-level location information
  • Integrated citation extraction into the base language model processing pipeline

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
dspy/adapters/types/document.py New Document type with content, title, and metadata fields for citation-enabled content
dspy/adapters/types/citation.py New Citations type with nested Citation class for handling citation responses
dspy/clients/base_lm.py Added citation extraction from LiteLLM provider-specific fields
dspy/adapters/base.py Integrated citation processing into adapter postprocessing pipeline
dspy/adapters/__init__.py Exported new Citation and Document types
dspy/__init__.py Made Citation and Document types available at package level
tests/adapters/test_document.py Comprehensive tests for Document type validation and formatting
tests/adapters/test_citation.py Tests for Citation types, extraction, and adapter integration
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant