{"record":{"id":"8d3369c65c112dcc","repo":"nautechsystems/nautilus_trader","slug":"python-on-historical-data-failed-e","errorCode":null,"errorMessage":"Python on_historical_data failed: {e}","messagePattern":"Python on_historical_data failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1212,"sourceCode":"    }\n\n    #[cfg(feature = \"defi\")]\n    fn on_pool_flash(&mut self, flash: &PoolFlash) -> anyhow::Result<()> {\n        self.dispatch_on_pool_flash(flash.clone())\n            .map_err(|e| anyhow::anyhow!(\"Python on_pool_flash failed: {e}\"))\n    }\n\n    fn on_historical_data(&mut self, data: &dyn Any) -> anyhow::Result<()> {\n        Python::attach(|py| {\n            let py_data: Py<PyAny> = if let Some(custom_data) = data.downcast_ref::<CustomData>() {\n                Py::new(py, custom_data.clone())?.into_any()\n            } else if let Some(custom_data) = data.downcast_ref::<Vec<CustomData>>() {\n                custom_data.clone().into_py_any(py)?\n            } else {\n                anyhow::bail!(\"Failed to convert historical data to Python: unsupported type\");\n            };\n            self.dispatch_on_historical_data(py_data)\n                .map_err(|e| anyhow::anyhow!(\"Python on_historical_data failed: {e}\"))\n        })\n    }\n\n    fn on_historical_book_deltas(&mut self, deltas: &[OrderBookDelta]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_book_deltas(deltas.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_book_deltas failed: {e}\"))\n    }\n\n    fn on_historical_book_depth(&mut self, depths: &[OrderBookDepth10]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_book_depth(depths.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_book_depth failed: {e}\"))\n    }\n\n    fn on_historical_quotes(&mut self, quotes: &[QuoteTick]) -> anyhow::Result<()> {\n        self.dispatch_on_historical_quotes(quotes.to_vec())\n            .map_err(|e| anyhow::anyhow!(\"Python on_historical_quotes failed: {e}\"))\n    }\n","sourceCodeStart":1194,"sourceCodeEnd":1230,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1194-L1230","documentation":"Raised by `DataActor::on_historical_data` (crates/common/src/python/actor.rs:1212) when converting arbitrary historical data to a Python object or dispatching it to the actor's `on_historical_data` callback fails. Unlike the typed handlers, this path first tries downcasting to `CustomData`/`Vec<CustomData>` and explicitly bails with 'Failed to convert historical data to Python: unsupported type' for anything else; any Python exception in the callback is also wrapped as `Python on_historical_data failed: {e}`.","triggerScenarios":"Fires when historical data that is neither `CustomData` nor `Vec<CustomData>` (and not otherwise matched upstream) reaches `on_historical_data`, causing the anyhow bail; or when the Python `on_historical_data` callback raises; or when `Py::new`/`into_py_any` conversion of CustomData fails.","commonSituations":"Requesting historical data of a type not registered as CustomData (custom catalog data not registered with the type registry); passing Rust-native types through the generic path; handler code assuming a specific data shape; a CustomData pyclass lacking proper clone/conversion support.","solutions":["Read the wrapped `{e}`; if it is 'unsupported type', register your data type with the data catalog/type registry so it round-trips as CustomData.","Ensure custom data classes are decorated as pyo3-compatible pyclasses (NautilusCustomData/CustomData) and implement the required conversion methods.","Request historical data using concrete supported types (bars, quotes, trades) instead of pushing arbitrary Rust objects through the generic historical-data path.","Fix any exception raised inside the Python `on_historical_data` handler shown in the traceback."],"exampleFix":"# before: unregistered Rust struct pushed as historical data\nactor.request_data(MyRustSnapshot {})  # -> unsupported type\n\n# after: use a registered custom data type\nfrom nautilus_trader.model.custom import CustomData\nclass MySnapshot(CustomData):\n    ...\nactor.request_data(MySnapshot(...))","handlingStrategy":"validation","validationCode":"# before requesting, confirm the type is registered/convertible\nif not issubclass(MyData, CustomData):\n    raise TypeError(\"historical data must be a registered CustomData type\")\n","typeGuard":"def is_convertible_historical_data(data):\n    return isinstance(data, (CustomData, list)) and all(isinstance(d, CustomData) for d in data) if isinstance(data, list) else isinstance(data, CustomData)\n","tryCatchPattern":"try:\n    actor.on_historical_data(data)\nexcept Exception as e:\n    log.error(f\"on_historical_data failed: {e}\", exc_info=True)  # 'unsupported type' means register the data type\n","preventionTips":["Register custom data types with the data catalog/type registry before requesting them","Use concrete supported types (bars/quotes/trades) instead of pushing arbitrary objects through the generic path","Make custom data classes proper pyo3-compatible pyclasses","Read the chained cause: 'unsupported type' always points to registration, not handler bugs"],"tags":["python","historical-data","conversion","pyo3","rust"],"backgroundTag":"unsupported-operation","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}