nautechsystems/nautilus_trader · error

Python on_historical_book_deltas failed: {e}

Error message

Python on_historical_book_deltas failed: {e}

What it means

Raised by `DataActor::on_historical_book_deltas` (crates/common/src/python/actor.rs:1218) when dispatching a slice of `OrderBookDeltas` to the Python actor's `on_historical_book_deltas` callback fails. The dispatch clones the deltas into a Vec and calls the Python method via pyo3; Python exceptions in the handler or conversion failures of the deltas list become `Python on_historical_book_deltas failed: {e}`.

Source

Thrown at crates/common/src/python/actor.rs:1218

    }

    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}"))
    }

    fn on_historical_trades(&mut self, trades: &[TradeTick]) -> anyhow::Result<()> {
        self.dispatch_on_historical_trades(trades.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_trades failed: {e}"))
    }

    fn on_historical_funding_rates(

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect the wrapped `{e}` cause (Python traceback) and fix the code in `on_historical_book_deltas`.
  2. Verify the Python actor implements `on_historical_book_deltas(self, deltas)`.
  3. Handle empty delta lists and book-status/reset records defensively before applying deltas.
  4. Confirm delta field access matches OrderBookDelta in your installed nautilus version.

Example fix

# before: assumes every record is an update
def on_historical_book_deltas(self, deltas):
    for d in deltas:
        self.book.apply(d)

# after
def on_historical_book_deltas(self, deltas):
    if not deltas:
        return
    for d in deltas:
        if d.action == BookAction.CLEAR:
            self.book.clear(d.instrument_id)
        else:
            self.book.apply(d)
Defensive patterns

Strategy: try-catch

Validate before calling

assert callable(getattr(actor, 'on_historical_book_deltas', None)), "actor must implement on_historical_book_deltas"

Type guard

def has_book_deltas_handler(obj):
    return callable(getattr(obj, 'on_historical_book_deltas', None))

Try / catch

try:
    actor.on_historical_book_deltas(deltas)
except Exception as e:
    log.error(f"on_historical_book_deltas failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: Occurs when historical order-book deltas are delivered (e.g. from a historical request) and the Python callback raises an exception, the Python object lacks a callable `on_historical_book_deltas`, or the deltas fail to convert into Python objects.

Common situations: Handlers rebuilding book state from deltas raising on malformed/empty delta batches; treating historical deltas like live incremental updates; version drift in OrderBookDelta fields; missing handler after subscribing to historical book data.

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


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