Hello Amol,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you are having error (string index out of range) in sending JSON Input to Azure Cognitive Services Translation API Using Azure Client Libraries.
Azure's client libraries require specific methods or parameters for sending translation requests, and the format might differ slightly from what you use with the REST API directly. So, the data you are sending should be a list of dictionaries where each dictionary contains the text to be translated and also, use the correct Method and Parameters. https://learn.microsoft.com/en-us/python/api/azure-ai-translation-text/azure.ai.translation.text.texttranslationclient . See code sample below:
from azure.ai.translation.text import TextTranslationClient
from azure.core.credentials import AzureKeyCredential
# Replace these variables with your actual values
endpoint = "https://<your-resource-name>.cognitiveservices.azure.com/"
api_key = "<your-api-key>"
# Initialize the client
client = TextTranslationClient(endpoint, AzureKeyCredential(api_key))
# Define the input data
data = [
{"text": "Good Morning"},
{"text": "Good night"}
]
# Translate the text
response = client.translate(
to=["de"], # target language(s)
text=data # the list of text objects
)
# Print the results
for translation in response:
for result in translation.translations:
print(f"Original text: {result.text}")
print(f"Translated text: {result.translated_text}")
Secondly, you will have to use the latest version of the Azure SDK installed. You can update it using pip: pip install --upgrade azure-ai-translation-text
NOTE: If you've done the above and you still have "string index out of range" error, check that:
- Your
endpoint
andapi_key
are correct. - You have network connectivity and the endpoint is reachable.
- The library version is compatible with the API you're using.
Accept Answer
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.
Best Regards,
Sina Salam