nautechsystems/nautilus_trader · error

Failed to convert historical data to Python: unsupported typ

Error message

Failed to convert historical data to Python: unsupported type

What it means

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.

Source

Thrown at crates/trading/src/python/strategy.rs:1215

    fn on_option_greeks(&mut self, greeks: &OptionGreeks) -> anyhow::Result<()> {
        self.dispatch_on_option_greeks(*greeks)
            .map_err(|e| anyhow::anyhow!("Python on_option_greeks failed: {e}"))
    }

    fn on_option_chain(&mut self, slice: &OptionChainSlice) -> anyhow::Result<()> {
        self.dispatch_on_option_chain(slice)
            .map_err(|e| anyhow::anyhow!("Python on_option_chain failed: {e}"))
    }

    fn on_historical_data(&mut self, data: &dyn Any) -> anyhow::Result<()> {
        Python::attach(|py| {
            let py_data: Py<PyAny> = if let Some(custom_data) = data.downcast_ref::<CustomData>() {
                Py::new(py, custom_data.clone())?.into_any()
            } else if let Some(custom_data) = data.downcast_ref::<Vec<CustomData>>() {
                custom_data.clone().into_py_any(py)?
            } else {
                anyhow::bail!("Failed to convert historical data to Python: unsupported type");
            };
            self.dispatch_on_historical_data(py_data)
                .map_err(|e| anyhow::anyhow!("Python on_historical_data failed: {e}"))
        })
    }

    fn on_historical_book_deltas(&mut self, deltas: &[OrderBookDelta]) -> anyhow::Result<()> {
        self.dispatch_on_historical_book_deltas(deltas.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_book_deltas failed: {e}"))
    }

    fn on_historical_book_depth(&mut self, depths: &[OrderBookDepth10]) -> anyhow::Result<()> {
        self.dispatch_on_historical_book_depth(depths.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_book_depth failed: {e}"))
    }

    fn on_historical_quotes(&mut self, quotes: &[QuoteTick]) -> anyhow::Result<()> {
        self.dispatch_on_historical_quotes(quotes.to_vec())

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Wrap the payload in CustomData (or Vec<CustomData> for batches) before dispatching to on_historical_data.
  2. Register the custom data type with the Python bindings (pyo3 class and data registry) so downcast_ref::<CustomData> succeeds.
  3. Verify the data type you request/subscribe to matches a type supported by the Python conversion path.

Example fix

// before
self.dispatch_historical_data(data);

// after
let custom = CustomData::new(data, TSInit::default());
self.dispatch_historical_data(&custom); // now downcasts to CustomData
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(data, (CustomData, list)) or (isinstance(data, list) and not all(isinstance(d, CustomData) for d in data)):
    raise TypeError("historical data must be CustomData or list[CustomData]")

Type guard

def is_custom_data(d: object) -> bool:
    return isinstance(d, CustomData) or (isinstance(d, list) and all(isinstance(x, CustomData) for x in d))

Try / catch

try:
    strategy.on_historical_data(data)
except (ValueError, RuntimeError) as e:
    if "unsupported type" in str(e):
        data = CustomData(data, ts_init)
        strategy.on_historical_data(data)
    else:
        raise

Prevention

When it happens

Trigger: 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>.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/c7328b6c6a45a0d0. Report an issue: GitHub.