nautechsystems/nautilus_trader · error · anyhow::Error

Python on_historical_data failed: {e}

Error message

Python on_historical_data failed: {e}

What it means

Raised when the strategy's Python `on_historical_data` handler throws after the Rust bridge successfully converted the payload to a Python object. `dispatch_on_historical_data` calls the Python method via PyO3 and any exception is re-wrapped with this anyhow message. The root cause is an unhandled exception in the user's Python handler.

Source

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

            .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())
            .map_err(|e| anyhow::anyhow!("Python on_historical_quotes failed: {e}"))
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the chained Python traceback in `e` and fix `on_historical_data`
  2. Validate the payload type/shape inside the handler (check `data.data_type` / attributes before use)
  3. Confirm the data type string registered for the request matches what the handler expects
  4. Add a try/except inside the handler for non-fatal warm-up data

Example fix

// before (Python)
def on_historical_data(self, data):
    rows = data.payload  # AttributeError: payload renamed
// after
def on_historical_data(self, data):
    rows = getattr(data, 'payload', None) or getattr(data, 'value', None)
Defensive patterns

Strategy: type-guard

Validate before calling

def payload_ok(data):
    return data is not None and (hasattr(data, 'value') or hasattr(data, 'payload'))

Type guard

def usable_historical_payload(data):
    return data is not None and hasattr(data, 'data_type') and data.value is not None

Try / catch

try:
    strategy.on_historical_data(data)
except Exception as e:
    strategy.log.error(f"on_historical_data failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: Historical data (CustomData payload) is delivered and the strategy's `on_historical_data(self, data)` raises — e.g. expecting a different payload shape, unpacking fields that don't exist, or errors in warm-up/backfill logic.

Common situations: Handlers written for one custom data payload version but receiving another; `.unwrap()`/attribute access on the custom data object; historical request configs changed between runs.

Related errors


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