dspy.experimental.Citations¶
dspy.experimental.Citations
¶
Bases: Type
Citations extracted from an LM response with source references.
This type represents citations returned by language models that support citation extraction, particularly Anthropic's Citations API through LiteLLM. Citations include the quoted text and source information.
Example
import dspy
from dspy.signatures import Signature
from dspy.experimental import Citations, Document
class AnswerWithSources(Signature):
'''Answer questions using provided documents with citations.'''
documents: list[Document] = dspy.InputField()
question: str = dspy.InputField()
answer: str = dspy.OutputField()
citations: Citations = dspy.OutputField()
# Create documents to provide as sources
docs = [
Document(
data="The Earth orbits the Sun in an elliptical path.",
title="Basic Astronomy Facts"
),
Document(
data="Water boils at 100°C at standard atmospheric pressure.",
title="Physics Fundamentals",
metadata={"author": "Dr. Smith", "year": 2023}
)
]
# 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())
Functions¶
description() -> str
classmethod
¶
Description of the citations type for use in prompts.
Source code in dspy/adapters/types/citation.py
extract_custom_type_from_annotation(annotation)
classmethod
¶
Extract all custom types from the annotation.
This is used to extract all custom types from the annotation of a field, while the annotation can
have arbitrary level of nesting. For example, we detect Tool
is in list[dict[str, Tool]]
.
Source code in dspy/adapters/types/base_type.py
format() -> list[dict[str, Any]]
¶
from_dict_list(citations_dicts: list[dict[str, Any]]) -> Citations
classmethod
¶
Convert a list of dictionaries to a Citations instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
citations_dicts
|
list[dict[str, Any]]
|
A list of dictionaries, where each dictionary should have 'cited_text' key and 'document_index', 'start_char_index', 'end_char_index' keys. |
required |
Returns:
Type | Description |
---|---|
Citations
|
A Citations instance. |
Example
Source code in dspy/adapters/types/citation.py
serialize_model()
¶
validate_input(data: Any)
classmethod
¶
Source code in dspy/adapters/types/citation.py
:::