{"record":{"id":"b8d9f71a86af1c19","repo":"langchain-ai/deepagents","slug":"media-file-is-too-large-expected-bytes-exceeds","errorCode":null,"errorMessage":"media file is too large: {expected} 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":1231,"sourceCode":"\n    Raises:\n        ChannelMediaError: If the remote file exceeds `max_bytes`.\n    \"\"\"\n    destination.parent.mkdir(mode=0o700, parents=True, exist_ok=True)\n    request = urllib.request.Request(url)  # noqa: S310  # URL constructed from Bot API config.\n    with urllib.request.urlopen(  # noqa: S310  # download URL from Telegram API.\n        request,\n        timeout=timeout,\n    ) as response:\n        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:","sourceCodeStart":1213,"sourceCodeEnd":1249,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/talon/deepagents_talon/channels/telegram.py#L1213-L1249","documentation":"`_download_file` validates the `Content-Length` header before streaming: if the server-declared length (`expected`) exceeds `max_bytes`, it raises `ChannelMediaError` immediately, avoiding a doomed download. This is the pre-flight half of the media size cap, complementing the streaming check.","triggerScenarios":"Downloading a Telegram file whose HTTP response declares a Content-Length larger than `max_bytes`; called from `_download_file` (exercised by `test_download_file_aborts_when_stream_exceeds_cap`).","commonSituations":"Large videos whose Telegram CDN response reports a big Content-Length; misconfigured `max_media_bytes` smaller than legitimate media; server responses with gzip/transfer quirks producing an inflated declared length.","solutions":["Increase `max_media_bytes` in the Talon config to a value covering your expected media sizes.","Skip or reject the attachment upstream by checking file size (see the `get_file` result) before invoking the download.","Catch `ChannelMediaError` around downloads and surface a user-friendly 'file too large' message."],"exampleFix":"# before\ncfg.max_media_bytes = 10 * 1024 * 1024\n# after\ncfg.max_media_bytes = 50 * 1024 * 1024","handlingStrategy":"try-catch","validationCode":"import urllib.request\ndef declared_size_ok(url: str, max_bytes: int) -> bool:\n    req = urllib.request.Request(url, method=\"HEAD\")\n    with urllib.request.urlopen(req) as resp:\n        length = resp.headers.get(\"Content-Length\")\n        return length is None or int(length) <= max_bytes","typeGuard":null,"tryCatchPattern":"try:\n    _download_file(response, destination, max_bytes)\nexcept ChannelMediaError:\n    destination.unlink(missing_ok=True)\n    logging.warning(\"media download exceeded %d bytes; aborted\", max_bytes)","preventionTips":["Keep max_media_bytes consistent across all size checks (metadata and download)","Clean up partial files on any download failure","Prefer rejecting oversized media upstream (metadata check) to avoid wasted bandwidth","Account for transfer encoding when interpreting Content-Length"],"tags":["telegram","media","size-limit","download","http"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}