nautechsystems/nautilus_trader · error

Python on_instrument failed: {e}

Error message

Python on_instrument failed: {e}

What it means

Wraps any failure raised when dispatching a converted instrument into the Python actor's `on_instrument` handler. The Rust->Python conversion succeeded, but the Python `on_instrument` method raised, and Rust re-raises with the 'Python on_instrument failed' prefix.

Source

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

            .map_err(|e| anyhow::anyhow!("Python on_signal failed: {e}"))
    }

    fn on_queue_state(&mut self, event: &QueueStateChanged) -> anyhow::Result<()> {
        self.dispatch_on_queue_state(event)
            .map_err(|e| anyhow::anyhow!("Python on_queue_state failed: {e}"))
    }

    fn on_socket_state(&mut self, event: &SocketStateChanged) -> anyhow::Result<()> {
        self.dispatch_on_socket_state(event)
            .map_err(|e| anyhow::anyhow!("Python on_socket_state failed: {e}"))
    }

    fn on_instrument(&mut self, instrument: &InstrumentAny) -> anyhow::Result<()> {
        Python::attach(|py| {
            let py_instrument = instrument_any_to_pyobject(py, instrument.clone())
                .map_err(|e| anyhow::anyhow!("Failed to convert InstrumentAny to Python: {e}"))?;
            self.dispatch_on_instrument(py_instrument)
                .map_err(|e| anyhow::anyhow!("Python on_instrument failed: {e}"))
        })
    }

    fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {
        self.dispatch_on_quote(*quote)
            .map_err(|e| anyhow::anyhow!("Python on_quote failed: {e}"))
    }

    fn on_trade(&mut self, tick: &TradeTick) -> anyhow::Result<()> {
        self.dispatch_on_trade(*tick)
            .map_err(|e| anyhow::anyhow!("Python on_trade failed: {e}"))
    }

    fn on_bar(&mut self, bar: &Bar) -> anyhow::Result<()> {
        self.dispatch_on_bar(*bar)
            .map_err(|e| anyhow::anyhow!("Python on_bar failed: {e}"))
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect the wrapped `{e}` traceback and fix the exception inside `on_instrument`
  2. Confirm the signature `on_instrument(self, instrument)` accepting the converted instrument object
  3. Guard against instrument-type-specific attribute access (check the instrument class before reading specialized fields)
  4. Ensure actor state used in the handler is initialized in on_start before data can arrive

Example fix

// before
def on_instrument(self, instrument):
    strike = instrument.strike_price  # fails for non-options
// after
def on_instrument(self, instrument):
    if hasattr(instrument, 'strike_price'):
        strike = instrument.strike_price
Defensive patterns

Strategy: try-catch

Validate before calling

def _check_on_instrument(self, instrument):
    try:
        self.on_instrument(instrument)
    except Exception as e:
        self.log.error(f'on_instrument failed: {e!r}')

Type guard

def has_option_fields(instrument) -> bool:
    return hasattr(instrument, 'strike_price') and hasattr(instrument, 'expiry')

Try / catch

try:
    self.dispatch_on_instrument(py_instrument)
except Exception as e:
    self.log.error(f'Python on_instrument failed: {e}', exc_info=True)

Prevention

When it happens

Trigger: An instrument update arrives (after subscribe_instruments/subscribe_instrument) and the user's Python `on_instrument(self, instrument)` override raises an exception.

Common situations: Handler code assuming fields present for all asset classes (e.g. reading option-specific fields on FX instruments), wrong method arity, or state not initialized when the first instrument arrives during startup.

Related errors


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