Skip to content

ocr: POST /ocr in LiteParse's contract (#2440 step 4) - #2573

Merged
ashhart merged 7 commits into
ashhart:mainfrom
laudney:ocr-4-liteparse
Nov 24, 2026
Merged

ashhart merged 7 commits into
ashhart:mainfrom
laudney:ocr-4-liteparse

Conversation

@laudney

@laudney laudney commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor
Before (#2572) After
LiteParse lit parse --ocr-server-url http://HOST:PORT/ocr no endpoint POST /ocr: one text item a line, bbox in the page's pixels, confidence 1.0
OCR line estimates none a block's lines cut its box into equal bands

Step 4 of the stack #2440 set out. It follows #2572, now on main.

#2440 gate Proof in this pull request
Its unmodified client's request round-trips, with its own refusals tested liteparse_test.zig replays the exact 585-byte multipart body lit 2514f8e sent to a recording server (fixtures/, with the command) and checks the reply has only the fields the published spec defines; the refusals (no text boxes, a PDF, an empty or non-image file, part errors) each have a test

Provenance, following your ruling on #2530: every field comes from LiteParse's published OCR API spec, and every message is ours.

Field, option or message Source
POST /ocr, multipart, one file part (an image) OCR_API_SPEC.md (Endpoint, Request Format)
language part (optional; read and ignored) spec (language, optional, default en)
reply {"results": [...]}, in reading order spec (Response Format; "Results should be ordered by reading order")
text; bbox [x1, y1, x2, y2] axis-aligned, pixels, origin top-left, x2 > x1 and y2 > y1 spec (Fields; Coordinate System)
confidence 1.0 spec ("If your OCR engine doesn't provide confidence, use 1.0")
polygon: not sent spec ("Optional."); it does not say how LiteParse reads a rotation from it, so a rotated line goes as its axis-aligned box
error body {"error": "..."}, statuses 400 and 500 spec (Error Handling); 422, and 503 with Retry-After, are ours
every refusal message; line bands ours (docs/api.md OCR: Estimates)

An earlier version decided when to send polygon by re-implementing LiteParse's own rotation arithmetic from its source, tested with its quads; that is gone.

What this changes

POST /ocr (and /ocr/) answers LiteParse's HTTP OCR contract. The route reads the body once and picks the contract from ?format=liteparse, then the media type, then the part names; with only LiteParse served, JSON or no Content-Type is 415, and a body with any files part is 400 on every path rather than dropped (#2574 turns this into the docTR/LiteParse rule). Each OCR line becomes one result. GLM-OCR, which gives no boxes, is refused for LiteParse. A query name sent twice is 400, and with no OCR reader or no --vision every request gets the gate's 400 before the contract choice.

Checks on this head

  • Head commit: 5a68e5bf0, on main 72ebe5f6e: the rebuilt 4 commits, then 1 commit for your 11-23 review (a --boundary-- line closes only when blanks and a line break or the body's end follow). Rebuilt like ocr: PDF documents and pages on /v1/ocr, one page at a time (#2440 step 2) #2571 and ocr: GLM-OCR reader on /v1/ocr (#2440 step 3) #2572: the step-4 change, with the review fixes (a repeated query name refused; the gate's refusal before 415), applied to main in 4 commits with the original subjects. Each file's change equals the reviewed change, except three conflict lines: the docs/api.md scope row and the auth_scope.zig list, which main had grown, each gain /ocr; CHANGELOG.md is no longer touched.
  • Lean checker, python3 tools/zig/lean_check.py: 167 on main and 167 on this head, the same list, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-ocr-host test-api-boundaries test-server-cpu test-dots-mocr-host test-glm-ocr-host check-native-hip-host -Dhip=true: 24/24 steps, 2008 of 2027 pass, 19 skip, 0 fail
    • after the review commit, zig build test-ocr-host test-api-boundaries test-server-cpu -Dhip=true: 11/11 steps, 1941 of 1956 pass, 15 skip, 0 fail; lean 167
    • zig fmt --check zig/src build.zig zig/build and git diff --check: clean
  • CI: pending on this head. A test merge into main 72ebe5f6e is clean.
  • Not run, and why: the dots.mocr oracle-run test and the PDF tests skip here (no TF_DOTS_OCR_DIR runs and no PDFium on this run); this step does not change the PDF path. CUDA, Metal and Windows: no OCR reader is offered there; the readers are wired into the HIP offers only.

Receipt

  • Client receipts on gfx1201, LiteParse lit 2514f8e through a logging proxy: dots.mocr, a mixed PDF: 200, the server's results carry text, bbox and confidence only, and LiteParse's 3 OCR items equal the server's pixels x 72/150 exactly, with its 3 native lines kept; a cut page (curl): 422 {"error": "page 0: ..."}. GLM-OCR: curl 400 "this model gives no text boxes; LiteParse needs them"; lit on a scanned-only PDF aborts ("OCR failed for all 1 page(s)"), and on a page with native text continues with a warning.
  • Boxes it reaches: the HIP native server with an OCR reader and --vision.
  • New switch? No.
  • Environment: receipts on base 9c9bf542c, checks on main 72ebe5f6e; Zig 0.17.0; ROCm 10.1 (hipcc HIP 7.16); one Radeon AI PRO R9700 (gfx1201); Linux; dots.mocr dots-studio/dots.mocr e539fbb52280393adc081b289ec597430a0f9031, GLM-OCR zai-org/GLM-OCR 2e85a62840ccac27daa451df36c736c4636b8628; PDFium chromium/7725; libjpeg-turbo 3.1.4.1. The end-to-end client receipts ran on the whole stack's code (c517f4379, byte-equal to step 6's OCR files), which serves this step's requests through this step's code.
  • Paths and platforms the change reaches, which I ran and which I could not: HIP gfx1201 end to end; CUDA, Metal and Windows not run.
  • Exactness: n/a; no generation path changes.
  • Main against this branch: n/a; no chat path changes in this step.
  • Decode speed before and after: n/a.
  • Prompt speed before and after: n/a.
  • Licence of any new weights or drafter, or "n/a": n/a

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @laudney. An early read of step 4: the multipart and query readers are bounded and carefully written, and the source table makes the LiteParse fields easy to check against its published spec. One thing to carry into the rebase:

  1. zig/src/server/query.zig:5-14 takes the first value of a repeated parameter, while docTR's server takes the last, so ?resolve_lines=true&resolve_lines=false would mean the opposite here (and format= reads through the same function). Please refuse a repeated name with a 400 and add a test for it.

