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}&format={fmt}Enqueue an async export for one target language. Returns 202 Accepted with export_state=Pending.
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.

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 format="pdf",
29 )
30 while True:
31 status = client.document_translation.get_export_status(
32 job_id=job_id,
33 export_id=export.export_id,
34 )
35 if status.export_state == "Completed":
36 urls[t.target_language_code] = status.download_url
37 break
38 if status.export_state == "Failed":
39 print(f"Export failed for {t.target_language_code}")
40 break
41 time.sleep(5)
42 return urls
43
44
45final = poll_job(job_id)
46downloads = export_completed(job_id, final.translations or [])
47print("Downloads:", downloads)

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