How to poll status and export results

View as Markdown

live-status reports one overall job_state plus a state per language in translations. Export is per language: call it once for each target language, only after that language’s own state is Completed.

EndpointPurpose
GET /translate/document/jobs/{job_id}/live-statusOverall progress + per-language state. Poll every 10-15 seconds.
POST /translate/document/jobs/{job_id}/export?lang={code}Enqueue an async export for one target language. Returns 202 Accepted with export_state=Pending. Exports in the uploaded file’s format by default.
GET /translate/document/jobs/{job_id}/export/statusPoll export status until export_state is Completed, then download from download_url.

live-status response shapes differ by file type, but the poll/export logic below works for both:

Upload typeProgress tracking
PDFPer page (page_metrics)
Word, spreadsheet, presentationPer segment (translation_metrics)

job_state, translations[].state, and translations[].target_language_code are present in both shapes. See Overview for full field lists.

Check each language’s own state, not only top-level job_state.

A job can stay Running or PartiallyCompleted while some languages in translations[] are already Completed and ready to export. Gate your export loop on translations[].state, not job_state alone.

format is optional. When omitted, the export uses the uploaded file’s native format. PDF uploads may optionally set format to pdf, docx, html, txt, or epub. Microsoft Office uploads always export in the original format only.

Example

Replace YOUR_SARVAM_API_KEY with your key from Key Management. This example assumes you already created a job, uploaded the file, and called start — see the Overview quickstart.

1import os
2import time
3
4from sarvamai import SarvamAI
5
6client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
7job_id = "550e8400-e29b-41d4-a716-446655440000"
8TERMINAL = {"Completed", "PartiallyCompleted", "Failed"}
9
10
11def poll_job(job_id):
12 while True:
13 status = client.document_translation.get_live_status(job_id=job_id)
14 print(f"job_state={status.job_state} progress={status.progress}%")
15 if status.job_state in TERMINAL:
16 return status
17 time.sleep(12)
18
19
20def export_completed(job_id, translations):
21 urls = {}
22 for t in translations:
23 if t.state != "Completed":
24 continue
25 export = client.document_translation.trigger_export(
26 job_id=job_id,
27 lang=t.target_language_code,
28 )
29 while True:
30 status = client.document_translation.get_export_status(
31 job_id=job_id,
32 export_id=export.export_id,
33 )
34 if status.export_state == "Completed":
35 urls[t.target_language_code] = status.download_url
36 break
37 if status.export_state == "Failed":
38 print(f"Export failed for {t.target_language_code}")
39 break
40 time.sleep(5)
41 return urls
42
43
44final = poll_job(job_id)
45downloads = export_completed(job_id, final.translations or [])
46print("Downloads:", downloads)

The sample above covers polling through export. See the Overview for the complete five-step flow including create, upload, and start.