Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with destructor. #48

Open
vyastreb opened this issue Jan 31, 2024 · 0 comments
Open

Problems with destructor. #48

vyastreb opened this issue Jan 31, 2024 · 0 comments

Comments

@vyastreb
Copy link

There's a problem when the Mistral client is deleted.

Minimal example:

from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
import os
# Retrieve API key
try:
    api_key = os.environ["MISTRAL_API_KEY"]
except KeyError:
    print("Error: MISTRAL_API_KEY environment variable is not set.")
    exit()

client = MistralClient(api_key=api_key)

chat_response = client.chat(
    model="mistral-tiny",
    messages=[ChatMessage(role="user", content="How many fingers do you have?")],
    temperature=0.5, 
    max_tokens=10,
)
bot_response = chat_response.choices[0].message.content
print(bot_response)

Output

$ python3.10 minimalExample.py 
I don't have fingers, I'm
Exception ignored in: <function MistralClient.__del__ at 0x7f79bf7439a0>
Traceback (most recent call last):
  File ~/.local/lib/python3.10/site-packages/mistralai/client.py", line 49, in __del__
  File ~/.local/lib/python3.10/site-packages/httpx/_client.py", line 1272, in close
  File ~/.local/lib/python3.10/site-packages/httpx/_transports/default.py", line 243, in close
  File ~/.local/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 324, in close
  File ~/.local/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 172, in close
TypeError: 'NoneType' object is not callable

Solution suggestions: in ./mistralai/client.py.
A temporary solution, e.g. replace

    def __del__(self) -> None:
        self._client.close()

by

    def __del__(self) -> None:
        if self._client and getattr(self._client, 'close', None):
            try:
                self._client.close()
            except Exception as e:
                pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant