Melious

This guide shows you how to send page images to unlimited-ocr-2 on Chat completions and get back the text of each page, with its layout blocks. It's for scanned PDFs, faxes, photos of invoices and forms — anything where the text only exists as pixels.

unlimited-ocr-2 is Baidu's open-weight Unlimited-OCR-2 (3B, MIT license). It reads up to 20 pages per request and answers with one <PAGE> marker per page, in the order you sent them.

Read one page:

import base64

from openai import OpenAI

client = OpenAI(
    api_key="sk-mel-<YOUR_API_KEY>",
    base_url="https://api.melious.ai/v1",
)

with open("page-1.jpg", "rb") as f:
    b64 = base64.b64encode(f.read()).decode()

response = client.chat.completions.create(
    model="unlimited-ocr-2",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}},
                {"type": "text", "text": "<PAGE><image>Multi page parsing."},
            ],
        },
    ],
)
print(response.choices[0].message.content)
// ocr.mjs — an ES module (top-level await)
import { readFileSync } from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "sk-mel-<YOUR_API_KEY>",
  baseURL: "https://api.melious.ai/v1",
});

const b64 = readFileSync("page-1.jpg").toString("base64");

const response = await client.chat.completions.create({
  model: "unlimited-ocr-2",
  messages: [
    {
      role: "user",
      content: [
        { type: "image_url", image_url: { url: `data:image/jpeg;base64,${b64}` } },
        { type: "text", text: "<PAGE><image>Multi page parsing." },
      ],
    },
  ],
});
console.log(response.choices[0].message.content);
jq -n --rawfile img <(base64 < page-1.jpg | tr -d '\n') '{
  model: "unlimited-ocr-2",
  messages: [{
    role: "user",
    content: [
      {type: "image_url", image_url: {url: ("data:image/jpeg;base64," + $img)}},
      {type: "text", text: "<PAGE><image>Multi page parsing."}
    ]
  }]
}' | curl -sS https://api.melious.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-mel-<YOUR_API_KEY>" \
  -H "Content-Type: application/json" \
  -d @-

Turn a PDF into page images

The model reads images, not PDF files, so you render the pages on your side first. JPEG at 200 dpi is a good default: even 20 dense pages fit in one request at that resolution.

With PyMuPDF in Python:

import base64

import pymupdf  # pip install pymupdf


def page_images(path: str, dpi: int = 200) -> list[str]:
    """Render every page of a PDF as a base64 JPEG."""
    images = []
    with pymupdf.open(path) as doc:
        for page in doc:
            pixmap = page.get_pixmap(dpi=dpi)
            images.append(base64.b64encode(pixmap.tobytes("jpeg", jpg_quality=85)).decode())
    return images

From the shell, Poppler's pdftoppm writes one JPEG per page (page-1.jpg, page-2.jpg, …):

pdftoppm -jpeg -r 200 document.pdf page

A PDF that already has a text layer — exported from Word, generated by software — doesn't need OCR at all. Read its text directly (page.get_text() in PyMuPDF, pdftotext from Poppler): it's exact and free. Office files (DOCX, XLSX, PPTX) go through PDF first, for example with LibreOffice.

Send a whole document

Put all page images of a document into one message, followed by one text part: <PAGE><image> once per page, then Multi page parsing.. The model reads them as one document and keeps their order; streaming gets you the first text after about a second instead of after the last page.

from openai import OpenAI

client = OpenAI(
    api_key="sk-mel-<YOUR_API_KEY>",
    base_url="https://api.melious.ai/v1",
)


def read_document(images: list[str]) -> str:
    """OCR up to 20 base64 JPEG pages in one streamed request; returns the raw output."""
    content = [
        {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}}
        for b64 in images
    ]
    content.append({"type": "text", "text": "<PAGE><image>" * len(images) + "Multi page parsing."})

    stream = client.chat.completions.create(
        model="unlimited-ocr-2",
        messages=[{"role": "user", "content": content}],
        stream=True,
        stream_options={"include_usage": True},
    )
    parts = []
    for chunk in stream:
        if chunk.choices and chunk.choices[0].delta.content:
            parts.append(chunk.choices[0].delta.content)
    return "".join(parts)


