{"record":{"id":"5dc7b2459360530f","repo":"NousResearch/hermes-agent","slug":"image-at-url-exceeds-max-bytes-1024-1024","errorCode":null,"errorMessage":"Image at {url} exceeds {max_bytes // (1024 * 1024)}MB cap; refusing to cache.","messagePattern":"Image at (.+?) exceeds (.+?)MB cap; refusing to cache\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"agent/image_gen_provider.py","lineNumber":332,"sourceCode":"        extension = \"png\"\n\n    ts = datetime.datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n    short = uuid.uuid4().hex[:8]\n    path = _images_cache_dir() / f\"{prefix}_{ts}_{short}.{extension}\"\n\n    bytes_written = 0\n    with path.open(\"wb\") as fh:\n        for chunk in response.iter_content(chunk_size=64 * 1024):\n            if not chunk:\n                continue\n            bytes_written += len(chunk)\n            if bytes_written > max_bytes:\n                fh.close()\n                try:\n                    path.unlink()\n                except OSError:\n                    pass\n                raise ValueError(\n                    f\"Image at {url} exceeds {max_bytes // (1024 * 1024)}MB cap; refusing to cache.\"\n                )\n            fh.write(chunk)\n\n    if bytes_written == 0:\n        try:\n            path.unlink()\n        except OSError:\n            pass\n        raise ValueError(f\"Image at {url} returned 0 bytes; refusing to cache.\")\n\n    return path\n\n\ndef success_response(\n    *,\n    image: str,\n    model: str,","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/NousResearch/hermes-agent/blob/c896c09c42910c584c4c7d2325b58c14713ea42c/agent/image_gen_provider.py#L314-L350","documentation":"While downloading an image to cache, the streamed body exceeded max_bytes before completion (bytes_written > max_bytes after a 64KB chunk). The partially-written file is closed and unlinked, and this ValueError refuses to cache the oversized image — a resource-exhaustion guard in agent/image_gen_provider.py so a hostile or misbehaving URL cannot fill the disk.","triggerScenarios":"Downloading from a URL whose Content-Length/body exceeds the configured cap (max_bytes, reported in the message as MB) — enforced incrementally per chunk, so it triggers even when the header understates the size.","commonSituations":"Image-generation provider returning unexpectedly large outputs; a URL returning a video or archive instead of an image; max_bytes configured too small for legitimately high-resolution renders.","solutions":["Regenerate at a smaller resolution / lower quality so the output fits under the cap.","Raise the cache size cap in the image-gen provider configuration if the cap is set below legitimate output sizes.","Verify the URL actually returns the intended image (curl -I) — oversized responses often mean a wrong content type.","Host oversized artifacts externally and pass a link instead of caching them."],"exampleFix":"# before — provider emits 20MB PNG, cap is 10MB\nimage = provider.generate(prompt=\"...\", size=\"2048x2048\")\n# after — smaller render fits the cap\nimage = provider.generate(prompt=\"...\", size=\"1024x1024\")","handlingStrategy":"validation","validationCode":"import urllib.request\n\ndef image_within_cap(url: str, max_bytes: int) -> bool:\n    size = int(urllib.request.urlopen(urllib.request.Request(\n        url, method='HEAD')).headers.get('Content-Length') or 0)\n    return 0 < size <= max_bytes  # note: enforcement is per-chunk, headers can lie","typeGuard":null,"tryCatchPattern":"try:\n    path = cache_image(url, max_bytes=max_bytes)\nexcept ValueError as e:\n    if 'exceeds' in str(e) and 'cap' in str(e):\n        # regenerate smaller or raise the configured cap; partial file is auto-removed\n        ...","preventionTips":["Generate images at resolutions whose outputs fit under the cache cap","Raise the size cap in provider config if legitimate outputs are large","Verify URLs return the expected image type before caching"],"tags":["image-gen","download","size-limit","caching"],"backgroundTag":null,"analyzedSha":"c896c09c42910c584c4c7d2325b58c14713ea42c","analyzedAt":"2026-08-14T17:18:01.089Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}