Skip to content

Commit 30fb0f4

Browse files
hive-botclaude
andcommitted
docs: document the document translation changes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 11ab183 commit 30fb0f4

2 files changed

Lines changed: 59 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7070
- `translate_text()` accepts `source_lang` and `target_lang` positionally after
7171
the text, for example `translate_text("Hello", None, "DE")`. Keyword use is
7272
unchanged; omitting `target_lang` still raises `TypeError`.
73+
- Added the `document_id` property to `DocumentStatus`, read from the status
74+
response of `translate_document_get_status()` (falling back to the handle's
75+
document ID).
76+
- `translate_document()`, `translate_document_from_filepath()` and
77+
`translate_document_upload()` now also accept `source_lang` and
78+
`target_lang` as positional arguments after the input (and output)
79+
arguments, for example `translate_document_upload(file, "EN", "DE")`.
80+
Keyword use is unchanged.
7381

7482
### Changed
7583
- HTTP 404 responses raise `NotFoundException` instead of `DeepLException`
@@ -89,6 +97,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8997
`download_translation_memory_export()` is now 64 KiB instead of 1 byte, so
9098
downloads into `output_file` no longer write one byte per call. The written
9199
content is unchanged; an explicit `chunk_size` still applies.
100+
- `DocumentStatus.status` is now typed `Union[DocumentStatus.Status, str]`: a
101+
status value the API returns that this version does not know is kept as a
102+
string instead of raising `ValueError`. Type-checked code that reads
103+
`status.status.value` may need an `isinstance(..., DocumentStatus.Status)`
104+
guard; `str(status)` returns the status string in both cases.
105+
- `translate_document_wait_until_done()` now raises `DeepLException` with the
106+
API's error message when the document translation fails, instead of
107+
returning the `DocumentStatus` with status `error`.
108+
- `translate_document()` and `translate_document_from_filepath()` now also
109+
wrap upload failures in `DocumentTranslationException`, with
110+
`document_handle` set to `None` and the original exception as `__cause__`.
111+
Invalid arguments still raise `ValueError` or `TypeError`.
92112

93113
### Deprecated
94114
- `Translator` is deprecated in favour of `DeepLClient`, which supports all its
@@ -140,6 +160,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
140160
`None` in that case.
141161
- Fixed `translate_text()` raising `IndexError` when the response holds fewer
142162
translations than texts sent. It now raises `DeepLException`.
163+
- Fixed `translate_document_get_status()` raising `ValueError` and
164+
`translate_document_wait_until_done()` stopping when the API returns a
165+
document status this version does not know. `DocumentStatus.status` keeps
166+
such a status as a string, and polling continues.
167+
- Fixed `translate_document()` and `translate_document_from_filepath()`
168+
sending `formality=default` when `formality` was not set. It is now sent
169+
only when given.
170+
- Fixed `translate_document_get_status()` and `translate_document_download()`
171+
not URL-encoding the document ID in the request path.
172+
- Fixed `translate_document_upload()` returning an empty `DocumentHandle` when
173+
the response lacks `document_id` or `document_key`, and raising
174+
`AttributeError` when the response body is not a JSON object. Both now
175+
raise `DeepLException`.
176+
- Fixed the type annotation of `DocumentStatus.error_message`, which is a
177+
string, not an integer.
143178
- Fixed the `DeprecationWarning` messages of `Usage.Detail.limit_exceeded` and
144179
`Usage.any_limit_exceeded`, which named the replacement instead of the
145180
deprecated property. They now read "limit_exceeded is deprecated, use

‎README.md‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ functions, the first and second arguments correspond to the input and output
310310
files respectively.
311311

312312
Just as for the `translate_text()` function, the `source_lang` and
313-
`target_lang` arguments specify the source and target language codes.
313+
`target_lang` arguments specify the source and target language codes. They
314+
may be passed as keywords or positionally after the input and output files,
315+
for example `translate_document_from_filepath(input_path, output_path, "EN",
316+
"DE")`.
314317

315318
There are additional optional arguments to control translation, see
316319
[Document translation options](#document-translation-options) below.
@@ -338,16 +341,17 @@ try:
338341
)
339342

340343
except deepl.DocumentTranslationException as error:
341-
# If an error occurs during document translation after the document was
342-
# already uploaded, a DocumentTranslationException is raised. The
343-
# document_handle property contains the document handle that may be used to
344-
# later retrieve the document from the server, or contact DeepL support.
345-
doc_id = error.document_handle.id
346-
doc_key = error.document_handle.key
347-
print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}")
348-
except deepl.DeepLException as error:
349-
# Errors during upload raise a DeepLException
350-
print(error)
344+
# If an error occurs during document translation, a
345+
# DocumentTranslationException is raised. If the document was already
346+
# uploaded, the document_handle property contains the document handle that
347+
# may be used to later retrieve the document from the server, or contact
348+
# DeepL support. If the upload itself failed, document_handle is None.
349+
if error.document_handle is not None:
350+
doc_id = error.document_handle.document_id
351+
doc_key = error.document_handle.document_key
352+
print(f"Error after uploading {error}, id: {doc_id} key: {doc_key}")
353+
else:
354+
print(f"Error during upload: {error}")
351355
```
352356

353357
`translate_document()` and `translate_document_from_filepath()` are convenience
@@ -360,6 +364,15 @@ these steps individually, you can instead use the following functions directly:
360364
`translate_document_wait_until_done()`), and
361365
- `translate_document_download()`
362366

367+
`translate_document_get_status()` returns a `DocumentStatus` with the
368+
`document_id`, `status`, `seconds_remaining`, `billed_characters` and
369+
`error_message` of the document, and the `done` (translation succeeded) and
370+
`ok` (translation has not failed) properties. A status value this library
371+
version does not know is kept as a string in `status`.
372+
`translate_document_wait_until_done()` polls the status until the translation
373+
is done and returns the final `DocumentStatus`; if the translation fails it
374+
raises a `DeepLException` with the API's error message.
375+
363376
`translate_document_download()` writes the translated document to the
364377
file-like object given as `output_file`, in chunks of `chunk_size` bytes
365378
(default 64 KiB). Calling it without `output_file` returns the streaming

0 commit comments

Comments
 (0)