Mistral-Common is a preprocessing library for Mistral's Large Language Models (LLMs). It encodes requests for Instruct, Transcription or Fill-In-The-Middle (FIM) tasks to tokens and optionally processed images or audios.
- Language: Python 3.10 to 3.14
- Package Manager: uv
- Testing: pytest
- Formatting and Linting: Ruff
- Type checker: mypy
- CI: GitHub Actions
mistral-common/
├── src/
│ └── mistral_common/
│ ├── guidance/
│ ├── integrations/
│ ├── protocol/
│ ├── tokens/
│ └── ...
├── scripts/
├── tests/
├── docs/
├── .github/
├── .pre-commit-config.yaml
├── pyproject.toml
└── README.md
audio.py: Audio processing utilities including Audio class and mel-scale conversionsbase.py: Base Pydantic model configurationdeprecation.py: Deprecation utilities (deprecated_import,warn_once) for emitting one-shot warnings on moved or removed symbolsexceptions.py: Custom exception classes for the libraryimage.py: Image processing utilities including download and serializationimports.py: Import utilities and dependency checksmultimodal.py: Multimodal processing utilities deprecated in favor toimage.py
src/mistral_common/protocol/: Protocol handlinginstruct/: Instruct protocolchunk.py: Chunks content used by messagesconverters.py: Converter helpers betweenChatCompletionRequestandOpenairequestsmessages.py: Instruct messages definitionnormalize.py: Normalizers forChatCompletionRequestrequest.py: Definition ofChatCompletionRequest. This is the entry point of user queries for Instruct requeststool_calls.py: Tool calling logicvalidator.py: Validators forChatCompletionRequest
fim/: Fill-in-the-middle protocolrequest.py: Definition ofFIMRequest. This is the entry point of user queries for FIM requests
transcription/: Transcription protocolrequest.py: Definition ofTranscriptionRequest. This is the entry point of user queries for Transcription requests
speech/: Speech protocolrequest.py: Definition ofSpeechRequest. This is the entry point of user queries for Speech requests
base.py: Definition ofBaseCompletionRequestsubclassed by FIM and Instruct requestsutils.py: Utility functions
src/mistral_common/tokens/: Tokenizationtokenizers/: Tokenizer implementationsaudio.py: Audio processingbase.py: Base Tokenizer implementationimage.py: Image processinginstruct.py: Instruct Tokenizer that encodes requests via Tekken tokenizermistral.py: Mistral Tokenizer that normalizes and validates requests to pass them to an instruct tokenizermodel_settings_builder.py: Builders (FieldBuilder,EnumBuilder,ModelSettingsBuilder) for validating and constructing model settingsmultimodal.py: deprecated in favor ofimage.pysentencepiece.py: Sentence Piece tokenizer (deprecated)tekken.py: Tekken tokenizer used by all recent modelsutils.py: Utility functions for the tokenizers
instruct/: deprecated in favor ofsrc/mistral_common/protocol/instruct/request.py
src/mistral_common/guidance/: Creates Lark grammars for tool calls, JSON schema and reasoning using llguidancegrammar_factory.py:GrammarFactorythat builds and renders Lark grammars from Jinja templatestokenizer.py: Adapts Tekken tokenizer for llguidancedata/: Jinja-templated Lark grammar files for base, thinking (special tokens) and thinking (plain text) modes
src/mistral_common/integrations/: Third-party framework integrationschat_templates/: Chat template generation for HuggingFace Transformerschat_templates.py: Public API for generating chat templates (generate_chat_template)template_generator.py: Core template generation engine withTemplateConfigandbuild_chat_template
scripts/generate_chat_template.py: CLI for generating and saving chat templates
src/mistral_common/experimental/: Experimental featuresutils.py: Utility functionstools.py: Tool calls parserthink.py: Thinking parserapp/: FastAPI applicationrouters.py: API routersmain.py: Application entry pointmodels.py: Pydantic models
src/mistral_common/data/: Data files for tokenizers
tests/: Test suitedocs/: Documentation.github/workflows/: CI/CD workflows.pre-commit-config.yaml: Pre-commit hookspyproject.toml: Project configuration
- Respect ruff and mypy rules
- Naming: snake_case for functions/variables, PascalCase for classes
- Use Python functionalities supported by Python 3.10
- Call function arguments explicitly by keyword, not implicitly by position (e.g.
fn(x=1, y=2), notfn(1, 2))
- Do NOT write comments that paraphrase or restate what the code already says.
- Comments should explain the "why" (intent, rationale, non-obvious constraints), not the "what".
- Use absolute imports for modules within the project
- Do NOT use wildcard imports
- Do NOT add import inside
__init__ - Use
TYPE_CHECKINGblocks for type-only imports - Do NOT use
from __future__ import annotations
- Use Python's type hints extensively
- Use modern (Python 3.10+) typing module types
- Use custom exceptions from
mistral_common.exceptions - Provide meaningful error messages
- Use Google-style docstrings
- One-liner for simple functions
- Multi-line with Args/Returns for complex ones
- Use
r"""for raw docstrings - Document all parameters and return values.
- Do NOT put types in the docstring for parameters and return values.
- For the returns sections, only describe the returned value and do not write its name
- Include examples where appropriate that can be tested via
doctest
- Aim for high, meaningful coverage. Prioritise tests that verify behaviour over hitting a line-count target.
- New and changed code should be covered by tests.
- Avoid coverage-only comments. Prefer restructuring so branches are genuinely reachable and tested (e.g. validate inputs and test the error path) over excluding lines.
- Set up using uv
uv sync --frozen --all-extras --group dev --python 3.12
source .venv/bin/activate
uv run pre-commit install- Make Changes
- Follow code style guidelines
- Write tests for new functionality
- Update documentation
- When adding dependencies, modify root
pyproject.toml, then runuv lockfollowed byuv sync --frozen - Backward Compatibility: Don't break existing functionality
- Run linter, formatter (Ruff), type checker (mypy) and tests (pytest), including doctests
- After adding your changes before committing ensure pre-commit is installed or run it manually.
- Use imperative grammar, start with a verb and be concise.