Implementation:Cohere ai Cohere python Document Model
| Knowledge Sources | |
|---|---|
| Domains | SDK, RAG |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Document is a Pydantic model representing a RAG (Retrieval-Augmented Generation) document that can be passed to Cohere models as grounding context for generating accurate, cited responses.
Description
The Document class models a document that provides relevant information for the Cohere Chat API's RAG capabilities. Each document consists of a data dictionary containing key-value pairs of contextual information, and an optional id field used for citation referencing. The content of each document should generally be kept under 300 words. Both the key names and values in the data dictionary are passed to the model, making metadata an effective way to provide structured context.
When id is not provided, the API will automatically generate a unique identifier for the document. The data field accepts a dictionary of type Dict[str, Any], allowing flexible content structures.
The class extends UncheckedBaseModel and is auto-generated by the Fern API definition toolchain. It supports both Pydantic v1 and v2 through a compatibility layer.
Usage
Use Document when providing grounding documents to the Cohere Chat API for retrieval-augmented generation. This is the standard way to supply external knowledge that the model can reference and cite in its responses.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/types/document.py
Signature
class Document(UncheckedBaseModel):
data: typing.Dict[str, typing.Any]
id: typing.Optional[str] = None
Import
from cohere.types import Document
I/O Contract
Fields
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
data |
Dict[str, Any] |
Yes | -- | A relevant document as a string-any dictionary. Both key names and values are passed to the model. |
id |
Optional[str] |
No | None |
Unique identifier for this document, referenced in citations. Auto-generated if not provided. |
Usage Examples
Basic Document Construction
from cohere.types import Document
# Create a document with explicit ID
doc = Document(
id="doc-1",
data={
"title": "Python Programming",
"text": "Python is a high-level programming language known for its readability.",
"source": "wikipedia",
},
)
print(doc.id) # "doc-1"
print(doc.data["title"]) # "Python Programming"
Using Documents in RAG Chat
import cohere
from cohere.types import Document
co = cohere.Client()
documents = [
Document(
id="annual-report",
data={
"title": "Q4 2025 Revenue Report",
"text": "Total revenue reached $50M in Q4 2025, a 25% increase YoY.",
},
),
Document(
data={
"title": "Company Overview",
"text": "The company was founded in 2020 and operates in 15 countries.",
},
),
]
response = co.chat(
model="command-r-plus",
message="What was the Q4 2025 revenue?",
documents=documents,
)
print(response.text)