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:
@@ -123,8 +123,12 @@ async def parse_pdf_report(
|
||||
page_count = min(len(doc), _PDF_MAX_PAGES)
|
||||
for i in range(page_count):
|
||||
pix = doc[i].get_pixmap(dpi=_PDF_RENDER_DPI)
|
||||
png = pix.tobytes("png")
|
||||
images_b64.append(("image/png", base64.b64encode(png).decode("ascii")))
|
||||
# JPEG (not PNG): a rendered table page is mostly white, and
|
||||
# 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:
|
||||
logger.exception("Legal Aid PDF render failed")
|
||||
raise HTTPException(status_code=422, detail=f"PDF render error: {e}")
|
||||
|
||||
Reference in New Issue
Block a user