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

From Leeroopedia
Revision as of 12:18, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Cohere_ai_Cohere_python_ToolContent_Union.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains SDK, Function Calling
Last Updated 2026-02-15 14:00 GMT

Overview

ToolContent is a discriminated union type representing the content blocks within a tool result, supporting either plain text or document-based content.

Description

The ToolContent union defines the shape of content returned from a tool execution. It is discriminated on the type field and contains two variants:

  • TextToolContent (type="text") -- A content block containing a plain text string from the tool result. Contains a required text field.
  • DocumentToolContent (type="document") -- A content block containing a structured document from the tool result. Contains a required document field of type Document.

Both variants extend UncheckedBaseModel and allow extra fields. This type is used within V2 chat API tool result messages to provide rich, typed content back to the model.

Usage

Use ToolContent when constructing tool result messages for the V2 Chat API. Wrap your tool output as TextToolContent for simple text results, or DocumentToolContent when the tool returns structured document data that the model should be able to cite.

Code Reference

Source Location

Signature

class TextToolContent(UncheckedBaseModel):
    type: typing.Literal["text"] = "text"
    text: str

class DocumentToolContent(UncheckedBaseModel):
    type: typing.Literal["document"] = "document"
    document: Document

ToolContent = typing_extensions.Annotated[
    typing.Union[TextToolContent, DocumentToolContent],
    UnionMetadata(discriminant="type")
]

Import

from cohere.types import ToolContent, TextToolContent, DocumentToolContent

I/O Contract

TextToolContent Fields

Field Type Required Description
type Literal["text"] Yes (default: "text") Discriminant identifying this as a text content block.
text str Yes The plain text content from the tool result.

DocumentToolContent Fields

Field Type Required Description
type Literal["document"] Yes (default: "document") Discriminant identifying this as a document content block.
document Document Yes A structured document object from the tool result.

Usage Examples

from cohere.types import TextToolContent, DocumentToolContent

# Create text content from a tool result
text_result = TextToolContent(text="The temperature in London is 15 degrees Celsius.")

# Create document content from a tool result
doc_result = DocumentToolContent(
    document={
        "id": "weather_london",
        "data": {
            "city": "London",
            "temperature": 15,
            "unit": "celsius",
            "condition": "cloudy"
        }
    }
)

# Use in a V2 chat tool result message
response = client.v2.chat(
    model="command-r-plus",
    messages=[
        {"role": "user", "content": "What is the weather in London?"},
        {"role": "assistant", "tool_calls": [
            {"id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": '{"city": "London"}'}}
        ]},
        {"role": "tool", "tool_call_id": "call_1", "content": [text_result]}
    ]
)

Related Pages

Page Connections

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