# HTTP 413 `payload_too_large` — API Store Hub

> VI: HTTP 413 xảy ra khi tổng dung lượng HTTP body (thường do nhúng nhiều ảnh PNG gốc độ phân giải 4K hoặc tệp PDF/audio mã hóa base64 nở thêm 33%) vượt giới hạn kích thước request.
> EN: HTTP 413 triggers when the HTTP request body—usually uncompressed PNG screenshots, large PDFs, or base64 audio (which adds 33% overhead)—exceeds size limits.

- Canonical VI: https://apistorehub.com/vi/loi-api/413-payload-too-large-media
- Canonical EN: https://apistorehub.com/en/loi-api/413-payload-too-large-media
- Retry Policy: Never retry oversized payloads as-is; compress or split the media first.

## Root Causes
- Uploading raw 10-20MB PNG photos directly into Vision requests without resizing.
- Base64 encoding inflating binary payloads by 33% across multiple attached pages.
- Re-sending previous base64 images on every subsequent chat turn.

## Step-by-Step Resolution
1. Resize images to 1536–2048px on the long edge and encode as JPEG/WebP at quality 80-85%.
2. Split multi-page PDFs into batches of 2–5 pages and merge extracted JSON.
3. Strip old base64 image blocks from conversation history once OCR text is extracted.
4. Verify `client_max_body_size` on your own upstream reverse proxies.

## Diagnostic cURL
```bash
curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gemini-3.8-flash","messages":[{"role":"user","content":[{"type":"text","text":"Read invoice total"},{"type":"image_url","image_url":{"url":"https://example.com/invoice-small.webp"}}]}]}'
```

## Production Fix (Python)
```
import base64, io
from PIL import Image

def compress_for_vision(path: str, max_side: int = 1600) -> str:
    img = Image.open(path).convert("RGB")
    img.thumbnail((max_side, max_side))
    buf = io.BytesIO()
    img.save(buf, format="JPEG", quality=82, optimize=True)
    return "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode("ascii")
```

## FAQ
- **Does resizing to 1600px JPEG hurt OCR accuracy?**: No. 1500–2000px preserves crisp text and table digits while cutting payload size and image tokens sharply.
- **Is passing a public signed image URL smaller than base64?**: Yes. URL references keep the JSON payload tiny and eliminate base64 overhead.
- **Which Vision models are best for document OCR?**: `gemini-3.8-flash` for high-volume speed and `claude-sonnet-5-5` or `gpt-6.2-sol` for complex layouts.
