Hello Ubana, Siddharth,
Thank you for your question and for sharing your implementation details using the Azure AI Translator .NET SDK. The error message: "The format parameter is not valid."
typically occurs when the file format is not supported by the synchronous document translation API. While your code correctly sets the content type to "application/pdf", the issue might lie in the API’s current limitations indicating a mismatch between the content type and accepted formats for the translation operation.
Key Points to Address Your Scenario:
1. Azure AI Translator Supported Formats
- As of the most recent Azure documentation, the Document Translation service supports various file formats, including PDF. The typical content type for PDFs is application/pdf.
- However, the error you are facing stems not just from the MIME type but also from the format parameter required by the Document Translator.
- How to Specify the Format Parameter
- In most SDK and API scenarios, the format parameter is explicitly passed to indicate the document format (e.g., 'pdf', 'docx', etc.), not automatically derived just from the MIME/content type.
- If you are using the REST API, you'd set format=pdf in the request URL or body.
- In the .NET SDK, you typically do not have to set this manually if the input is correctly constructed. But some SDKs expect certain constructors or methods for PDFs.
- SingleDocumentTranslationClient and DocumentTranslateContent
- The SingleDocumentTranslationClient is a newer model, and from current SDK documentation, it expects you to wrap the PDF file as a supported DocumentTranslateContent.
- However, mistakes in content construction or the use of MultipartFormFileData might lead to format detection errors.
Recommended solutions:
- To translate PDF documents, you must use the asynchronous document translation API, which supports a broader range of formats including scanned and native PDFs. This process requires:
- Uploading the source PDF to an Azure Blob Storage container
- Creating a target container for the translated output
- Using the DocumentTranslationClient.BeginTranslationAsync( ) method with SAS URLs
Here’s a quick reference:
var client = new DocumentTranslationClient(new Uri(endpoint), new AzureKeyCredential(key));
var operation = await client.StartTranslationAsync(sourceUri, targetUri, targetLanguage);
await operation.WaitForCompletionAsync();
- Verify SDK Version and Documentation
- Ensure you are using the most up-to-date Azure.AI.Translation.Document SDK.
- Review the relevant Azure AI Translator .NET SDK documentation for method signatures and supported formats.
- Azure AI Translator's document translation API does support PDFs, but the .NET SDK expects the correct format to be explicitly declared during request construction. The DocumentTranslateContent class must receive both the file stream and a correctly specified format.
try
{
string filePath = @"C:\Users\siubana\Desktop\TestTranslatorDocument.pdf";
using Stream fileStream = System.IO.File.OpenRead(filePath);
// Provide the correct format explicitly as "pdf"
var sourceDocument = new MultipartFormFileData(
fileName: Path.GetFileName(filePath),
content: fileStream,
contentType: "application/pdf",
format: "pdf" // This line is critical
);
DocumentTranslateContent content = new DocumentTranslateContent(sourceDocument);
var response = await client.TranslateAsync("hi", content);
return Ok(new { translation = response.ToString() });
}
- General Troubleshooting
- Make sure your file stream is not empty, corrupted, or locked.
- Ensure that the translation region and service endpoint align with your subscription.
Reference Links:
- https://learn.microsoft.com/en-us/dotnet/api/azure.ai.translation.document?view=azure-dotnet
- https://learn.microsoft.com/en-us/azure/ai-services/translator/document-translation/overview#synchronous-supported-document-formats
- https://learn.microsoft.com/en-us/answers/questions/1661419/azure-translator-synchronous-document-translation
Happy to help !! 😊