DeepL Python Library
The DeepL Python Library is an official client for the DeepL API, providing a convenient way for Python applications to interact with DeepL's language AI for high-quality text and document translation. It supports all major API functions and is actively maintained with frequent releases, currently at version 1.30.0.
Warnings
- breaking Dropped support for Python 3.8 and older versions.
- breaking DeepL API deprecation of GET requests to /translate endpoint and query parameter authentication for all endpoints. While the `deepl-python` library versions 1.0.0+ handle this internally, direct API users or those using older versions of the client library (prior to v1.0.0) might be affected.
- gotcha When using the `custom_instructions` parameter in `translate_text()`, the API will default to the `quality_optimized` model type. Combining `custom_instructions` with the `latency_optimized` model type will result in an error.
- gotcha The DeepL API uses different endpoints for Free and Pro accounts. Free users must specify `server_url="https://api-free.deepl.com"` when initializing `DeepLClient`.
- gotcha The generic 'EN' language code for target languages is deprecated. You must use a specific regional variant.
- gotcha The library implements retries with exponential backoff for HTTP 429 (too many requests) and 500 (internal server error) responses. However, HTTP 456 (quota exceeded) indicates your account's character limit has been reached and requires user action (upgrading plan or increasing cost control limits).
- gotcha In `v1.28.0`, the `NotFoundException` error message was improved by removing the misleading 'check server_url' suggestion. This implies that previously, users might have incorrectly tried to change `server_url` for 404 errors that were not related to endpoint configuration.
Install
-
pip install --upgrade deepl
Imports
- DeepLClient
import deepl deepL_client = deepl.DeepLClient(auth_key)
Quickstart
import os
import deepl
auth_key = os.environ.get("DEEPL_AUTH_KEY", "") # Replace with your key or set DEEPL_AUTH_KEY environment variable
if not auth_key:
print("Please set the DEEPL_AUTH_KEY environment variable.")
exit(1)
# For DeepL API Free, use server_url="https://api-free.deepl.com"
deepL_client = deepl.DeepLClient(auth_key)
text_to_translate = "Hello, world!"
target_language = "FR"
try:
result = deepL_client.translate_text(text_to_translate, target_lang=target_language)
print(f"Translated text: {result.text}")
print(f"Detected source language: {result.detected_source_lang}")
except deepl.exceptions.DeepLException as e:
print(f"Error during translation: {e}")