Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(api): return an empty document for images whose OCR finds no text (#4506)
Abimael Martell430b01080642
diff --git a/apps/api/src/__tests__/snips/v2/scrape-image-ocr.test.ts b/apps/api/src/__tests__/snips/v2/scrape-image-ocr.test.tsindex 2dafe5859..8c866a9ff 100644--- a/apps/api/src/__tests__/snips/v2/scrape-image-ocr.test.ts+++ b/apps/api/src/__tests__/snips/v2/scrape-image-ocr.test.ts@@ -622,6 +622,27 @@ describeIf(SHOULD_RUN)("Image OCR (f-e and fire-pdf dependent)", () => {scrapeTimeout,);+ it(+ "returns an empty document for an image without text",+ async () => {+ // A 16x16 JPEG 2000 image: the browser hands it to image OCR, which+ // finds nothing to read. That is a complete, empty result billed like+ // any other image, not an engine failure that exhausts the waterfall.+ const response = await scrape(+ {+ url: `${TEST_SUITE_WEBSITE}/tiny-image.jp2`,+ formats: ["markdown"],+ },+ identity,+ );++ expect(response.metadata.contentType).toBe("image/jp2");+ expect(response.metadata.statusCode).toBe(200);+ expect((response.markdown ?? "").trim()).toBe("");+ },+ scrapeTimeout,+ );+it("serves the raw image through the rawBase64 format",async () => {diff --git a/apps/api/src/__tests__/snips/v2/scrape-mislabeled-files.test.ts b/apps/api/src/__tests__/snips/v2/scrape-mislabeled-files.test.tsindex becf8fdeb..3a3cf0aa2 100644--- a/apps/api/src/__tests__/snips/v2/scrape-mislabeled-files.test.ts+++ b/apps/api/src/__tests__/snips/v2/scrape-mislabeled-files.test.ts@@ -25,8 +25,10 @@ beforeAll(async () => {// routes them to a parser by content type — and servers mislabel. Static// hosts serve a `.jp2` file as image/jp2 whatever its bytes are, so// mislabeled-pdf.jp2 reproduces a PDF served with an image content type,-// while tiny-image.jp2 is a real JPEG 2000 image. Only fire-engine performs-// the handoff, hence the gate.+// while tiny-image.jp2 is a real JPEG 2000 image. This identity has no+// imageOcr team flag, so the real image must keep the historical rejection+// (the flagged behaviour lives in scrape-image-ocr.test.ts). Only+// fire-engine performs the handoff, hence the gate.describeIf(!process.env.TEST_SUITE_SELF_HOSTED && ALLOW_TEST_SUITE_WEBSITE)("Mislabeled file handoff (f-e dependent)",() => {@@ -50,7 +52,7 @@ describeIf(!process.env.TEST_SUITE_SELF_HOSTED && ALLOW_TEST_SUITE_WEBSITE)();it(- "keeps rejecting a real JPEG 2000 image as unsupported",+ "keeps rejecting a real JPEG 2000 image for a team without image OCR",async () => {const response = await scrapeWithFailure({diff --git a/apps/api/src/scraper/scrapeURL/index.ts b/apps/api/src/scraper/scrapeURL/index.tsindex b1e8b3138..270437228 100644--- a/apps/api/src/scraper/scrapeURL/index.ts+++ b/apps/api/src/scraper/scrapeURL/index.ts@@ -735,9 +735,16 @@ async function scrapeURLLoopIter((engineResult.statusCode >= 200 && engineResult.statusCode < 300) ||engineResult.statusCode === 304;const hasNoPageError = engineResult.error === undefined;+ // A parsed image is a complete result even when OCR found no text: the+ // image engine has already verified the bytes and run them through OCR,+ // so a blank scan or a photo legitimately comes back as an empty document.+ // Without this it would be "deemed unsuccessful" below and, as the last+ // engine in its waterfall, surface as an all-engines failure.+ const isParsedImage =+ engine === "image" && isGoodStatusCode && hasNoPageError;const hasRequiredOutput = hasRawBase64? engineResult.rawBase64 !== undefined- : isLongEnough || !isGoodStatusCode;+ : isParsedImage || isLongEnough || !isGoodStatusCode;const isLikelyProxyError = [401, 403, 429].includes(engineResult.statusCode,);@@ -765,7 +772,12 @@ async function scrapeURLLoopIter(// should we just use all the fallbacks and pick the one with the longest text? - mogeryif (hasRequiredOutput) {meta.logger.info("Scrape via " + engine + " deemed successful.", {- factors: { isLongEnough, isGoodStatusCode, hasNoPageError },+ factors: {+ isLongEnough,+ isGoodStatusCode,+ hasNoPageError,+ isParsedImage,+ },});return engineResult;} else {