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:Mistralai Client python ChatCompletionResponse

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

Overview

Concrete tool for extracting generated text and metadata from chat completion API responses provided by the mistralai SDK.

Description

The ChatCompletionResponse Pydantic model encapsulates the complete API response. It contains a list of ChatCompletionChoice objects (each with a message and finish reason), usage statistics (UsageInfo with prompt, completion, and total token counts), and metadata (response ID, model name, creation timestamp). The primary way to access the generated text is through response.choices[0].message.content.

Usage

Access the response object returned by client.chat.complete() or client.chat.complete_async(). Always check choices[0].finish_reason to handle truncation (length), tool calls (tool_calls), or normal completion (stop).

Code Reference

Source Location

  • Repository: client-python
  • File: src/mistralai/client/models/chatcompletionresponse.py (L1-32), chatcompletionchoice.py (L1-33)

Signature

class ChatCompletionResponse(BaseModel):
    id: str
    object: str
    created: int
    model: str
    choices: List[ChatCompletionChoice]
    usage: UsageInfo

class ChatCompletionChoice(BaseModel):
    index: int
    message: AssistantMessage
    finish_reason: Optional[ChatCompletionChoiceFinishReason] = None

class UsageInfo(BaseModel):
    prompt_tokens: int
    completion_tokens: int
    total_tokens: int

Import

from mistralai.models import ChatCompletionResponse
# Typically received as return value from client.chat.complete()

I/O Contract

Inputs

Name Type Required Description
response ChatCompletionResponse Yes Return value from chat.complete() or chat.complete_async()

Outputs

Name Type Description
content str Generated text from choices[0].message.content
finish_reason ChatCompletionChoiceFinishReason "stop", "length", or "tool_calls"
usage UsageInfo Token consumption (prompt_tokens, completion_tokens, total_tokens)
tool_calls Optional[List[ToolCall]] Tool calls if the model invoked functions

Usage Examples

Extract Response Text

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[UserMessage(content="Hello!")],
)

# Extract generated text
text = response.choices[0].message.content
print(text)

# Check finish reason
reason = response.choices[0].finish_reason
print(f"Finished because: {reason}")

# Access usage statistics
print(f"Tokens used: {response.usage.total_tokens}")
print(f"  Prompt: {response.usage.prompt_tokens}")
print(f"  Completion: {response.usage.completion_tokens}")

Related Pages

Implements Principle

Page Connections

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