Skip to navigation

Document AI API overview

View as Markdown

Sarvam’s Document AI API turns scanned and digital documents into structured, machine-readable data. It is powered by Sarvam Vision and supports 23 languages (22 Indian + English), so a Hindi invoice and an English contract go through the same API.

Pick an operation

Four operations share one job model. Pick based on what you need out the other end.

ParseExtractSplitClassify
You provideA documentA document + a schema (or config_id)A document (+ categories, optional)A document (+ categories, optional)
Fetch output withget_download_url() (ZIP) or get_results() (JSON blocks)get_results()get_results()get_results()
EndpointPOST /doc-ai/v1/job/parsePOST /doc-ai/v1/job/extractPOST /doc-ai/v1/job/splitPOST /doc-ai/v1/job/classify

Before you start

1

Get an API key

Create one on the Sarvam dashboard and export it as SARVAM_API_KEY.

export SARVAM_API_KEY="YOUR_SARVAM_API_KEY"
2

Install the SDK

pip install -U sarvamai

Prefer plain HTTP? Every how-to also has a cURL version. See the Quickstart for the full setup.

How a job works

Every operation follows the same three steps:

  1. Submit with parse(), extract(), split(), or classify(). One call creates the job and returns a job_id.
  2. Poll get_status() until the job finishes.
  3. Fetch the output with get_results() or get_download_url().

Job states, the status response, and error handling are covered in Jobs & Errors.

Your first call

This example parses one PDF end to end. Save any PDF as sample-document.pdf next to the script, or change the file name. No PDF handy? Use any document you have, find a sample invoice or form online, or ask an AI assistant to create one and save it as a PDF.

import os
import time
import urllib.request
import zipfile
from sarvamai import SarvamAI
client = SarvamAI(api_subscription_key=os.environ["SARVAM_API_KEY"])
# 1. Submit: one call creates the job
with open("sample-document.pdf", "rb") as f:
job = client.doc_ai.parse(
file=[("sample-document.pdf", f, "application/pdf")],
language="en-IN",
)
# 2. Poll until the job reaches a terminal state
while True:
st = client.doc_ai.get_status(job_id=job.job_id)
if st.status in {"completed", "partially_completed", "failed", "rejected"}:
break
time.sleep(5)
# 3. Fetch: download the ZIP and unpack it
if st.status in ("completed", "partially_completed"):
dl = client.doc_ai.get_download_url(job_id=job.job_id)
urllib.request.urlretrieve(dl.url, "output.zip")
with zipfile.ZipFile("output.zip") as z:
z.extractall("output")
print(z.namelist())
else:
print("job did not complete:", st.status)

Each how-to page below has the same flow with every option, plus a cURL version.

Combine operations

The operations are designed to chain.

GoalChain
Process a scanned bundleSplit the file, then classify each child document by its document_ref
Route by document typeClassify, then extract with the schema (or config_id) that matches the type
Index a document archiveParse to Markdown, then chunk and embed the output
Capture the same fields at scaleSave a schema as a config_id and reuse it across extract calls

Document AI supersedes the earlier Document Digitization API. The namespace is doc_ai, and each operation is a single call.

Moving from the legacy Document Digitization API

The legacy API (client.document_intelligence, /doc-digitization/job/v1) is deprecated. To move to Document AI:

Legacy callDocument AI
create_job()parse() (or extract()): creates and starts the job in one call
upload_file() (get upload links)Send the file with the same call. To upload separately, use POST /doc-ai/v1/job/upload and pass upload_ids
start()Not needed. The job starts when you submit it
wait_until_complete()Poll get_status() yourself
get_page_metrics()Page counts in the usage object of get_status(); per-page details are in the metadata/ files of an HTML or Markdown Parse ZIP, or inside the JSON file for JSON output
download_output() (get download links)get_download_url() for the ZIP, or get_results()

Next steps