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:Langchain ai Langchain ChatMistralAI

From Leeroopedia
Knowledge Sources
Domains LLM, Chat Model, MistralAI
Last Updated 2026-02-11 00:00 GMT

Overview

ChatMistralAI is a LangChain chat model integration that communicates with the Mistral AI API for conversational language model inference.

Description

The ChatMistralAI class, defined in the langchain-mistralai partner package, extends BaseChatModel from langchain-core. It provides synchronous and asynchronous chat completion via the Mistral AI REST API using httpx clients and server-sent events (SSE) for streaming. The class handles message conversion between LangChain message types and Mistral's expected format, supports tool calling with Mistral-compatible tool call ID formatting, structured output via function calling, JSON mode, and JSON schema methods, and includes built-in retry logic with tenacity. It also integrates model profiles from an auto-generated profiles registry for token limits and capability metadata.

Usage

Import this class when you need to use Mistral AI models (e.g., mistral-small, mistral-large-latest) for chat-based completions with support for tool calling, structured output, and streaming.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/mistralai/langchain_mistralai/chat_models.py
  • Lines: 1-1222

Signature

class ChatMistralAI(BaseChatModel):
    client: httpx.Client = Field(default=None, exclude=True)
    async_client: httpx.AsyncClient = Field(default=None, exclude=True)
    mistral_api_key: SecretStr | None = Field(alias="api_key", ...)
    endpoint: str | None = Field(default=None, alias="base_url")
    max_retries: int = 5
    timeout: int = 120
    max_concurrent_requests: int = 64
    model: str = Field(default="mistral-small", alias="model_name")
    temperature: float = 0.7
    max_tokens: int | None = None
    top_p: float = 1
    random_seed: int | None = None
    safe_mode: bool | None = None
    streaming: bool = False
    model_kwargs: dict[str, Any] = Field(default_factory=dict)

Import

from langchain_mistralai import ChatMistralAI

I/O Contract

Inputs

Name Type Required Description
model str No Mistral model name to use. Defaults to "mistral-small". Alias: model_name.
mistral_api_key SecretStr or None No API key for authentication. Read from MISTRAL_API_KEY env var if not provided. Alias: api_key.
endpoint str or None No Base URL for the Mistral API. Defaults to https://api.mistral.ai/v1. Alias: base_url.
temperature float No Sampling temperature in range [0.0, 1.0]. Defaults to 0.7.
max_tokens int or None No Maximum number of tokens to generate. None means no limit.
top_p float No Nucleus sampling parameter in [0.0, 1.0]. Defaults to 1.
random_seed int or None No Random seed for reproducible generation.
safe_mode bool or None No Whether to enable Mistral's safe mode for content filtering.
max_retries int No Maximum number of retries on request failure. Defaults to 5.
timeout int No Request timeout in seconds. Defaults to 120.
streaming bool No Whether to stream results. Defaults to False.
model_kwargs dict No Additional invocation parameters not explicitly specified.

Outputs

Name Type Description
ChatResult ChatResult Contains ChatGeneration objects with AIMessage responses including content, tool calls, and usage metadata.
ChatGenerationChunk Iterator[ChatGenerationChunk] When streaming, yields message chunks incrementally with tool call chunks and usage metadata.

Key Methods

bind_tools

Binds tool-like objects (Pydantic classes, functions, dicts) to the model using OpenAI-compatible tool format. Supports tool_choice to force a specific tool or use "auto"/"any".

with_structured_output

Returns a Runnable that produces structured output matching a given schema. Supports three methods:

  • "function_calling" -- uses Mistral's function/tool calling API
  • "json_mode" -- uses Mistral's JSON mode (requires schema instructions in prompt)
  • "json_schema" -- uses Mistral's structured output API with a JSON schema

Usage Examples

Basic Usage

from langchain_mistralai import ChatMistralAI

model = ChatMistralAI(model="mistral-large-latest", temperature=0)

response = model.invoke("What is the capital of France?")
print(response.content)

Structured Output

from langchain_mistralai import ChatMistralAI
from pydantic import BaseModel, Field


class AnswerWithJustification(BaseModel):
    """An answer to the user question along with justification."""
    answer: str
    justification: str | None = Field(
        default=None, description="A justification for the answer."
    )


model = ChatMistralAI(model="mistral-large-latest", temperature=0)
structured_model = model.with_structured_output(AnswerWithJustification)

result = structured_model.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
print(result)

Streaming

from langchain_mistralai import ChatMistralAI

model = ChatMistralAI(model="mistral-small", streaming=True)

for chunk in model.stream("Tell me a short joke"):
    print(chunk.content, end="")

Related Pages

Page Connections

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