DeepLcom / deepl-python

Official Python library for the DeepL language translation API.
MIT License
1.06k stars 75 forks source link

DeepL documentation states ZH is supported as target for translate_docs but on execution it errors #101

Closed raeudigerRaeffi closed 3 months ago

raeudigerRaeffi commented 3 months ago

When execution the following code:


with open(f".{file.split('.')[0]}.html", "rb") as in_file, open(interm_path, "wb") as out_file:
                        translator.translate_document(
                            in_file,
                            out_file,
                            target_lang="ZH",
                            formality="more"
                    )

it fails with

---------------------------------------------------------------------------
DeepLException                            Traceback (most recent call last)
Cell In[8], line 2
      1 with open(f".{file.split('.')[0]}.html", "rb") as in_file, open(interm_path, "wb") as out_file:
----> 2                         translator.translate_document(
      3                             in_file,
      4                             out_file,
      5                             target_lang="ZH",
      6                             formality="more"
      7                     )

File ~\anaconda3\Lib\site-packages\deepl\translator.py:604, in Translator.translate_document(self, input_document, output_document, source_lang, target_lang, formality, glossary, filename, output_format)
    565 def translate_document(
    566     self,
    567     input_document: Union[TextIO, BinaryIO, Any],
   (...)
    575     output_format: Optional[str] = None,
    576 ) -> DocumentStatus:
    577     """Upload document, translate it into the target language, and download
    578     result.
    579 
   (...)
    601         translation, the exception includes the document handle.
    602     """
--> 604     handle = self.translate_document_upload(
    605         input_document,
    606         target_lang=target_lang,
    607         source_lang=source_lang,
    608         formality=formality,
    609         glossary=glossary,
    610         filename=filename,
    611         output_format=output_format,
    612     )
    614     try:
    615         status = self.translate_document_wait_until_done(handle)

File ~\anaconda3\Lib\site-packages\deepl\translator.py:680, in Translator.translate_document_upload(self, input_document, source_lang, target_lang, formality, glossary, filename, output_format)
    676     files = {"file": input_document}
    677 status, content, json = self._api_call(
    678     "v2/document", data=request_data, files=files
    679 )
--> 680 self._raise_for_status(status, content, json)
    682 if not json:
    683     json = {}

File ~\anaconda3\Lib\site-packages\deepl\translator.py:203, in Translator._raise_for_status(self, status_code, content, json, glossary, downloading_document)
    198     raise DeepLException(
    199         f"Not found, check server_url{message}",
    200         http_status_code=status_code,
    201     )
    202 elif status_code == http.HTTPStatus.BAD_REQUEST:
--> 203     raise DeepLException(
    204         f"Bad request{message}", http_status_code=status_code
    205     )
    206 elif status_code == http.HTTPStatus.TOO_MANY_REQUESTS:
    207     raise TooManyRequestsException(
    208         "Too many requests, DeepL servers are currently experiencing "
    209         f"high load{message}",
    210         should_retry=True,
    211         http_status_code=status_code,
    212     )

DeepLException: Bad request, message: formality is not supported for given target_lang.

Even though the API documentation clearly states ZH as a supported language pair. I am encountering the same issue with ID EL TR

raeudigerRaeffi commented 3 months ago

I manged to make the code run by manually setting the formality to prefer_more so the issue is just that the default is not supported

daniel-jones-dev commented 3 months ago

Hi @raeudigerRaeffi, thanks for the detailed explanation.

Yes this error is caused because formality is not supported for some languages for example ZH; please review the documentation about this parameter. The prefer_* options are offered for this purpose; formality=default works too.