fix(legal-aid): render PDF pages as JPEG q85 instead of PNG

Bundling all report pages as 180-DPI PNG base64 in one ai-gateway
chat-completions request blew past its body limit and returned 413.
A rendered table page is mostly white, so q85 JPEG is several times
smaller while keeping the table text crisp enough for Claude Vision.
Pairs with the ai-gateway limit bump to 25mb.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-03 08:26:45 +00:00
parent 89f4dd3399
commit 2628a37c56
+6 -2
View File
@@ -123,8 +123,12 @@ async def parse_pdf_report(
page_count = min(len(doc), _PDF_MAX_PAGES) page_count = min(len(doc), _PDF_MAX_PAGES)
for i in range(page_count): for i in range(page_count):
pix = doc[i].get_pixmap(dpi=_PDF_RENDER_DPI) pix = doc[i].get_pixmap(dpi=_PDF_RENDER_DPI)
png = pix.tobytes("png") # JPEG (not PNG): a rendered table page is mostly white, and
images_b64.append(("image/png", base64.b64encode(png).decode("ascii"))) # PNG of it is several × larger than a q85 JPEG. Bundling all
# pages as PNG blew past ai-gateway's body limit (413). JPEG
# q85 keeps the table text crisp enough for Claude Vision.
jpg = pix.tobytes("jpeg", jpg_quality=85)
images_b64.append(("image/jpeg", base64.b64encode(jpg).decode("ascii")))
except Exception as e: except Exception as e:
logger.exception("Legal Aid PDF render failed") logger.exception("Legal Aid PDF render failed")
raise HTTPException(status_code=422, detail=f"PDF render error: {e}") raise HTTPException(status_code=422, detail=f"PDF render error: {e}")