Optional: zig/src/server/ocr/route.zig:42 chooses the dialect before run.gate, so on a model without an OCR reader a text/csv body gets 415 instead of the usual "this model has no OCR reader". Gating first would make that refusal the same for every request.

It follows #2572 in the order you set out.

  • Synthena (Ash's Agent)

@laudney

laudney commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Both are done as a new local commit, carried to #2574 and #2575. They go up with the rebase after #2572, as you suggested.

Point Change
1. A repeated query parameter query.get returns error.Repeated when a name occurs twice (checked before its value is decoded, so ?x=%G1&x=1 is repeated, not malformed). /ocr answers 400 "the format query parameter is repeated; send it once"; in #2574 docTR's own parameters answer the same, for example "the resolve_lines query parameter is repeated; send it once". Tests: query_test.zig (five repeated forms, including an encoded name), route_test.zig (selection and the wire), and in #2574 doctr_test.zig (?resolve_lines=true&resolve_lines=false)
Optional: the gate before the dialect choice The dialect choice still runs first, so a request that picks LiteParse (or docTR, PaddleX) keeps its own error shape. When no dialect takes the request, a model without a reader or a server without --vision now answers the gate's 400 ("this model has no OCR reader; ..."), not 415 or 400. A new route test fails with the old route.zig
Check (local, steps 4 to 6) Result
zig build test-ocr-host test-api-boundaries test-server-cpu test-dots-mocr-host test-glm-ocr-host check-native-hip-host -Dhip=true exit 0 on each step
Lean checker 116 on each step, as before
Review (Codex and mine) no findings

docs/api.md names both rules.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @laudney. Both points are handled the way you describe. A repeated name is refused with a 400 before its value is decoded, and there are tests in the query reader, the route and on the wire. A request that no dialect takes now gets the gate's refusal when the model has no reader. I'll read the new commit once it's up. Apart from that, I have nothing more to ask about step 4's code.

When #2572 lands:

  1. Please rebase onto main and push, including this round's commit. Main has moved since the trial merge in the description. Merging this head (79fafba30) into main 3885dceb2b now conflicts in zig/src/core/engine_status.zig, zig/src/server/routes.zig, zig/src/server/auth_scope.zig and docs/api.md, not only CHANGELOG.md. Most of it is the lines ocr: POST /v1/ocr on dots.mocr for images (#2440 step 1) #2570's rebase already resolved. In auth_scope.zig and routes.zig, step 4's /ocr goes on the same lines as step 1's /v1/ocr, so those two need it added on top of the rebased steps.
  2. Please refresh "Checks on this head" for the new head: CI, the merge line, and the test counts, which are still from 6df521396.

Once it's rebased, CI is green and there are no other asks, it follows #2572 into the landing queue.

  • Synthena (Ash's Agent)

@laudney

laudney commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@ashhart rebased onto main 72ebe5f6e (with #2572), as you asked, and marked ready. New head: 2b0a8e917.

Before (79fafba30) After (2b0a8e917)
Base 9c9bf542c, under the old #2572 main 72ebe5f6e
Commits 4 for step 4, then 8 review commits 4, with the original subjects
Review fixes from your 11-12 read local only in: a repeated query name is 400; with no reader or no --vision, the gate's 400 comes before 415
Change each file's change equals the old one, except the /ocr entry in the scope row of docs/api.md and in auth_scope.zig, whose lists main had grown; CHANGELOG.md is no longer touched
Merge with main conflicted clean
Check on 2b0a8e917 Result
zig build test-ocr-host test-api-boundaries test-server-cpu test-dots-mocr-host test-glm-ocr-host check-native-hip-host -Dhip=true 24/24 steps, 2008 of 2027 pass, 19 skip, 0 fail
Lean checker 167 on main, 167 on this head, the same list

#2574 follows when this lands.

@AIFlowML

Copy link
Copy Markdown
Contributor

I checked the ready head 2b0a8e917 against main 1eed88bb0, which already contains #2572. It trial-merges cleanly and passes git diff --cached --check.

zig build test-ocr-host -Dcpu=apple_m1 -j4 --summary all

On M5 Max with Zig 0.17.0, all five steps completed with 122 tests passed and two skipped. The filter includes the recorded LiteParse multipart request and the OCR route's result and refusal fixtures. This run used the host server fixtures without loading an OCR model.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @laudney. The LiteParse integration and the multipart and query coverage look carefully put together.

One small hardening, not a blocker. A body ending in --XyZ--junk, with boundary XyZ, is treated as the closing delimiter, and parsing stops there, so any later parts are ignored. The doc comment and test suggest this leniency is deliberate. If it is, could you say why? Otherwise, please accept only optional padding and a line ending after the closing delimiter, and add a regression case.

  • Synthena

@laudney

laudney commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@ashhart thanks. It was not deliberate, so I hardened it in 5a68e5bf0, as you suggest.

--XyZ-- followed by Before After (RFC 2046: padding, then CRLF or the end)
junk close; later parts ignored body text, like other boundary-like lines; later parts are read
x then CRLF close body text
nothing (end of body), blanks, CRLF, LF, CRLF and an epilogue close close

The regression test multipart_test.zig "the close delimiter takes only blanks and a line break or the end after it" reads a second part after a --XyZ--junk line, refuses a body whose only close is --XyZ--junk, and accepts the five valid close forms. It fails on 2b0a8e917 (1 part instead of 2) and passes on this head.

Check on 5a68e5bf0 Result
zig build test-ocr-host test-api-boundaries test-server-cpu -Dhip=true 11/11 steps, 1941 of 1956 pass, 15 skip, 0 fail
Lean checker 167, the same as main

laudney and others added 7 commits November 24, 2026 13:50
The /ocr clients post multipart/form-data and pass options as query
parameters, which the server never read before. multipart.zig
parses a body already read into parts that are slices of it,
keeping the raw Content-Type and a normalized media type, with
bounded headers and part counts; query.zig decodes one parameter,
refuses a name sent twice so a repeated option cannot mean two
things, and reads pydantic's boolean words.
The line contracts on /ocr carry plain-text lines, while block
readers give one box a block, so markdown.zig reads a block's
normalized Markdown as plain-text lines, table rows included, and
lines.zig divides the block's box into equal bands along its reading
edges, one per line, and a line's box into words in proportion to
each word's character count, splitting on the whitespace Python's
str.split uses. Every derived quad passes the shared validation, so
no encoder sees a zero-area box.
LiteParse's --ocr-server-url sends one page image as a multipart
file part and reads text items with boxes in the image's pixels, so
POST /ocr now chooses its contract from ?format=, the media type and
the part names, and answers LiteParse with one result a line: text,
an axis-aligned bbox and confidence 1.0, the fields LiteParse's
published OCR API spec defines. A reader without boxes and a PDF
are refused, files parts are refused on every path rather than
dropped, a repeated format is refused, and the client's captured
request is replayed. Refusals made before the contract is known use
the OpenAI envelope, and a model without a reader or a server
without --vision gives the gate's refusal on every path.
The API guide gains the /ocr route: how it picks its contract and
which refusals use the OpenAI envelope, the gate's refusal on every
path, the refusal of a repeated query name, LiteParse's failure
column, the estimated lines and scores, and the LiteParse section on
its published OCR spec with the refusal of readers without boxes;
the inference scope lists /ocr.
A body line `--XyZ--junk`, with boundary XyZ, was taken as the close
delimiter, so the parser stopped there and ignored any later parts.
RFC 2046 lets only transport padding and a line break, or the end
of the body, follow `--boundary--`, so the close now needs exactly
that, and a line with more text stays in the part body like any
other boundary-like text; a regression test reads a later part past
such a line and checks the accepted close forms.
…gument post

main's cache-salt isolation test called ocr/route.zig's post with three
arguments; this branch gives post the route it dispatches on. The test sends
a /v1/ocr request, so it now passes "/v1/ocr". Nothing else changes.
@ashhart
ashhart merged commit 83f4a6b into ashhart:main Nov 24, 2026
12 checks passed
@ashhart

ashhart commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you @laudney. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit to IF-YFXJ/TensorFold that referenced this pull request Nov 24, 2026
ashhart added a commit that referenced this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants