Google Translate module randomly throws "the JSON object must be str, bytes or bytearray, not NoneType"

299 views Asked by At

I use Google Translate in Python and found this bug in my code. I copy here a minimal reproducible example:

import time

from googletrans import Translator as google_Translator

class Translator:
    lang: str
    def __init__(self, lang: str  = 'en'):
        self.lang = lang

    def go(self, text: str):
        try:
            translator = google_Translator()
        except Exception as e:
            print("First exception type: ", e)
            print("Argument: ", text)
            print("lang: ", self.lang)
            print("translator: ", translator)
            return None

        try:
            t1 = translator.translate(text, dest=self.lang)
        except Exception as e:
            print("Second exception type: ", e)
            print("Argument: '%s'" % text)
            print("lang: ", self.lang)
            print("translator: ", translator)
            return None

        try:
            t2 = t1.text
        except Exception as e:
            print("Third exception type: ", e)
            print("argument: ", text)
            print("lang: ", self.lang)
            print("translator: ", translator)
            return None

        return t2

if __name__ == '__main__':
    t = Translator('English')
    while True:
        print("\n---\n%s\n---\n" % t.go('Mieux Comprendre, Mieux Décider, Mieux Soigner'))
        time.sleep(5)

Sometimes the code works and translates the sentence, other times it does not. Here's sample output:

---
Better understand, decide better, better care
---


---
Better understand, decide better, better care
---


---
Better understand, decide better, better care
---


---
Better understand, decide better, better care
---

Second exception type:  the JSON object must be str, bytes or bytearray, not NoneType
Argument: 'Mieux Comprendre, Mieux Décider, Mieux Soigner'
lang:  English
translator:  <googletrans.client.Translator object at 0x109e96d40>

---
None
---


---
Better understand, decide better, better care
---

Here are my system specifications:

(venv)$ python3 --version
Python 3.10.11
(venv)$ python3 -m pip show googletrans
Name: googletrans
Version: 4.0.0rc1
Summary: Free Google Translate API for Python. Translates totally free of charge.
Home-page: https://github.com/ssut/py-googletrans
Author: SuHun Han
Author-email: [email protected]
License: MIT
Location: ~/venv/lib/python3.10/site-packages
Requires: httpx
Required-by: 

Why does it behave like this, and how to fix it?

update

The exception thrown is often TypeError: the JSON object must be str, bytes or bytearray, not NoneType. If the text only has spaces, it also sometimes throws IndexError: list index out of range. Here is the stacktrace for the latter.

TypeError from translator:  list index out of range
Text =  

Traceback (most recent call last):
  File "/Translator.py", line 22, in go
    return translator.translate(text, dest=self.lang).text
  File "/home/ubuntu/.local/lib/python3.10/site-packages/googletrans/client.py", line 222, in translate
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
  File "/home/ubuntu/.local/lib/python3.10/site-packages/googletrans/client.py", line 222, in <lambda>
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
IndexError: list index out of range

I'll add the stacktrace of the former when I see it.

0

There are 0 answers