Skip to content

fix(models): make the Mistral adapter work with mistralai 2.x and 3.x - #2073

Open
BlueX888 wants to merge 1 commit into
dottxt-ai:mainfrom
BlueX888:fix/prep-mistral-adapter-broken-against-mistralai-3
Open

BlueX888 wants to merge 1 commit into
dottxt-ai:mainfrom
BlueX888:fix/prep-mistral-adapter-broken-against-mistralai-3

Conversation

@BlueX888

Copy link
Copy Markdown

Summary

from_mistral and every MistralTypeAdapter.format_input path imported Mistral, UserMessage, AssistantMessage and SystemMessage from the mistralai top level. mistralai 2.0.0 removed those top-level exports: the package became a namespace package whose client lives at mistralai.client and whose message classes live at mistralai.client.models. Since the mistral extra is declared without a version bound (pyproject.toml:48), a fresh pip install "outlines[mistral]" resolves mistralai 3.2.0, and every entry point (from_mistral, format_input, generate, stream) fails with an ImportError before any request is sent.

Why

  • Root cause: src/outlines/models/mistral.py:81, :101, :120 and :545 import from the mistralai top level, which mistralai >= 2.0.0 no longer exports (checked against the 2.0.0, 2.10.1, 3.0.0 and 3.2.0 wheels: there is no mistralai/__init__.py; the client is at mistralai.client, the message classes at mistralai.client.models and SDKError at mistralai.client.errors).
  • The runtime API the adapter calls (client.chat.complete / chat.stream / chat.complete_async / chat.stream_async) is unchanged in 3.2.0, so only the import paths need to move.
  • The try-new-path/fallback-to-1.x pattern follows the existing convention in src/outlines/exceptions.py:298, which already resolves mistralai.client.errors with an ImportError fallback for older SDKs, so mistralai 1.x keeps working.

Fixes #2070

Changes

  • src/outlines/models/mistral.py: import the client from mistralai.client and the message classes from mistralai.client.models, keeping the top-level imports as a fallback for mistralai 1.x (the uv.lock test environment still pins 1.9.11).
  • tests/models/test_mistral.py, tests/models/test_mistral_type_adapter.py, tests/models/test_provider_exceptions.py: these imported from the removed surface themselves and failed at collection time on mistralai >= 2.0.0; updated with the same fallback.
  • tests/models/test_mistral_sdk_compat.py (new): regression tests covering the four entry points (format_input with str/list/Chat, and from_mistral) against whichever mistralai release is installed.

Testing

On the unpatched tree with mistralai 3.2.0 (what a fresh outlines[mistral] install resolves), the new tests fail with the reported error:

$ python -m pytest tests/models/test_mistral_sdk_compat.py -m 'not api_call' -q
E   ImportError: cannot import name 'UserMessage' from 'mistralai' (unknown location)
src/outlines/models/mistral.py:81: ImportError
...
4 failed in 0.36s

On the patched tree:

$ python -m pytest tests/models/test_mistral_sdk_compat.py -m 'not api_call' -q
4 passed in 2.58s                                                    # mistralai 3.2.0

$ python -m pytest tests/models/test_mistral.py tests/models/test_mistral_type_adapter.py tests/models/test_mistral_sdk_compat.py tests/models/test_provider_exceptions.py -m 'not api_call' -q
94 passed, 18 deselected in 1.55s                                    # mistralai 3.2.0
94 passed, 18 deselected in 7.40s                                    # mistralai 1.9.11 (versions from uv.lock)

$ pre-commit run --all-files
check for merge conflicts................................................Passed
debug statements (python)................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
mypy.....................................................................Passed
ruff.....................................................................Passed

The module suite passes both on mistralai 1.9.11 (the version pinned in uv.lock, used by CI) and on 3.2.0.

Checklist

  • We should be able to understand what the PR does from its title only;
  • There is a high-level description of the changes;
  • There is an issue discussing this already;
  • There are links to all the relevant issues, discussions and PRs;
  • The branch is rebased on the latest main commit;
  • Commit messages follow these [guidelines][git-guidelines];
  • One commit per logical change;
  • The code respects the current naming conventions;
  • Docstrings follow the [numpy style guide][docstring-guidelines];
  • pre-commit is installed and configured on your machine, and you ran it before opening the PR;
  • There are tests covering the changes;
  • The documentation is up-to-date;

mistralai 2.0.0 removed the top-level exports (`Mistral`, `UserMessage`,
...) that the adapter imported lazily, so every entry point (`from_mistral`,
`format_input`, `generate`, `stream`) crashed with ImportError on the
mistralai releases a fresh `outlines[mistral]` install resolves to. Import
the client from `mistralai.client` and the message classes from
`mistralai.client.models`, keeping the top-level imports as a fallback for
mistralai 1.x. The mistral test files imported from the removed surface as
well and failed at collection time, so update them too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant