Implementation:Langchain ai Langchain MistralAIEmbeddings
| Knowledge Sources | |
|---|---|
| Domains | Embeddings, MistralAI |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
MistralAIEmbeddings is a LangChain embedding model integration that generates text embeddings using the Mistral AI API.
Description
The MistralAIEmbeddings class, defined in the langchain-mistralai partner package, extends both BaseModel (Pydantic) and Embeddings (LangChain core). It generates vector embeddings by sending text to the Mistral AI /embeddings endpoint via httpx clients. The class implements intelligent batching using a tokenizer (the Mixtral-8x7B tokenizer from HuggingFace, or a dummy fallback) to ensure API requests stay within a 16,000-token limit per batch. It supports both synchronous and asynchronous embedding operations, with configurable retry logic using tenacity for handling timeouts and HTTP errors.
Usage
Import this class when you need to generate text embeddings using Mistral AI models for tasks such as semantic search, document similarity, or retrieval-augmented generation (RAG).
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/mistralai/langchain_mistralai/embeddings.py - Lines: 1-329
Signature
class MistralAIEmbeddings(BaseModel, Embeddings):
client: httpx.Client = Field(default=None)
async_client: httpx.AsyncClient = Field(default=None)
mistral_api_key: SecretStr = Field(alias="api_key", ...)
endpoint: str = "https://api.mistral.ai/v1/"
max_retries: int | None = 5
timeout: int = 120
wait_time: int | None = 30
max_concurrent_requests: int = 64
tokenizer: Tokenizer = Field(default=None)
model: str = "mistral-embed"
Import
from langchain_mistralai import MistralAIEmbeddings
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | No | Name of the Mistral AI embedding model to use. Defaults to "mistral-embed".
|
| mistral_api_key | SecretStr | No | API key for authentication. Read from MISTRAL_API_KEY env var if not provided. Alias: api_key.
|
| endpoint | str | No | Base URL for the Mistral API. Defaults to "https://api.mistral.ai/v1/".
|
| max_retries | int or None | No | Maximum number of retries on failure. Defaults to 5. Set to None to disable. |
| timeout | int | No | Request timeout in seconds. Defaults to 120. |
| wait_time | int or None | No | Seconds to wait before retrying on 429 errors. Defaults to 30. |
| max_concurrent_requests | int | No | Maximum concurrent API requests. Defaults to 64. |
| tokenizer | Tokenizer | No | HuggingFace tokenizer for batch size calculation. Auto-loaded from mistralai/Mixtral-8x7B-v0.1 or falls back to a dummy tokenizer.
|
Outputs
| Name | Type | Description |
|---|---|---|
| embed_documents | list[list[float]] | Returns a list of embedding vectors, one per input document. |
| embed_query | list[float] | Returns a single embedding vector for a query text. |
Usage Examples
Basic Usage
from langchain_mistralai import MistralAIEmbeddings
embed = MistralAIEmbeddings(
model="mistral-embed",
# api_key="...",
)
# Embed a single query
vector = embed.embed_query("The meaning of life is 42")
print(vector[:3])
# Embed multiple documents
vectors = embed.embed_documents(["Document 1...", "Document 2..."])
print(len(vectors))
Async Usage
from langchain_mistralai import MistralAIEmbeddings
embed = MistralAIEmbeddings(model="mistral-embed")
vector = await embed.aembed_query("The meaning of life is 42")
print(vector[:3])