nautechsystems/nautilus_trader · error
Python on_block failed: {e}
Error message
Python on_block failed: {e} What it means
This error wraps any Python exception raised inside the actor's `on_block` callback when a DeFi `Block` event is dispatched to the Python subclass (only compiled with the `defi` feature). The Rust `PyDataActor` clones the block, converts it via `into_py_any`, and calls the Python method via `call_method1`; a resulting `PyErr` becomes an anyhow error with this message. The exception originates in the user's Python override or the Rust-to-Python conversion.
Source
Thrown at crates/common/src/python/actor.rs:1169
fn on_instrument_close(&mut self, update: &InstrumentClose) -> anyhow::Result<()> {
self.dispatch_on_instrument_close(*update)
.map_err(|e| anyhow::anyhow!("Python on_instrument_close failed: {e}"))
}
fn on_option_greeks(&mut self, greeks: &OptionGreeks) -> anyhow::Result<()> {
self.dispatch_on_option_greeks(*greeks)
.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.clone())
.map_err(|e| anyhow::anyhow!("Python on_option_chain failed: {e}"))
}
#[cfg(feature = "defi")]
fn on_block(&mut self, block: &Block) -> anyhow::Result<()> {
self.dispatch_on_block(block.clone())
.map_err(|e| anyhow::anyhow!("Python on_block failed: {e}"))
}
#[cfg(feature = "defi")]
fn on_pool(&mut self, pool: &Pool) -> anyhow::Result<()> {
self.dispatch_on_pool(pool.clone())
.map_err(|e| anyhow::anyhow!("Python on_pool failed: {e}"))
}
#[cfg(feature = "defi")]
fn on_pool_swap(&mut self, swap: &PoolSwap) -> anyhow::Result<()> {
self.dispatch_on_pool_swap(swap.clone())
.map_err(|e| anyhow::anyhow!("Python on_pool_swap failed: {e}"))
}
#[cfg(feature = "defi")]
fn on_pool_liquidity_update(&mut self, update: &PoolLiquidityUpdate) -> anyhow::Result<()> {
self.dispatch_on_pool_liquidity_update(update.clone())
.map_err(|e| anyhow::anyhow!("Python on_pool_liquidity_update failed: {e}"))View on GitHub (pinned to 18893faf8b)
Solutions
- Read the appended Python traceback after 'Python on_block failed:' to locate the raising line.
- Confirm the Block field types in Python (str vs bytes for hashes) before string operations.
- Enable/rebuild with the defi feature consistently so bindings match your Python stubs.
- Add unit tests feeding synthetic Block objects to the handler.
Example fix
// before
def on_block(self, block):
if block.hash.startswith("0x"):
self.recent.append(block.hash)
// after
def on_block(self, block):
h = block.hash.decode() if isinstance(block.hash, bytes) else block.hash
if h.startswith("0x"):
self.recent.append(h) Defensive patterns
Strategy: type-guard
Validate before calling
h = block.hash
if isinstance(h, bytes):
h = h.hex()
if not h:
return Type guard
def is_valid_block(block):
h = getattr(block, "hash", None)
return isinstance(h, (str, bytes)) and len(h) > 0 Try / catch
def on_block(self, block):
try:
self._monitor_block(block)
except Exception as e:
self.log.error(f"on_block failed: {e}", exc_info=True) Prevention
- Normalize hash fields (str vs bytes) at the handler entry point
- Keep the defi feature enabled consistently across build and Python stubs
- Test DeFi handlers with synthetic Block objects
- Review Block bindings after nautilus_trader upgrades
When it happens
Trigger: Any exception raised by the user's `on_block(self, block)` override — e.g. parsing block.number/block_hash with wrong assumptions, state keyed by a hash type that changed in conversion, or a raising Rust-to-Python conversion of Block.
Common situations: DeFi/on-chain strategies monitoring blocks whose handlers were written against an older Block binding; using string methods on a field that arrives as bytes (or vice versa) after conversion.
Related errors
- Python on_pool failed: {e}
- Python on_book failed: {e}
- Python on_mark_price failed: {e}
- Python on_index_price failed: {e}
- Python on_funding_rate failed: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/77afae03dd17f2f9.
Report an issue: GitHub.