Title: feat(knowledge): read a PDF's text layer before paying for OCR by waleedlatif1 · Pull Request #6850 · simstudioai/sim · GitHub
Open Graph Title: feat(knowledge): read a PDF's text layer before paying for OCR by waleedlatif1 · Pull Request #6850 · simstudioai/sim
X Title: feat(knowledge): read a PDF's text layer before paying for OCR by waleedlatif1 · Pull Request #6850 · simstudioai/sim
Description: Every PDF is sent to OCR — an external, per-document call — even though most carry an embedded text layer that costs nothing to read. This reads the text layer first and reserves OCR for the documents that actually need it. The evidence Across a real corpus of 2,693 PDFs that exist both ways (locally extracted and OCR'd): Local extraction produced nothing 0 Local within 70% of OCR's character volume 2,515 (93%) Local materially under-extracted 178 (7%) Separately, of ~1,480 PDFs in that library only ~9% genuinely lacked a text layer. So the large majority of OCR calls were buying nothing. One caveat stated plainly: local extraction returned more characters than OCR on average (116K vs 14K), which is not local winning — pdfjs emits positioning noise where OCR returns clean markdown. Character volume demonstrates coverage, not fidelity. That is why the threshold is tunable and the routing is logged. How a text layer is judged Three independent failure modes, none of which catches the others. Taken from prior art rather than invented — pdfmux documents a text-density check, and docling#2963 describes the corrupted-layer cases: No text — a scan. Sparse text — present but too thin to be the document, e.g. a header or stamp on a scanned page. Measured per page, since a total-character floor cannot tell a short memo from a long scan. Text that is not language — a broken encoding, or the raw character ids a CID-keyed font emits when it has no ToUnicode map (/31 /8 /18 /12). This one matters here: it produces plenty of characters, so a length check reads it as healthy, and it is common in documents from older generators and subset fonts — much of the contract and procurement material that reaches a knowledge base. KB_PDF_MIN_CHARS_PER_PAGE (default 100) moves the balance toward cost or fidelity without a deploy. A typeset page carries roughly 1,500–3,000 characters and a scan carries none, so the gap is wide and the threshold does not need to be precise. Beyond cost This narrows an availability dependency. Previously an OCR outage failed every PDF; now it only reaches the minority that cannot be read locally. That materially reduces the blast radius of the one failure mode this design deliberately does not paper over with a silent fallback. Known limitation The judgement is per document, so a file mixing typeset pages with scanned inserts can average above the threshold and keep its partial text. Per-page routing would catch it and needs per-page extraction we do not currently have. Called out rather than hidden — it is the natural next refinement. One behavioural change worth review attention assertKnowledgeOpaqueModelInputSafe now runs after the local read rather than before it, because the local read is what decides whether OCR is needed at all. The guarantee is unchanged: bytes never reach an external model when the projection is refused. The refusal simply fires at the outbound request instead of the storage read, and its test now asserts against fetch accordingly. This is consistent with the case directly above it in that file, which establishes that local parsing is not model egress. It is also the better ordering — a secret-bearing PDF with a usable text layer is now indexed locally instead of being refused for an external call that never happens. Testing vitest run lib/knowledge/ lib/uploads/ lib/file-parsers/ connectors/ app/api/knowledge/ — 2,020 passed (131 files) pdf-text-layer.test.ts — 9 cases: typeset document, scan, sparse text, CID escapes, replacement characters, prose containing slashes and digits, accented and non-Latin text, unknown page count, and threshold scaling with length pdf-ocr-triage.test.ts — 4 routing cases: text layer used with no OCR call, and fallthrough on scan, CID escapes, and an unparseable PDF. Verified the first fails when the triage is disabled bun run check:audits — 29/29 tsgo --noEmit — no errors in changed files Checklist Self-reviewed No secrets or customer data introduced
Open Graph Description: Every PDF is sent to OCR — an external, per-document call — even though most carry an embedded text layer that costs nothing to read. This reads the text layer first and reserves OCR for the docume...
X Description: Every PDF is sent to OCR — an external, per-document call — even though most carry an embedded text layer that costs nothing to read. This reads the text layer first and reserves OCR for the docume...
Opengraph URL: https://github.com/simstudioai/sim/pull/6850
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:0eedf6ea-84ce-69e9-d8d6-2361c460fd4d |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E6A8:858A:BB5C85:1092951:6AFB5E20 |
| html-safe-nonce | bac9db084296431993bdf4adca59b5cd8cfee44046514e9528645c664c2645c9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkE4Ojg1OEE6QkI1Qzg1OjEwOTI5NTE6NkFGQjVFMjAiLCJ2aXNpdG9yX2lkIjoiMjM4MzIwOTM4MTE2NjY4OTYxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 33986cfa3fce9481cb2343bf7f6fabacb7888065e2df95c911483f3b0e024eb7 |
| hovercard-subject-tag | pull_request:4316144574 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/6850/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | Every PDF is sent to OCR — an external, per-document call — even though most carry an embedded text layer that costs nothing to read. This reads the text layer first and reserves OCR for the docume... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | ea33d13c96d0ff66bd8fa906b193d2213536b78cb06b0c50a4632c58b1418f94 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 59ef97164c52cf59bd10558ddec9c4f482763988 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width