nautechsystems/nautilus_trader · error
Python on_book failed: {e}
Error message
Python on_book failed: {e} What it means
This error wraps any Python exception raised inside the actor's `on_book` callback when an `OrderBook` snapshot is delivered to the Python subclass. The Rust `PyDataActor` calls `call_method1(py, "on_book", ...)` and converts the resulting `PyErr` into an anyhow error with this message. The original Python traceback (including line number and exception type) is appended after the `{e}` placeholder, so the actual bug is in user Python code, not the Rust core.
Source
Thrown at crates/common/src/python/actor.rs:1128
fn on_bar(&mut self, bar: &Bar) -> anyhow::Result<()> {
self.dispatch_on_bar(*bar)
.map_err(|e| anyhow::anyhow!("Python on_bar failed: {e}"))
}
fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {
self.dispatch_on_book_deltas(deltas.clone())
.map_err(|e| anyhow::anyhow!("Python on_book_deltas failed: {e}"))
}
fn on_book_depth(&mut self, depth: &OrderBookDepth10) -> anyhow::Result<()> {
self.dispatch_on_book_depth(depth)
.map_err(|e| anyhow::anyhow!("Python on_book_depth failed: {e}"))
}
fn on_book(&mut self, order_book: &OrderBook) -> anyhow::Result<()> {
self.dispatch_on_book(order_book)
.map_err(|e| anyhow::anyhow!("Python on_book failed: {e}"))
}
fn on_mark_price(&mut self, mark_price: &MarkPriceUpdate) -> anyhow::Result<()> {
self.dispatch_on_mark_price(*mark_price)
.map_err(|e| anyhow::anyhow!("Python on_mark_price failed: {e}"))
}
fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {
self.dispatch_on_index_price(*index_price)
.map_err(|e| anyhow::anyhow!("Python on_index_price failed: {e}"))
}
fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {
self.dispatch_on_funding_rate(*funding_rate)
.map_err(|e| anyhow::anyhow!("Python on_funding_rate failed: {e}"))
}
fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {View on GitHub (pinned to 18893faf8b)
Solutions
- Read the full traceback appended after 'Python on_book failed:' — it names the exact line in your on_book override that raised.
- Add a defensive check inside on_book for missing data (e.g. book.best_bid_price() returning None/NaN) before use.
- Call super().on_book(book) at the start of your override if you changed the method signature or forgot it.
- Reproduce locally with the same data by running the actor in a sandbox/backtest before live deployment.
Example fix
// before (Rust actor.rs propagates the wrapped exception)
self.dispatch_on_book(order_book)
.map_err(|e| anyhow::anyhow!("Python on_book failed: {e}"))
// after (user Python hardening)
# before
def on_book(self, book):
px = book.best_bid_price()
qty = book.best_bid_qty()
self.order(px, qty)
# after
def on_book(self, book):
px = book.best_bid_price()
qty = book.best_bid_qty()
if px is None or qty is None or qty <= 0:
return
self.order(px, qty) Defensive patterns
Strategy: try-catch
Validate before calling
if book.best_bid_price() is None or book.best_ask_price() is None:
return # skip empty/uninitialized book Type guard
def is_valid_book(book):
bid, ask = book.best_bid_price(), book.best_ask_price()
return bid is not None and ask is not None and bid != 0 and ask != 0 Try / catch
def on_book(self, book):
try:
self._process_book(book)
except Exception as e:
self.log.error(f"on_book handler failed: {e}", exc_info=True) Prevention
- Always guard against None/empty book sides before trading logic
- Log the raw event at debug level so failures can be replayed
- Call super().on_book(book) if overriding the base handler
- Backtest with the same data feeds before going live
When it happens
Trigger: Any exception raised by the user's `on_book(self, book)` override, or during `into_py_any` conversion of the OrderBook to a Python object. E.g. accessing `book.instrument_id` and calling methods on None, dividing by a zero book.best_bid_price(), indexing into an empty bids list, or a typo'd helper method invoked inside on_book.
Common situations: Live trading with a new data handler added to an existing actor; upgrading nautilus_trader where OrderBook Python bindings changed shape (e.g. attribute renamed), so attribute access in the handler now raises AttributeError; handling the first snapshot before book initialization assumptions hold.
Related errors
- Python on_mark_price failed: {e}
- Python on_index_price failed: {e}
- Python on_funding_rate failed: {e}
- Python on_instrument_status failed: {e}
- Python on_instrument_close failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/ef4bbaebf8a01130.
Report an issue: GitHub.