images = page_images("document.pdf")  # from the PyMuPDF example above
raw = "".join(read_document(images[i : i + 20]) for i in range(0, len(images), 20))

A document longer than 20 pages goes out in chunks of 20, as in the last line. The pages of each chunk come back in order, so concatenating the outputs keeps the whole document in order.

Read the output

The output starts each page with <PAGE>. Inside a page, every layout block starts with <BLOCK>, its type, its bounding box, and <FORK>, followed by the block's text:

<PAGE><BLOCK>title<LEVEL2>[389, 200, 674, 222]<FORK>LIST OF EXERCISES
<BLOCK>text[187, 233, 873, 869]<FORK>MODEL SCRIPT SET ..... 2-5
EXERCISE 1. BUSINESS TRANSACTIONS EXPLAINED ..... 7
<PAGE><BLOCK>text[463, 81, 934, 147]<FORK>New York, Oct 24 1886
  • Block types include title, text, table, image, figure, chart, and seal. Titles carry their heading depth as <LEVEL1>, <LEVEL2>, and so on.
  • Bounding boxes are [x1, y1, x2, y2] on a 0–1000 scale of the page's width and height.
  • Tables come as HTML <table> markup.
  • Blocks that continue on the next page have the type prefixed with cross_ or append_, for example cross_table.

To get plain text per page, split on <PAGE> and drop the block headers:

import re

BLOCK_HEADER = re.compile(r"<BLOCK>[a-z_]+(?:<LEVEL\d+>)?\[[^\]]*\]<FORK>")


def page_texts(raw: str) -> list[str]:
    """One plain-text string per page, in page order."""
    pages = raw.split("<PAGE>")[1:]
    return [BLOCK_HEADER.sub("\n", page).replace("<MERGE>", "").strip() for page in pages]

For Markdown with headings, merged tables, and image placeholders, Baidu publishes a converter with no dependencies: utils/process_unlimitedOCR2.py in the model's repository (convert(raw_output) returns the Markdown for one output).

What Melious sets for you

The model needs a fixed set of engine parameters to read pages reliably, and Melious sets them on every request. You don't send them, and if you do, we ignore your values — the request still succeeds. That's the case for temperature, skip_special_tokens, custom_logit_processor, custom_params, and images_config, which Baidu's own client examples send. max_tokens is capped at 6,000 tokens times the number of pages. A lower value of yours is kept, and it limits the whole request, not each page — leave it out unless you want the output cut.

The model doesn't reason, call tools, or produce structured output. We drop reasoning_effort; leave tools and response_format out of OCR requests.

Limits

LimitValue
Pages per request1–20 image parts. More, or none, is a 400.
Content partsimage_url and text only. Any other part type is a 400.
Request body25 MB, base64 included. At 200 dpi that's about 20 dense pages; at 300 dpi 12–16.
URL imagesFetched by us: at most 20 MB, answered within 10 s. Otherwise a 400.
OutputAt most 6,000 tokens per page. A page that runs into the cap is cut there; the next page starts normally.
Concurrent requests16 at a time per account for this model, on every plan. Above that you get a 429 with Retry-After: 5 (on a stream, an error event) — see Rate limits.

A page usually takes 2–5 seconds and a 20-page document about a minute, streamed or not. Without streaming you see nothing until the last page is done, so set your client's timeout to a few minutes, or stream.

Cost

Billing is per token, like every chat model: the page images count as input tokens, the text as output tokens. A single page is about 1,300 input and 1,000 output tokens; a page inside a multi-page request is about 300 input tokens, because the model reads it at a lower tiling. See the model in the hub for the current per-token price, and Pricing for how billing works.

Troubleshooting

400 "This model accepts at most 20 page images per request" — split the document into chunks of 20 pages, as in Send a whole document.

400 "This model reads page images: send at least one image_url content part" — the request had no image. Check that your image parts survived serialization.

400 "This model accepts only text and image_url content parts" — remove other part types, for example input_audio or file. Send PDFs as rendered images.

413 request_too_large — the body is over 25 MB. Render at 200 dpi, or send fewer pages per request.

A page comes back empty — it's a blank page, or an image the model can't read: check the image and the resolution. Empty pages keep their <PAGE> marker, so page numbers don't shift.

On this page