{"record":{"id":"cf00ae625d28e7c7","repo":"run-llama/llama_index","slug":"resolve-image-returned-zero-bytes","errorCode":null,"errorMessage":"resolve_image returned zero bytes","messagePattern":"resolve_image returned zero bytes","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/base/llms/types.py","lineNumber":391,"sourceCode":"        \"\"\"\n        data_buffer = (\n            self.image\n            if isinstance(self.image, IOBase)\n            else resolve_binary(\n                raw_bytes=self.image,\n                path=self.path,\n                url=str(self.url) if self.url else None,\n                as_base64=as_base64,\n            )\n        )\n\n        # Check size by seeking to end and getting position\n        data_buffer.seek(0, 2)  # Seek to end\n        size = data_buffer.tell()\n        data_buffer.seek(0)  # Reset to beginning\n\n        if size == 0:\n            raise ValueError(\"resolve_image returned zero bytes\")\n        return data_buffer\n\n    def inline_url(self) -> str:\n        b64 = self.resolve_image(as_base64=True)\n        b64_str = b64.read().decode(\"utf-8\")\n        return f\"data:{self.image_mimetype};base64,{b64_str}\"\n\n    async def aestimate_tokens(self, *args: Any, **kwargs: Any) -> int:\n        \"\"\"\n        Many APIs measure images differently. Here, we take a large estimate.\n\n        This is based on a 2048 x 1536 image using OpenAI.\n\n        TODO: In the future, LLMs should be able to count their own tokens.\n        \"\"\"\n        try:\n            self.resolve_image()\n            return 2125","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/base/llms/types.py#L373-L409","documentation":"Raised by ImageBlock.resolve_image(as_base64=...) when the resolved data buffer contains zero bytes after resolving from raw bytes, path, or URL. The block guards against sending empty image payloads to APIs that would otherwise fail cryptically.","triggerScenarios":"ImageBlock with an empty image=b\"\" value; a path pointing at a 0-byte file; a URL that returns an empty response body (200 with empty content); a failed download silently yielding b''.","commonSituations":"Upstream write producing empty files; signed URLs expiring and returning empty bodies; placeholder records with empty image bytes in a database; network interception returning empty 200s.","solutions":["Check the source before constructing the block: os.path.getsize(path) > 0, len(image_bytes) > 0.","Fetch and validate the URL content-length/bytes yourself before assigning url=, or catch this error and retry the download.","Fix the upstream producer writing empty files/records."],"exampleFix":"# before\nblock = ImageBlock(path=\"downloaded.png\")  # file is 0 bytes\ndata = block.resolve_image(as_base64=True).read()\n\n# after\nimport os\nif os.path.getsize(\"downloaded.png\") == 0:\n    raise RuntimeError(\"empty download, refetching\")\nblock = ImageBlock(path=\"downloaded.png\")","handlingStrategy":"validation","validationCode":"import os\nok = (image_bytes and len(image_bytes) > 0) or (path and os.path.getsize(path) > 0)\nif not ok:\n    raise ValueError(\"image source is empty\")","typeGuard":null,"tryCatchPattern":"try:\n    buf = block.resolve_image(as_base64=True)\nexcept ValueError as e:\n    if \"zero bytes\" in str(e):\n        refetch_or_skip()  # re-download or skip record\n    else:\n        raise","preventionTips":["Verify file size / byte length before building ImageBlock.","For URL sources, validate the downloaded payload is non-empty."],"tags":["llama-index","image","empty-input","multimodal"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}