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 Source Union

From Leeroopedia
Knowledge Sources
Domains SDK, Citations
Last Updated 2026-02-15 14:00 GMT

Overview

Source is a discriminated union type representing the origin of cited data in Cohere chat responses, distinguishing between tool-sourced and document-sourced citations.

Description

The Source union provides source attribution metadata for citations generated by the Cohere chat API. It is discriminated on the type field and contains two variants:

  • ToolSource (type="tool") -- Indicates the cited data originated from a tool result. Contains an optional id for identifying the source and a tool_output dictionary with the raw tool output data.
  • DocumentSource (type="document") -- Indicates the cited data originated from a document. Contains an optional id for identifying the source and a document dictionary with the raw document data.

Both variants extend UncheckedBaseModel and allow extra fields.

Usage

Use Source when processing citation objects from chat responses. The type discriminant allows you to determine whether a citation came from a tool call result or from a provided document, enabling appropriate attribution in your application.

Code Reference

Source Location

Signature

class ToolSource(UncheckedBaseModel):
    type: typing.Literal["tool"] = "tool"
    id: typing.Optional[str] = None
    tool_output: typing.Optional[typing.Dict[str, typing.Any]] = None

class DocumentSource(UncheckedBaseModel):
    type: typing.Literal["document"] = "document"
    id: typing.Optional[str] = None
    document: typing.Optional[typing.Dict[str, typing.Any]] = None

Source = typing_extensions.Annotated[
    typing.Union[ToolSource, DocumentSource],
    UnionMetadata(discriminant="type")
]

Import

from cohere.types import Source, ToolSource, DocumentSource

I/O Contract

ToolSource Fields

Field Type Required Description
type Literal["tool"] Yes (default: "tool") Discriminant identifying this as a tool source.
id Optional[str] No Identifier for the tool source.
tool_output Optional[Dict[str, Any]] No The raw output data from the tool that was cited.

DocumentSource Fields

Field Type Required Description
type Literal["document"] Yes (default: "document") Discriminant identifying this as a document source.
id Optional[str] No Identifier for the document source.
document Optional[Dict[str, Any]] No The raw document data that was cited.

Usage Examples

from cohere.types import ToolSource, DocumentSource

# Processing sources from a chat response with citations
response = client.chat(
    message="What is the capital of France?",
    documents=[{"title": "Geography", "text": "The capital of France is Paris."}]
)

if response.citations:
    for citation in response.citations:
        for source in citation.sources:
            if source.type == "document":
                print(f"Document source ID: {source.id}")
                print(f"Document data: {source.document}")
            elif source.type == "tool":
                print(f"Tool source ID: {source.id}")
                print(f"Tool output: {source.tool_output}")

Related Pages

Page Connections

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