nautechsystems/nautilus_trader · error

Python on_historical_book_depth failed: {e}

Error message

Python on_historical_book_depth failed: {e}

What it means

Raised by `DataActor::on_historical_book_depth` (crates/common/src/python/actor.rs:1223) when dispatching a slice of `OrderBookDepth10` snapshots to the Python actor's `on_historical_book_depth` callback fails. The dispatch copies the depths and invokes the Python method via pyo3 `call_method1`; exceptions raised in the Python handler or conversion failures of the depth list are wrapped as `Python on_historical_book_depth failed: {e}`.

Source

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

                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(
        &mut self,
        funding_rates: &[FundingRateUpdate],
    ) -> anyhow::Result<()> {
        self.dispatch_on_historical_funding_rates(funding_rates.to_vec())
            .map_err(|e| anyhow::anyhow!("Python on_historical_funding_rates failed: {e}"))

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the wrapped `{e}` cause for the original Python exception and fix `on_historical_book_depth`.
  2. Ensure the Python actor defines `on_historical_book_depth(self, depth)`.
  3. Handle None/partial depth levels instead of assuming all 10 levels are populated.
  4. Recheck depth field access against OrderBookDepth10 for your nautilus version.

Example fix

# before: crashes on thin books with None levels
def on_historical_book_depth(self, depth):
    best = depth.bids[0].price

# after
def on_historical_book_depth(self, depth):
    levels = [lvl for lvl in depth.bids if lvl is not None]
    if not levels:
        return
    best = levels[0].price
Defensive patterns

Strategy: try-catch

Validate before calling

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

Type guard

def has_book_depth_handler(obj):
    return callable(getattr(obj, 'on_historical_book_depth', None))

Try / catch

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

Prevention

When it happens

Trigger: Fires when historical L2/L3 depth snapshots are delivered to the actor and the Python callback raises, the Python instance has no callable `on_historical_book_depth`, or the `OrderBookDepth10` entries fail Python conversion.

Common situations: Handlers iterating depth levels with assumptions about fixed 10-level shape (None levels on thin books) causing TypeErrors; mixing up depth vs deltas handling; missing handler implementation; schema changes across nautilus versions.

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/a6574b2a5004e1e1. Report an issue: GitHub.