Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Cohere ai Cohere python V2ChatResponse Model

From Leeroopedia
Metadata
Source Repo Cohere Python SDK
Domains NLP, Response_Parsing, Chat_API
Last Updated 2026-02-15 14:00 GMT

Overview

Concrete Pydantic model representing the structured response from Cohere V2 chat completions.

Description

V2ChatResponse is a Pydantic model (extending UncheckedBaseModel) returned by V2Client.chat(). It contains: id (unique response identifier), finish_reason (ChatFinishReason enum), message (AssistantMessageResponse with content, tool_calls, citations), usage (Optional[Usage] with token counts), and logprobs (Optional token probabilities).

Usage

Access after calling client.chat(). Extract generated text from response.message.content, check finish_reason for control flow, and use usage for billing tracking.

Code Reference

  • Source Location: Repository cohere-ai/cohere-python, File src/cohere/v2/types/v2chat_response.py, Lines L1-31
  • Signature:
class V2ChatResponse(UncheckedBaseModel):
    id: str
    finish_reason: ChatFinishReason
    message: AssistantMessageResponse
    usage: typing.Optional[Usage] = None
    logprobs: typing.Optional[typing.List[LogprobItem]] = None
  • Import: from cohere.v2.types import V2ChatResponse (typically accessed via client.chat() return value)

I/O Contract

Name Type Description
Inputs
Raw HTTP response JSON Raw HTTP JSON response from Cohere API (parsed automatically by SDK)
Outputs
id str Unique response identifier
finish_reason ChatFinishReason "COMPLETE", "MAX_TOKENS", "TOOL_CALL", etc.
message AssistantMessageResponse Contains content list, tool_calls, citations
usage Optional[Usage] Token counts with billed_units and tokens fields
logprobs Optional[List[LogprobItem]] Token log probabilities if requested

Usage Examples

response = client.chat(
    model="command-a-03-2025",
    messages=[UserChatMessageV2(content="Hello!")],
)

# Access response fields
print(response.id)
print(response.finish_reason)  # e.g., "COMPLETE"

# Get generated text
for content_block in response.message.content:
    print(content_block.text)

# Check usage
if response.usage:
    print(f"Input tokens: {response.usage.tokens.input_tokens}")
    print(f"Output tokens: {response.usage.tokens.output_tokens}")

Related Pages

Page Connections

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