{"record":{"id":"69916d8e8053b8dc","repo":"reflex-dev/reflex","slug":"invalid-data-type-type-data-for-download-use","errorCode":null,"errorMessage":"Invalid data type {type(data)} for download. Use `str` or `bytes`.","messagePattern":"Invalid data type (.+?) for download\\. Use `str` or `bytes`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/event/__init__.py","lineNumber":1776,"sourceCode":"            is_data_url = (data.js_type() == \"string\") & (\n                data.to(str).startswith(\"data:\")\n            )\n\n            # If it's a data: URI, use it as is, otherwise convert the Var to JSON in a data: URI.\n            url = cond(\n                is_data_url,\n                data.to(str),\n                f\"data:{mime_type},\" + data.to_string(),\n            )\n        elif isinstance(data, bytes):\n            if mime_type is None:\n                mime_type = \"application/octet-stream\"\n            # Caller provided bytes, so base64 encode it as a data: URI.\n            b64_data = b64encode(data).decode(\"utf-8\")\n            url = f\"data:{mime_type};base64,\" + b64_data\n        else:\n            msg = f\"Invalid data type {type(data)} for download. Use `str` or `bytes`.\"\n            raise ValueError(msg)\n\n    return server_side(\n        \"_download\",\n        inspect.signature(download),\n        url=url,\n        filename=filename,\n    )\n\n\ndef call_script(\n    javascript_code: str | Var[str],\n    callback: \"EventType[Any] | None\" = None,\n) -> EventSpec:\n    \"\"\"Create an event handler that executes arbitrary javascript code.\n\n    Args:\n        javascript_code: The code to execute.\n        callback: EventHandler that will receive the result of evaluating the javascript code.","sourceCodeStart":1758,"sourceCodeEnd":1794,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/event/__init__.py#L1758-L1794","documentation":"The data argument of rx.download must be str or bytes so it can be base64-encoded into a data: URI; other types (int, dict, numpy arrays, objects) cannot be encoded.","triggerScenarios":"rx.download(data=123), rx.download(data={'a': 1}), or passing a Path/bytearray/object.","commonSituations":"Passing a JSON dict expecting it to be serialized, or a pathlib.Path instead of its contents; numeric data from computations.","solutions":["Encode as str/bytes: str(value).encode() or json.dumps(dict).encode()","For a Path, read it first: data=path.read_bytes()","For text, pass the str directly (mime_type defaults to text/plain)"],"exampleFix":"# before\nrx.download(data={'a': 1})\n# after\nimport json\nrx.download(data=json.dumps({'a': 1}), filename='data.json')","handlingStrategy":"type-guard","validationCode":"def ok_download_data(d) -> bool:\n    return d is None or isinstance(d, (str, bytes))","typeGuard":"def is_download_data(d) -> TypeGuard[str | bytes | None]: return d is None or isinstance(d, (str, bytes))","tryCatchPattern":null,"preventionTips":["json.dumps()/str().encode() before passing data; read Path objects with read_bytes()"],"tags":["reflex","download","type-error"],"backgroundTag":"invalid-argument-type","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}