Skip to content

[Bug] ModuleNotFoundError: No module named 'mistralai.models.chat_completion' #677

Description

@WahahaZeng

Describe the bug

Traceback (most recent call last):
File "/home/xyz/HarmBench/generate_completions.py", line 9, in
from api_models import api_models_map
File "/home/xyz/HarmBench/api_models.py", line 11, in
from mistralai.models.chat_completion import ChatMessage
ModuleNotFoundError: No module named 'mistralai.models.chat_completion'

The version is am using is the latest one
$ pip list |grep mistralai
mistralai 1.2.5

I also tried some old version like "mistralai==1.2.0", "mistralai==1.0.1", but the bug is still there

- OS: Linux
- Python version: 3.10.15

This bug was reported before in #640

Activity

  1. micedevai commented on Dec 27, 2024

    @micedevai

    The error you're encountering, ModuleNotFoundError: No module named 'mistralai.models.chat_completion', suggests that the module mistralai.models.chat_completion does not exist in the version of the mistralai library that you have installed (version 1.2.5 or any older version). Here are several steps you can take to address this:

    Steps to Fix:

    1. Check the Available Modules in the Installed Version of Mistralai

    It's possible that the structure of the mistralai library has changed in recent versions. To check the available modules, navigate to the installation directory of mistralai and inspect its contents.

    • To find the installation location of mistralai, run the following:

      pip show mistralai

      This will provide the path to the mistralai package.

    • Once you have the path, navigate to it:

      cd <path_to_mistralai>/mistralai/models

      Look for the chat_completion.py file. If it doesn't exist, it confirms that the module has either been removed or renamed.

    2. Consult Mistralai Documentation/Changelog

    • Check the Documentation: Mistralai may have changed its module structure in recent versions, which could explain why the chat_completion module is missing.

    • Check the GitHub Repo for Changelog: Review the [Mistralai GitHub repo](https://github.com/mistralai/mistralai) to see if there have been any breaking changes or deprecations related to the chat_completion module in the changelog.

    3. Use the Correct Version

    Since you've already tried several versions, it's important to figure out if the chat_completion module was present in earlier versions. It's possible that the module was either removed or relocated in a recent release.

    • Try Downgrading to a Specific Version:
      If the module was present in a previous version, try to find which version it existed in by checking the release notes. You can also search for older versions of the package and install them with:
      pip install mistralai==<specific_version>

    For example:

    pip install mistralai==1.1.0

    4. Explore an Alternative Approach

    If you cannot find the chat_completion module or if it has been deprecated, you may need to modify your code to work with the available modules in the mistralai package. You can look for equivalent functionality under other module paths or newer classes.

    • Search for ChatMessage in the Codebase: If you're trying to use the ChatMessage class, it might have been moved to another module or replaced by a different class in newer versions of the library.
      You can search for ChatMessage in the installed mistralai package:
      grep -r "ChatMessage" <path_to_mistralai>
      This will show where ChatMessage is defined in the current package.

    5. Use a Virtual Environment to Isolate Dependencies

    If there are potential conflicts with other libraries or previous installations of mistralai, try installing it in a clean Python virtual environment to isolate dependencies and test again:

    • Create a new virtual environment:

      python -m venv myenv
      source myenv/bin/activate  # On Linux/Mac
    • Install mistralai in the new environment:

      pip install mistralai
    • Then test again if the error persists in the isolated environment.

    6. Check for Open Issues or Report Your Own

    Since you've mentioned that this bug was reported before in issue [#640](https://github.com/mistralai/mistralai/issues/640), it's possible that the maintainers are already aware of the issue, and there may be a fix or workaround suggested in the comments. Make sure to check that issue for any updates.

    If there is no solution yet, consider posting your own detailed bug report on GitHub:

    Let me know how these steps go, and if you need more assistance, feel free to ask!

  2. WahahaZeng commented on Jan 9, 2025

    @WahahaZeng
    Author

    @micedevai Thank you so much for the help.

    I found this article (https://github.com/mistralai/client-python/blob/d14559af390123cbd2d923e1e05f780500ce5411/MIGRATION.md?plain=1#L78) and it solves my problem.

    Based on this article, chat_completion.py is deprecated.

  3. micedevai commented on Jan 10, 2025

    @micedevai

    @WahahaZeng, You're welcome! I'm glad the article helped solve your problem. It’s good to know that the chat_completion.py is deprecated according to the article. If you need further assistance with any updates or migrating your code, feel free to reach out.

  4. freedomsha commented on Mar 18, 2025

    @freedomsha

    J'ai changé mistral_client.py comme ceci :
    `# from mistralai.client import MistralClient

    from mistralai.models.chat_completion import ChatMessage

    from mistralai import Mistral, UserMessage
    from src.config import Config

    class MistralAi:
    def init(self):
    config = Config()
    api_key = config.get_mistral_api_key()
    # self.client = MistralClient(api_key=api_key)
    self.client = Mistral(api_key=api_key)

    def inference(self, model_id: str, prompt: str) -> str:
        print("prompt", prompt.strip())
        # chat_completion = self.client.chat(
        #     model=model_id,
        #     messages=[
        #         ChatMessage(role="user", content=prompt.strip())
        #     ],
        #     temperature=0
        # )
        chat_completion = self.client.chat(
            model=model_id,
            messages = [
                {
                    "role": "user",
                    "content": prompt.strip(),
                },
            ],
            temperature=0
        )
        return chat_completion.choices[0].message.content
    

    `

  5. ramdhakne commented on Mar 26, 2025

    @ramdhakne

    I ran into the same issue, the py client needs to be on version 0.4.2, chat completion works on that.
    eg:

    from mistralai.client import MistralClient
    from mistralai.models.chat_completion import ChatMessage
    
    # works only on mistralai==0.4.2 
    # pip install mistralai==0.4.2 
    
    api_key = "XYqvvvvvvyyyyyyyyyy6666666666u1HzNO"
    client = MistralClient(api_key=api_key)
    
    messages = [
        ChatMessage(role="system", content="You are an AI assistant."),
        ChatMessage(role="user", content="What is the capital of Japan?")
    ]
    
    response = client.chat(model="mistral-large-latest", messages=messages)
    print(response.choices[0].message.content)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions