OCR
/v1/ocr extracts text, layout and tables from PDFs and images. The gateway serves one OCR request format and routes to OCR providers including Mistral, Azure Document Intelligence, Amazon Textract, Reducto and others behind the same request. Results come back as markdown per page and are priced per page from the price map, so document processing is budgeted and logged like every other call.
Scenario
An accounts team receives supplier invoices as PDFs and wants the line items in a spreadsheet. Their script sends each invoice to the gateway, which returns every page as markdown with the tables preserved, and the script parses the table rows. The team's key has a budget, each page has a known price, and the Logs page shows which invoices were processed.
Steps
-
Register an OCR model. In the Admin UI open Models + Endpoints (
/ui/models-and-endpoints), click Add Model, pick the Provider (for example Mistral), choose the OCR model, set the Public Model Name tomistral-ocrand set Mode to OCR - /ocr, then click Add Model.model_list:- model_name: mistral-ocrspanway_params:model: mistral/mistral-ocr-latestapi_key: os.environ/MISTRAL_API_KEYmodel_info:mode: ocr -
Create a key in Virtual Keys (
/ui/api-keys) and pick the model under Models. -
Send a document by URL.
curl -X POST http://localhost:4000/v1/ocr \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d '{"model": "mistral-ocr","document": {"type": "document_url", "document_url": "https://example.com/invoice.pdf"},"include_image_base64": false}'Or upload the file as multipart form data.
curl -X POST http://localhost:4000/v1/ocr \-H "Authorization: Bearer $SPANWAY_API_KEY" \-F "model=mistral-ocr" \-F "file=@invoice.pdf" -
Check the cost in Logs (
/ui/logs). The row shows the pages processed and the cost.
Expected result
{
"object": "ocr",
"model": "mistral-ocr",
"pages": [
{"index": 0, "markdown": "# Invoice 4411\n\n| Item | Qty | Price |\n| --- | --- | --- |\n| Gateway seats | 10 | 2,000.00 |", "images": [], "dimensions": {"dpi": 200, "height": 1650, "width": 1275}}
],
"usage_info": {"pages_processed": 1, "doc_size_bytes": 48213}
}
Each page comes back as markdown with tables preserved, plus extracted images when include_image_base64 is true.
Request fields
document.type can be document_url for PDFs or image_url for images, and the URL can be a data URL for inline content. pages selects specific pages. Providers that support it, such as Azure Document Intelligence, can return their own payload instead of the normalised one with the x-req-format: native header or "req_format": "native" in the body, with cost tracking unchanged.
In ingestion pipelines
The RAG ingest endpoint can run OCR on uploaded files before chunking and embedding them, by naming an OCR model in its ocr options. RAG ingest accepts documents up to 512 MB, see upload checks, while a direct /v1/ocr upload is capped at 50 MB.
Errors
Every error comes back in the OpenAI shape, {"error": {"message": ..., "type": ..., "param": ..., "code": ...}}.
| Status | Message | What it means | Fix |
|---|---|---|---|
| 400 | Empty request body. For file uploads, use multipart/form-data ... | The request had no body. | Send a JSON body or a multipart form. |
| 400 | Invalid JSON in request body: ... | The JSON body does not parse. | Fix the JSON. |
| 400 | Multipart OCR request must include a 'file' field with the document to process | The form has no file. | Add -F "file=@document.pdf". |
| 400 | Uploaded file is empty | The uploaded file has no bytes. | Upload the real file. |
| 400 | Failed to parse multipart form data: ... | The form is broken, often because Content-Type was set by hand. | Let curl set the header with -F. |
| 400 | document type 'file' is not supported through the JSON API. ... | A JSON body tried to name a server side file. | Upload the file as multipart form data. |
| 400 | reducto:// file IDs are not accepted through the proxy OCR API; ... | A provider file id was sent, which is not tied to your key. | Upload the file in the same request. |
| 400 | Invalid model name passed in model=... | model is missing or unknown. | Send a model your key may use. |
| 400 | provider message | The URL could not be fetched by the provider, or the file is not a PDF or image. | Make the URL reachable from the provider, or upload the file. |
| 413 | OCR file exceeds the size limit of 50 MB | OCR uploads are capped at 50 MB, whatever max_file_size_mb says. | Split the document. |
| 401 | Authentication Error, ... | The key is missing, wrong or expired. | Use a live key. |
| 403 | model access message | The key may not use the model. | Add the model to the key. |
| 422 | Budget has been exceeded! ... | A budget is used up. The status follows budget_exceeded_status_code. | Raise the budget or wait for its reset. |
| 429 | RateLimitError | A rate limit was reached. | Slow down or raise the limit. |
Related
- RAG ingest for OCR inside an ingestion pipeline.
- Managed files for uploading documents once.
- Custom pricing for per-page prices.
Providers
The gateway's endpoint support matrix marks 6 provider integrations as supporting this endpoint:
Azure AI, Azure AI Document Intelligence, Cohere, Mistral AI API, Reducto (reducto), Google - Vertex AI.