{"record":{"id":"c7328b6c6a45a0d0","repo":"nautechsystems/nautilus_trader","slug":"failed-to-convert-historical-data-to-python-unsup-c7328b","errorCode":null,"errorMessage":"Failed to convert historical data to Python: unsupported type","messagePattern":"Failed to convert historical data to Python: unsupported type","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/python/strategy.rs","lineNumber":1215,"sourceCode":"\n    fn on_option_greeks(&mut self, greeks: &OptionGreeks) -> anyhow::Result<()> {\n        self.dispatch_on_option_greeks(*greeks)\n            .map_err(|e| anyhow::anyhow!(\"Python on_option_greeks failed: {e}\"))\n    }\n\n    fn on_option_chain(&mut self, slice: &OptionChainSlice) -> anyhow::Result<()> {\n        self.dispatch_on_option_chain(slice)\n            .map_err(|e| anyhow::anyhow!(\"Python on_option_chain 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())","sourceCodeStart":1197,"sourceCodeEnd":1233,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/strategy.rs#L1197-L1233","documentation":"Raised in the Rust→Python bridge (Python::attach block) when on_historical_data dispatch receives a data payload that is neither a CustomData nor a Vec<CustomData>, so no Py<PyAny> conversion is possible. Only custom data types wrapped in CustomData are supported on this Rust-to-Python historical-data path; built-in or unregistered data types bail out.","triggerScenarios":"Dispatching historical data to a strategy's Python on_historical_data handler with a Rust-native data type (e.g. a raw Bar/QuoteTick struct) or a custom data struct not downcastable to CustomData/Vec<CustomData>.","commonSituations":"Requesting historical data whose Rust type is not routed through CustomData; a user-defined data class not registered with the PyO3 bindings/data registry; a version change that altered which data types cross the FFI boundary wrapped in CustomData.","solutions":["Wrap the payload in CustomData (or Vec<CustomData> for batches) before dispatching to on_historical_data.","Register the custom data type with the Python bindings (pyo3 class and data registry) so downcast_ref::<CustomData> succeeds.","Verify the data type you request/subscribe to matches a type supported by the Python conversion path."],"exampleFix":"// before\nself.dispatch_historical_data(data);\n\n// after\nlet custom = CustomData::new(data, TSInit::default());\nself.dispatch_historical_data(&custom); // now downcasts to CustomData","handlingStrategy":"type-guard","validationCode":"if not isinstance(data, (CustomData, list)) or (isinstance(data, list) and not all(isinstance(d, CustomData) for d in data)):\n    raise TypeError(\"historical data must be CustomData or list[CustomData]\")","typeGuard":"def is_custom_data(d: object) -> bool:\n    return isinstance(d, CustomData) or (isinstance(d, list) and all(isinstance(x, CustomData) for x in d))","tryCatchPattern":"try:\n    strategy.on_historical_data(data)\nexcept (ValueError, RuntimeError) as e:\n    if \"unsupported type\" in str(e):\n        data = CustomData(data, ts_init)\n        strategy.on_historical_data(data)\n    else:\n        raise","preventionTips":["Always route historical data through CustomData wrappers when crossing the Rust/Python boundary","Register every custom data class with the PyO3 bindings and data registry before use","Verify subscribed/requested data types match the handler's expected conversion path"],"tags":["rust","python","pyo3","historical-data","type-conversion"],"backgroundTag":"type-mismatch","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}