Skip to content

fix(models): Add compatibility shim for mistralai v2.x import path (#…#1927

Closed
shivamnegi1705 wants to merge 1 commit intostrands-agents:mainfrom
shivamnegi1705:fix/mistral-v2-import-compatibility
Closed

fix(models): Add compatibility shim for mistralai v2.x import path (#…#1927
shivamnegi1705 wants to merge 1 commit intostrands-agents:mainfrom
shivamnegi1705:fix/mistral-v2-import-compatibility

Conversation

@shivamnegi1705
Copy link

Description

mistralai v2.0 changed the import path for the Mistral client class from mistralai.Mistral to mistralai.client.Mistral. Since pyproject.toml specifies mistralai>=1.8.2 with no upper bound, fresh installs pull v2.0+ and fail at runtime with AttributeError: module 'mistralai' has no attribute 'Mistral' when stream() or structured_output() is called.

This PR adds a compatibility shim using importlib.import_module that tries the v2.x path first (mistralai.client.Mistral) and falls back to v1.x (mistralai.Mistral). All three mistralai.Mistral() call sites in stream() and structured_output() are replaced with the resolved MistralClient alias.

Related Issues

Closes #1924

Documentation PR

N/A — no documentation changes needed.

Type of Change

Bug fix

Testing

All 35 unit tests pass (33 existing + 2 new import shim tests).

Reproducing the issue

pip install 'strands-agents[mistral]'
# Pulls mistralai 2.0.4

Import succeeds but runtime usage fails:

python -c "from strands.models.mistral import MistralModel; print('OK')"
# OK
python -c "
import asyncio
from strands.models.mistral import MistralModel

m = MistralModel(model_id='mistral-small-latest')
messages = [{'role': 'user', 'content': [{'text': 'hi'}]}]

async def test():
    async for chunk in m.stream(messages):
        print(chunk)

asyncio.run(test())
"
# AttributeError: module 'mistralai' has no attribute 'Mistral'

After applying the fix

The same script with mistralai==2.0.4 no longer throws AttributeError. Instead it proceeds to the API call and succeeds.

Also verified with mistralai==1.12.4 (v1.x) — works as before with no regressions.

New unit tests

  • test_mistral_client_import_v2: verifies shim resolves MistralClient from mistralai.client.Mistral (v2.x path)

  • test_mistral_client_import_v1_fallback: verifies fallback to mistralai.Mistral when mistralai.client is unavailable (v1.x path)

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@mkmeral
Copy link
Contributor

mkmeral commented Mar 19, 2026

Hi @shivamnegi1705 , thank you for your PR.

However, I will close this PR in favor of #1935 We have not tested or gone through Mistral changes, so we cannot reliably say we support Mistral 2.0

We will release a patch change right now to make sure Mistral works as is (forces to v1, through upper bound). We can iterate on 2.0 client of MistralAI in another ticket

@mkmeral mkmeral closed this Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MistralModel breaks with mistralai SDK v2.0 (import path change)

2 participants