{"record":{"id":"c2a244c3ca5f4051","repo":"langchain-ai/deepagents","slug":"media-file-is-too-large-total-bytes-exceeds-ma","errorCode":null,"errorMessage":"media file is too large: {total} bytes exceeds {max_bytes}","messagePattern":"media file is too large: (.+?) bytes exceeds (.+?)","errorType":"exception","errorClass":"ChannelMediaError","httpStatus":null,"severity":"error","filePath":"libs/talon/deepagents_talon/channels/telegram.py","lineNumber":1241,"sourceCode":"        length = response.headers.get(\"content-length\")\n        if length is not None:\n            try:\n                expected = int(length)\n            except ValueError:\n                expected = None\n            if expected is not None and expected > max_bytes:\n                msg = f\"media file is too large: {expected} bytes exceeds {max_bytes}\"\n                raise ChannelMediaError(msg)\n\n        total = 0\n        with destination.open(\"wb\") as file:\n            while chunk := response.read(64 * 1024):\n                total += len(chunk)\n                if total > max_bytes:\n                    file.close()\n                    destination.unlink(missing_ok=True)\n                    msg = f\"media file is too large: {total} bytes exceeds {max_bytes}\"\n                    raise ChannelMediaError(msg)\n                file.write(chunk)\n    destination.chmod(0o600)\n\n\n# --- Offset persistence (ticket 23) ---\n\n\ndef _load_offset(offset_file: Path) -> int:\n    \"\"\"Load the persisted getUpdates offset from disk.\n\n    Args:\n        offset_file: Path to the offset state file.\n\n    Returns:\n        Persisted offset value, or ``0`` if the file is missing or corrupt.\n    \"\"\"\n    if not offset_file.is_file():\n        return 0","sourceCodeStart":1223,"sourceCodeEnd":1259,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/talon/deepagents_talon/channels/telegram.py#L1223-L1259","documentation":"While streaming the download in 64 KB chunks, `_download_file` tracks the running total and raises `ChannelMediaError` as soon as the bytes written would exceed `max_bytes`. It closes the file and unlinks the partial destination first, so no truncated file is left behind. This catches cases where the actual body exceeds the declared/checked size cap.","triggerScenarios":"Server sends more body bytes than `max_bytes` (missing/lying Content-Length, chunked transfer), or the Content-Length pre-check passed but the stream exceeds the cap mid-download.","commonSituations":"Proxies or CDNs that don't honor Content-Length; attacks/malformed responses with endless bodies; legitimate media slightly larger than a tight `max_media_bytes` cap that the header check missed due to encoding differences.","solutions":["Raise `max_media_bytes` in the channel config.","Investigate why the response exceeded its declared size (proxy compression, redirect to a different file) and fix the fetch path.","Wrap the download in `except ChannelMediaError` and clean up / notify, which the code already partially does by unlinking the partial file."],"exampleFix":"# before\nmax_bytes = 1024 * 1024  # 1 MB — too small for typical photos\n# after\nmax_bytes = 20 * 1024 * 1024  # 20 MB","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    path = _download_file(response, destination, max_bytes)\nexcept ChannelMediaError:\n    logging.warning(\"stream exceeded cap of %d bytes; partial file removed\", max_bytes)\n    # destination already unlinked by _download_file","preventionTips":["Set max_media_bytes generously enough that legitimate streams never trip it","Treat mid-stream aborts as a signal to investigate proxies/CDN behavior","Always unlink partial files after failed downloads to avoid truncated media","Add tests with streams larger than the cap (see test_download_file_aborts_when_stream_exceeds_cap)"],"tags":["telegram","media","size-limit","download","streaming"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}