Skip to main content
Every model exposes a method extra keyword argument for more specialized tasks, such as parse_layout or ocr. Document requests run one page at a time. Each model applies its own default method.
Use JSON mode (response_format: {"type":"json_object"}) when you call the API programmatically. Omit response_format for text when a model runs as an agent tool call or through MCP.

1. Pick a model

Start with paddlepaddle/paddleocr-vl-1.6 for general-purpose markdown, or paddleocr/pp-ocrv6 for fine-grained bounding boxes, as in traditional OCR. Switch only when you need a method neither serves. To compare models, see the model catalog. For each method and its parameters, see Methods.

2. Send the document

Pass the PDF as a document_url content part. Prefer a hosted URL over a base64 data URI. For size limits, see Supported Inputs. For document_dpi and other options, see Method Parameters.

Request knobs

Document rasterization

document_dpi is the rasterization DPI for each page of a document_url, before OCR. Set it at the top level or inside method_params. When both are set, method_params wins. Start at the default, and raise document_dpi if the model misses dense or small text. For page-count and file-size limits, see Supported Inputs.

3. Streaming vs non-streaming

In text mode, stream: true applies to every chat request. The streamed result is byte-identical to the non-streaming result.
  • Document request: One SSE chunk per completed page block, in ascending page order.
  • Single-image request on a chat VLM: Streams deltas as they generate.
  • Single-image request on an OCR method that returns the model’s text unchanged: Streams deltas as they generate.
  • JSON response_format: Buffered. With stream: true, the finished object is sent as SSE frames. Per-page deltas cannot form one JSON object.
Each delta.content is one piece of the document: the <document> open tag, one <page> block, or the close tag. Each <page> carries its own format, so the page is self-describing before the client buffers the header.

4. Parse the response

response_format selects text mode or JSON mode. method selects each page’s payload kind. Both modes carry the same per-page content. The contract is in Response Formats.

Text mode (default)

Every document method uses the same wrapper:
  • One <document> element per input PDF. Attributes carry the file metadata.
  • One <page> block per rasterized page.
  • method sets only the page body. format names that body.
The reply is one string in choices[0].message.content:
  • A failed page is self-closing. It has status="error" and no body. Page numbering stays intact.
  • A single image (not a PDF) returns the payload block alone. There is no wrapper.
  • A structured request carries one input medium. The reply holds at most one document or one image.

JSON mode

Set response_format to {"type":"json_object"}. Every method returns one shape. A failed page has "status": "error" and no content.
Page content is always doc.page.blocks. items is [] when nothing is found. Markdown-kind methods return one block with text. For the full list of fields on each block, see the document block record.
This is the only JSON shape the Gateway returns. You cannot extract a custom document schema here. For schema-driven extraction, use structured responses and custom schemas.

Element attributes

Consuming each shape

Non-streaming blocks are concatenated in order in choices[0].message.content. Streaming chunks are OpenAI-style SSE, in document order, byte-identical to the non-streaming reply. Split text mode on the <document> and <page> tags. Read each page body by its format. Use JSON mode to skip tag parsing.

5. Track cost and handle errors

Non-streaming cost is response.usage.cost, in USD. For streaming, set stream_options to {"include_usage": true}. The last SSE event carries usage, including usage.cost, on the chunk instead of delta.content. Read usage before treating a chunk as page content.
Both modes:
  • Each page gets three attempts. Then it is marked status="error". The retry unit is the page. A page that succeeded is not re-run. A page that exhausts its attempts is not billed. usage counts the pages that produced output.
  • When every page fails, the request returns 502 with code: "document_inference_failed" and bills nothing. The message says whether a retry can help. See Document inference failed (502).
  • Retry 429 and 500 with backoff, but do not retry 400 capability violations. For the full list, see Error Codes.
  • Keep the x-request-id response header, so support can trace the request if you need help.

Supported Inputs

Content part types, document limits, and URL vs base64 tradeoffs.

Methods

Per-model method and method_params reference.

Error Codes

Status codes, error bodies, and retry guidance.

Models

Document and image OCR model catalog.