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 MessageEnd Stream Finalization

From Leeroopedia
Revision as of 12:17, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Cohere_ai_Cohere_python_MessageEnd_Stream_Finalization.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Field Value
Source Repo Cohere Python SDK
Domains Streaming, Data_Aggregation, Chat_API
Last Updated 2026-02-15 14:00 GMT

Overview

Concrete data models for the message-end stream event containing finish reason and usage statistics.

Description

MessageEndV2ChatStreamResponse is the final event in a chat stream (type="message-end"). It contains a delta field with ChatMessageEndEventDelta, which includes finish_reason (ChatFinishReason) and usage (Usage with billed_units and tokens). This event signals the end of generation and provides the same metadata available in a non-streaming V2ChatResponse.

Usage

Check for type=="message-end" in the stream event loop. Extract finish_reason to determine if further interaction is needed, and usage for billing and monitoring.

Code Reference

  • Source Location: Repository cohere-ai/cohere-python https://github.com/cohere-ai/cohere-python
    • File src/cohere/v2/types/v2chat_stream_response.py, Lines L199-214 (MessageEndV2ChatStreamResponse)
    • File src/cohere/types/chat_message_end_event_delta.py (ChatMessageEndEventDelta)
    • File src/cohere/types/usage.py (Usage)

Signature:

class MessageEndV2ChatStreamResponse:
    type: typing.Literal["message-end"] = "message-end"
    id: typing.Optional[str] = None
    delta: typing.Optional[ChatMessageEndEventDelta] = None

class ChatMessageEndEventDelta:
    finish_reason: typing.Optional[ChatFinishReason] = None
    usage: typing.Optional[Usage] = None

class Usage:
    billed_units: typing.Optional[UsageBilledUnits] = None
    tokens: typing.Optional[UsageTokens] = None

Import:

Types are accessed via stream event iteration (not imported directly).

I/O Contract

Inputs

Input Description
Final SSE event in the chat stream Event with type="message-end"

Outputs

Field Type Description
finish_reason Optional[ChatFinishReason] Why generation stopped: COMPLETE, MAX_TOKENS, or TOOL_CALL
usage Optional[Usage] Token counts with billed_units and tokens sub-objects

Usage sub-objects:

Class Fields Description
UsageBilledUnits input_tokens, output_tokens, search_units, classifications Billed unit counts
UsageTokens input_tokens, output_tokens Raw token counts

Usage Examples

from cohere import ClientV2, UserChatMessageV2

client = ClientV2()
full_text = ""

for event in client.chat_stream(
    model="command-a-03-2025",
    messages=[UserChatMessageV2(content="Tell me a story.")],
):
    if event.type == "content-delta":
        full_text += event.delta.message.content.text
    elif event.type == "message-end":
        finish_reason = event.delta.finish_reason
        usage = event.delta.usage
        print(f"Complete response: {full_text}")
        print(f"Finished: {finish_reason}")
        if usage and usage.tokens:
            print(f"Input tokens: {usage.tokens.input_tokens}")
            print(f"Output tokens: {usage.tokens.output_tokens}")

Related Pages

Page Connections

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