Prompt
How do I send documents to an OCR API in batches?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To send documents to an OCR API in batches, you usually want to:
- Collect documents into a list
- Split them into smaller chunks (batch size)
- Send each batch to the API
- Handle retries, rate limits, and failures
- Combine the results
Basic approach
1) Group documents
If you have many files, organize them into batches of, say, 10–50 documents each depending on the API limits.
2) Send one batch per request
Many OCR APIs accept either:
- multiple files in one multipart request, or
- a JSON payload containing file URLs/base64 content, or
- one file per request but support parallelization
Check the API docs for:
- maximum files per request
- maximum file size
- supported file types
- async vs sync processing
3) Example workflow
import requests
from itertools import islice
def chunked(iterable, size):
it = iter(iterable)
while batch := list(islice(it, size)):
yield batch
documents = [
"doc1.pdf",
"doc2.pdf",
"doc3.pdf",
"doc4.pdf",
]
API_URL = "https://example.com/ocr"
for batch in chunked(documents, 2):
files = []
for path in batch:
files.append(("files", open(path, "rb")))
response = requests.post(API_URL, files=files)
response.raise_for_status()
print(response.json())
If the API uses file URLs instead of uploads
Some OCR services prefer you upload files to storage first, then send URLs:
payload = {
"documents": [
{"url": "https://storage.example.com/doc1.pdf"},
{"url": "https://storage.example.com/doc2.pdf"}
]
}
response = requests.post(API_URL, json=payload)
Best practices
- Respect rate limits: add delays or concurrency control.
- Use async jobs for large PDFs or many documents.
- Track results by document ID so responses map back correctly.
- Retry transient errors like 429/500 with exponential backoff.
- Validate inputs before sending.
If you’re using multiprocessing or async
You can send multiple batches in parallel, but cap concurrency:
- 2–5 concurrent requests for moderate APIs
- more only if the API explicitly allows it
Recommended pattern
- Pre-upload large files to object storage
- Send OCR API a batch of URLs
- Poll for completion if processing is asynchronous
If you want, I can show:
- a Python example with retries and batching
- a Node.js version
- or a version tailored to a specific OCR API like Google Vision, Azure OCR, AWS Textract, or OpenAI vision-based OCR.