Skip to content

feat(text): per-page output with pages=true; 503/502/500 for OCR failures - #22

Open
dvejsada wants to merge 2 commits into
mainfrom
claude/relaxed-pascal-wrzw6x
Open

dvejsada wants to merge 2 commits into
mainfrom
claude/relaxed-pascal-wrzw6x

Conversation

@dvejsada

@dvejsada dvejsada commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #21.

pages=true on /text

A new optional form field. When pages=true, the response also includes "pages": [{"page": n, "text": "…"}].

  • Page numbers come from the 1-based metadata["page"] that the PDF, standalone-image and multi-page TIFF loaders already set. A TIFF gives one entry per frame.
  • Each page is cleaned exactly as the joined text is. text is identical with or without the flag.
  • Blank pages are kept with empty text, so the numbering has no gaps.
  • Formats without pages (.docx, .eml, .msg, .rtf, .xlsx, .pptx, text files…) return "pages": []. The rule: if any loaded document lacks an integer page, the list is empty.
  • Without the field the response is unchanged, so LibreChat and mcp_outlook are unaffected.

Status codes for OCR service failures (/text only)

Situation Before Now
Mistral 429 / 408 / 5xx after the SDK's own retries, no response, httpx timeout or connection error 400 503, with Mistral's Retry-After forwarded when present
Mistral rejects our credentials (401/403), or sends a response the SDK can't use (e.g. a 200 that fails validation) 400 502
OCR not configured (MISTRAL_API_KEY unset, mistralai missing) 400 500
Mistral rejects the document (400/422), or any non-OCR failure 400 400

The detail message is unchanged in every case, so callers matching on it keep working.

The classifier, classify_ocr_failure in app/utils/document_loader.py, identifies Mistral errors by module name, so the route doesn't import the SDK. It follows the exception chain the way Python prints it: __cause__, else __context__ unless raise … from None hid it. Only NoResponseError counts as a transient Mistral error without a status. The new OCRNotConfiguredError subclasses RuntimeError, so existing handlers and tests are unaffected. /embed is not changed.

Testing

  • New tests/test_text_pages.py (24 tests). The Mistral client is faked; the loaders, route and the SDK's real exception classes are used. The tests cover:
    • a 3-page PDF: pages 1–3, and text identical to the request without the flag
    • blank pages
    • a 3-frame TIFF
    • .docx → [] (through pandoc)
    • pages=false → current shape
    • each status-code row above
    • wrapped and from None-hidden exceptions
  • 17 of these fail on main; the 3 added after code review fail on the first commit of this PR. The rest guard behaviour that must not change.
  • Full unit suite passes on the lite and full dependency sets: 480 passed, 1 skipped.
  • Not run: anything against the real Mistral API, and the integration tests (Docker Hub rate-limited the image pull locally). Add the CI label to run them.

README: new "/text endpoint" section documenting the flag and the status codes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01URp9mLt2aeYaLwVPocNnyH

…ures

Closes #21.

/text gains an optional `pages` form field. With pages=true the response
also carries `pages: [{"page": n, "text": ...}]`, taken from the 1-based
metadata["page"] the PDF, image and multi-page TIFF loaders already set.
Each page is cleaned exactly as the joined `text` is, and `text` itself is
unchanged. Blank pages are kept with empty text so numbering has no gaps.
Formats without pages (any load where a Document lacks an integer page)
give []. Without the field the response shape is unchanged.

OCR service failures no longer answer 400:
- 503 for transient ones: Mistral 429/408/5xx left after the SDK's own
  retries, no response, or httpx transport errors and timeouts. Mistral's
  Retry-After is forwarded when present.
- 502 when Mistral rejects our credentials (401/403).
- 500 when OCR is not configured (new OCRNotConfiguredError, still a
  RuntimeError).
Other Mistral 4xx (400/422, the document itself) and non-OCR failures
stay 400. The detail message is the same as before in every case, so
callers matching on it keep working. /embed is not changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01URp9mLt2aeYaLwVPocNnyH
- Follow the exception chain as Python prints it: stop at a context hidden
  with `raise ... from None`, so a deliberate file error raised while
  handling a Mistral error is not reported as an OCR outage.
- A Mistral error with a non-error status (e.g. ResponseValidationError on
  a 200 the SDK can't parse) is upstream's fault: 502, not 400.
- Only NoResponseError counts as a transient Mistral error without a
  status; other errors from mistralai modules that carry no HTTP status
  are no longer 503.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01URp9mLt2aeYaLwVPocNnyH
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.

/text: optional per-page output (pages=true) so callers keep page numbers

2 participants