Soulter / hugging-chat-api

HuggingChat Python API🤗
GNU Affero General Public License v3.0
861 stars 123 forks source link

JSON response from API breaks python #194

Closed reflectored closed 5 months ago

reflectored commented 7 months ago

Describe the bug JSONDecodeError is hit while processing a particular response of the huggingface Chat API.

To Reproduce Run the following query, with web_search enabled (cherry-pick the fix for webSearch first #193, if not merged), and print the result:

>>> result = chatbot.chat('Go to the news, find 5 articles, and make a joke, write the title of the article used.', web_sea
rch=True)
>>> print(result)

Expected behavior I expect the query to return a result successfully.

Screenshots

>>> result = chatbot.chat('Go to the news, find 5 articles, and make a joke, write the title of the article used.', web_search=True)
>>> print(result)
Traceback (most recent call last):
  File "/home/folder/hugging-chat-api/src/hugchat/hugchat.py", line 770, in _stream_query
    obj = json.loads(res)
          ^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "/home/folder/hugging-chat-api/src/hugchat/hugchat.py", line 770, in _stream_query
    obj = json.loads(res)
          ^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/folder/hugging-chat-api/src/hugchat/message.py", line 228, in __str__
    return self.wait_until_done()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/folder/hugging-chat-api/src/hugchat/message.py", line 198, in wait_until_done
    self.__next__()
  File "/home/folder/hugging-chat-api/src/hugchat/message.py", line 153, in __next__
    raise self.error
  File "/home/folder/hugging-chat-api/src/hugchat/message.py", line 98, in __next__
    a: dict = next(self.g)
              ^^^^^^^^^^^^
  File "/home/folder/hugging-chat-api/src/hugchat/hugchat.py", line 790, in _stream_query
    raise exceptions.ChatError(f"Failed to parse response: {res}")
hugchat.exceptions.ChatError: Failed to parse response: <!DOCTYPE html>

Additional context What Operating System are you using? Ubuntu What Python version are you using? 3.11.2 What version of hugchat are you using? This branch reflectored:master

Soulter commented 7 months ago

It seems like a uncommon error from huggingchat and I cannot reproduce it, we throw the exception and let developers handle this error for now

github-actions[bot] commented 5 months ago

This issue was marked as stale because of inactivity.

github-actions[bot] commented 5 months ago

This issue was closed because of inactivity.