st1vms/unofficial-claude-api

UnicodeDecodeError in .send_message

Randomboiii03 opened this issue · 2 comments

Error:

line 393, in send_message
    err = json.loads(response.content.decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 0: invalid start byte

My code:

def test(cookies):
    user_agent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36"

    data = SessionData(cookies, user_agent)

    client = ClaudeAPIClient(data)

    prompt = "Wassup"

    file_path_list = []

    chat_id = client.create_chat()

    if not chat_id:
        return "Message limit hit, cannot create chat. Please try again later..."

    else:
        print(chat_id)

    try:
        res: SendMessageResponse = client.send_message(
            chat_id, "hello", timeout=240
        )

        if res.answer:
            return res.answer
        else:
            client.delete_chat(chat_id)
            return f"Error code {res.status_code}, response -> {res.error_response}"

    except MessageRateLimitError as e:
        client.delete_chat(chat_id)
        return f"Message limit hit, resets at {e.resetDate}. {e.sleep_sec} seconds left until -> {e.resetTimestamp}"
st1vms commented

Hello, can you please try upgrading to 0.1.7 and see if that errors still appear?
pip install unofficial-claude2-api --upgrade

It should now filter bad utf8 characters out of error response, or return en empty dictionary, not throwing the UnicodeError.

Please notify me as soon as possible.

Thanks it works well, cheers!🎉