How to Send JSON Input to Azure Cognitive Services Translation API Using Azure Client Libraries?

Amol 0 Reputation points
2024-08-23T11:38:37.33+00:00

I'm using Azure Cognitive Services for text translation and need to send data in the following JSON format:

data = [{"text":"Good Morning"},{"text":"Good night"}]

When I use the Azure REST API directly, I can successfully send this input, and it works as expected. However, when I try to send the same data using the Azure client libraries, I'm unable to do so.

Is there a way to send this specific JSON input using Azure client libraries for translation? If so, what would be the correct method to achieve this?

#This is my code.

from azure.ai.translation.text import TextTranslationClient

client = TextTranslationClient(endpoint, credential) result = client.translate(to=["de"], input=data)

when i execute this it gives me error - string index out of range

Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
411 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 12,011 Reputation points
    2024-08-23T12:23:27.38+00:00

    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 and api_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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.