Implementation:Cohere ai Cohere python Source Union
| 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 optionalidfor identifying the source and atool_outputdictionary with the raw tool output data. - DocumentSource (
type="document") -- Indicates the cited data originated from a document. Contains an optionalidfor identifying the source and adocumentdictionary 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
- Repository: Cohere Python SDK
- File:
src/cohere/types/source.py
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}")