dspy.experimental.Document¶
dspy.experimental.Document
¶
Bases: Type
A document type for providing content that can be cited by language models.
This type represents documents that can be passed to language models for citation-enabled responses, particularly useful with Anthropic's Citations API. Documents include the content and metadata that helps the LM understand and reference the source material.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
str
|
The text content of the document |
title |
str | None
|
Optional title for the document (used in citations) |
media_type |
Literal['text/plain', 'application/pdf']
|
MIME type of the document content (defaults to "text/plain") |
context |
str | None
|
Optional context information about the document |
Example
import dspy
from dspy.signatures import Signature
from dspy.experimental import Document, Citations
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
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",
)
]
# Use with a citation-supporting model
lm = dspy.LM("anthropic/claude-opus-4-1-20250805")
predictor = dspy.Predict(AnswerWithSources)
result = predictor(documents=docs, question="What temperature does water boil?", lm=lm)
print(result.citations)
Functions¶
adapt_to_native_lm_feature(signature: type[Signature], field_name: str, lm: LM, lm_kwargs: dict[str, Any]) -> type[Signature]
classmethod
¶
Adapt the custom type to the native LM feature if possible.
When the LM and configuration supports the related native LM feature, e.g., native tool calling, native
reasoning, etc., we adapt the signature and lm_kwargs to enable the native LM feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for the LM call. |
required |
field_name
|
str
|
The name of the field in the signature to adapt to the native LM feature. |
required |
lm
|
LM
|
The LM instance. |
required |
lm_kwargs
|
dict[str, Any]
|
The keyword arguments for the LM call, subject to in-place updates if adaptation if required. |
required |
Returns:
| Type | Description |
|---|---|
type[Signature]
|
The adapted signature. If the custom type is not natively supported by the LM, return the original |
type[Signature]
|
signature. |
Source code in dspy/adapters/types/base_type.py
description() -> str
classmethod
¶
Description of the document type for use in prompts.
Source code in dspy/adapters/types/document.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]]
¶
Format document for LM consumption.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list containing the document block in the format expected by citation-enabled language models. |
Source code in dspy/adapters/types/document.py
is_streamable() -> bool
classmethod
¶
parse_lm_response(response: str | dict[str, Any]) -> Optional[Type]
classmethod
¶
Parse a LM response into the custom type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
str | dict[str, Any]
|
A LM response. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Type]
|
A custom type object. |
Source code in dspy/adapters/types/base_type.py
parse_stream_chunk(chunk: ModelResponseStream) -> Optional[Type]
classmethod
¶
Parse a stream chunk into the custom type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
ModelResponseStream
|
A stream chunk. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Type]
|
A custom type object or None if the chunk is not for this custom type. |
Source code in dspy/adapters/types/base_type.py
serialize_model()
¶
Source code in dspy/adapters/types/base_type.py
validate_input(data: Any)
classmethod
¶
Source code in dspy/adapters/types/document.py
:::