Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python Document Model

From Leeroopedia
Revision as of 12:17, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Cohere_ai_Cohere_python_Document_Model.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

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)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment