{"record":{"id":"ef4bbaebf8a01130","repo":"nautechsystems/nautilus_trader","slug":"python-on-book-failed-e","errorCode":null,"errorMessage":"Python on_book failed: {e}","messagePattern":"Python on_book failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1128,"sourceCode":"\n    fn on_bar(&mut self, bar: &Bar) -> anyhow::Result<()> {\n        self.dispatch_on_bar(*bar)\n            .map_err(|e| anyhow::anyhow!(\"Python on_bar failed: {e}\"))\n    }\n\n    fn on_book_deltas(&mut self, deltas: &OrderBookDeltas) -> anyhow::Result<()> {\n        self.dispatch_on_book_deltas(deltas.clone())\n            .map_err(|e| anyhow::anyhow!(\"Python on_book_deltas failed: {e}\"))\n    }\n\n    fn on_book_depth(&mut self, depth: &OrderBookDepth10) -> anyhow::Result<()> {\n        self.dispatch_on_book_depth(depth)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book_depth failed: {e}\"))\n    }\n\n    fn on_book(&mut self, order_book: &OrderBook) -> anyhow::Result<()> {\n        self.dispatch_on_book(order_book)\n            .map_err(|e| anyhow::anyhow!(\"Python on_book failed: {e}\"))\n    }\n\n    fn on_mark_price(&mut self, mark_price: &MarkPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_mark_price(*mark_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_mark_price failed: {e}\"))\n    }\n\n    fn on_index_price(&mut self, index_price: &IndexPriceUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_index_price(*index_price)\n            .map_err(|e| anyhow::anyhow!(\"Python on_index_price failed: {e}\"))\n    }\n\n    fn on_funding_rate(&mut self, funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {\n        self.dispatch_on_funding_rate(*funding_rate)\n            .map_err(|e| anyhow::anyhow!(\"Python on_funding_rate failed: {e}\"))\n    }\n\n    fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {","sourceCodeStart":1110,"sourceCodeEnd":1146,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1110-L1146","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (Rust actor.rs propagates the wrapped exception)\nself.dispatch_on_book(order_book)\n    .map_err(|e| anyhow::anyhow!(\"Python on_book failed: {e}\"))\n\n// after (user Python hardening)\n# before\ndef on_book(self, book):\n    px = book.best_bid_price()\n    qty = book.best_bid_qty()\n    self.order(px, qty)\n\n# after\ndef on_book(self, book):\n    px = book.best_bid_price()\n    qty = book.best_bid_qty()\n    if px is None or qty is None or qty <= 0:\n        return\n    self.order(px, qty)","handlingStrategy":"try-catch","validationCode":"if book.best_bid_price() is None or book.best_ask_price() is None:\n    return  # skip empty/uninitialized book","typeGuard":"def is_valid_book(book):\n    bid, ask = book.best_bid_price(), book.best_ask_price()\n    return bid is not None and ask is not None and bid != 0 and ask != 0","tryCatchPattern":"def on_book(self, book):\n    try:\n        self._process_book(book)\n    except Exception as e:\n        self.log.error(f\"on_book handler failed: {e}\", exc_info=True)","preventionTips":["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"],"tags":["python","rust-bridge","callback-exception","actor"],"backgroundTag":"python-callback-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}