Microsoft Corporation Azure Ai Translation Text Client Library for Python
Project description
Azure Text Translation client library for Python
Azure text translation is a cloud-based REST API provided by the Azure Translator service. It utilizes neural machine translation technology to deliver precise, contextually relevant, and semantically accurate real-time text translations across all supported languages.
Use the Text Translation client library for Python to:
-
Retrieve the list of languages supported for translation and transliteration operations, as well as LLM models available for translations.
-
Perform deterministic text translation from a specified source language to a target language, with configurable parameters to ensure precision and maintain contextual integrity.
-
Execute transliteration by converting text from the original script to an alternative script representation.
-
Use LLM models to produce translation output variants that are tone-specific and gender-aware.
Source code | Package (PyPI) | API reference documentation | Product documentation | Samples
Getting started
Prerequisites
- Python 3.7 or later is required to use this package.
- An existing Translator service or Cognitive Services resource.
Install the package
Install the Azure Text Translation client library for Python with pip:
pip install azure-ai-translation-text
Create a Translator service resource
You can create Translator resource following Create a Translator resource.
Authenticate the client
Interaction with the service using the client library begins with creating an instance of the TextTranslationClient class. You will need an API key or TokenCredential to instantiate a client object. For more information regarding authenticating with cognitive services, see Authenticate requests to Translator Service.
Get an API key
You can get the endpoint, API key and Region from the Cognitive Services resource or Translator service resource information in the Azure Portal.
Alternatively, use the Azure CLI snippet below to get the API key from the Translator service resource.
az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>
Create a TextTranslationClient using an API key and Region credential
Once you have the value for the API key and Region, create an AzureKeyCredential. This will allow you to
update the API key without creating a new client.
With the value of the endpoint, credential and a region, you can create the TextTranslationClient:
credential = AzureKeyCredential(apikey)
text_translator = TextTranslationClient(credential=credential, region=region)
Key concepts
TextTranslationClient
A TextTranslationClient is the primary interface for developers using the Text Translation client library. It provides both synchronous and asynchronous operations to access a specific use of text translator, such as get supported languages detection or text translation.
Input
A TranslateInputItem is a single unit of input to be processed by the translation models in the Translator service. Each TranslateInputItem defines both the input string to translate and the output specifications for the translation.
For text element length limits, maximum requests size, and supported text encoding see here.
Examples
The following section provides several code snippets using the client created above, and covers the main features present in this client library. Although most of the snippets below make use of synchronous service calls, keep in mind that the Text Translation for Python library package supports both synchronous and asynchronous APIs.
Get Supported Languages
Gets the set of languages currently supported by other operations of the Translator.
try:
response = text_translator.get_supported_languages()
print(
f"Number of supported languages for translate operation: {len(response.translation) if response.translation is not None else 0}"
)
print(
f"Number of supported languages for transliterate operation: {len(response.transliteration) if response.transliteration is not None else 0}"
)
print(
f"Number of supported models for translation: {len(response.models) if response.models is not None else 0}"
)
if response.translation is not None:
print("Translation Languages:")
for key, value in response.translation.items():
print(f"{key} -- name: {value.name} ({value.native_name})")
if response.transliteration is not None:
print("Transliteration Languages:")
for key, value in response.transliteration.items():
print(f"{key} -- name: {value.name}, supported script count: {len(value.scripts)}")
if response.models is not None:
print(f"Models: {', '.join(response.models)}")
except HttpResponseError as exception:
if exception.error is not None:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
raise
For samples on using the languages endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of languages.
Translate
Renders single source-language text to multiple target-language texts with a single request.
try:
to_language = ["cs", "es", "de"]
input_text_elements = ["This is a test"]
response = text_translator.translate(body=input_text_elements, to_language=to_language)
translation = response[0] if response else None
if translation:
detected_language = translation.detected_language
if detected_language:
print(
f"Detected languages of the input text: {detected_language.language} with score: {detected_language.score}."
)
for translated_text in translation.translations:
print(
f"Text was translated to: '{translated_text.language}' and the result is: '{translated_text.text}'."
)
except HttpResponseError as exception:
if exception.error is not None:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
For samples on using the translate endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of translate.
Transliterate
Converts characters or letters of a source language to the corresponding characters or letters of a target language.
try:
language = "zh-Hans"
from_script = "Hans"
to_script = "Latn"
input_text_elements = ["这是个测试。"]
response = text_translator.transliterate(
body=input_text_elements,
language=language,
from_script=from_script,
to_script=to_script,
)
transliteration = response[0] if response else None
if transliteration:
print(
f"Input text was transliterated to '{transliteration.script}' script. Transliterated text: '{transliteration.text}'."
)
except HttpResponseError as exception:
if exception.error is not None:
print(f"Error Code: {exception.error.code}")
print(f"Message: {exception.error.message}")
raise
For samples on using the transliterate endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of transliterate.
Troubleshooting
When you interact with the Translator Service using the TextTranslator client library, errors returned by the Translator service correspond to the same HTTP status codes returned for REST API requests.
For example, if you submit a translation request without a target translate language, a 400 error is returned, indicating "Bad Request".
Provide Feedback
If you encounter any bugs or have suggestions, please file an issue in the Issues section of the project.
Next steps
More samples can be found under the samples directory.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Release History
2.0.0 (2026-06-06)
Features Added
- GA release of Azure AI Translator Text Translation SDK version 2.0.0.
- Updated to stable API version 2026-06-06.
- Added
TranslationToneandTranslationGenderenums for type-safe tone and gender options.
Breaking Changes
- Removed
gradeparameter from translation options.
Other Changes
- Simplified client constructor and internal authentication handling.
2.0.0b1 (2026-01-08)
Features Added
- Added support for the Azure AI Translator API 2025-10-01-preview, including translations using LLM models, adaptive custom translations, tone variant translations, and gender-specific language translations.
- Added
TranslationTargetfor configuring translation options.
Breaking Changes
- Dictionary, sentence boundaries and text alignments features have been removed, and relevant models and properties have been removed.
- Added
modelsproperty toGetSupportedLanguagesResultto include the list of LLM models available for translations. - Renamed property
confidencetoscoreinDetectedLanguage. - Removed property
source_textinTranslatedTextItem.
1.0.1 (2024-06-24)
Bugs Fixed
- Fixed a bug where Entra Id authentication couldn't be used with custom endpoint.
1.0.0 (2024-05-23)
Features Added
- Added support for Entra Id authentication.
Breaking Changes
- All calls to the client using parameter
contenthave been changed to use parameterbody. - Users can call methods using just a string type instead of complex objects.
get_languagesmethods were changed toget_supported_languages.- renamed
from_parametertofrom_language. - renamed
toparameter toto_language.
1.0.0b1 (2023-04-19)
- Initial Release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file azure_ai_translation_text-2.0.0.tar.gz.
File metadata
- Download URL: azure_ai_translation_text-2.0.0.tar.gz
- Upload date:
- Size: 103.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a467c87a7c7a2602447fbc882b7ca8a93471b2957ec15b9b6c2b76dbed7dcc72
|
|
| MD5 |
f406df2c8d2340d0baa0c5891087c7c1
|
|
| BLAKE2b-256 |
5910ea00242bd7483e8a34ae58557f422d3c35088a7b3315493f269903bbdbf7
|
File details
Details for the file azure_ai_translation_text-2.0.0-py3-none-any.whl.
File metadata
- Download URL: azure_ai_translation_text-2.0.0-py3-none-any.whl
- Upload date:
- Size: 105.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: RestSharp/106.13.0.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e07d0c90c11606b4f94f430b1ae75dc2f69e8fb4dfa06b666ec0a803db5c323f
|
|
| MD5 |
8554e3cfb9ceedbc270a6835c0bb42fe
|
|
| BLAKE2b-256 |
88c306ca8c3bb18cd666e4eadf05dcb5950ffce574068c60521366bacbbacc6b
|