nautechsystems/nautilus_trader · error

Python on_option_chain failed: {e}

Error message

Python on_option_chain failed: {e}

What it means

This error wraps any Python exception raised inside the actor's `on_option_chain` callback when an `OptionChainSlice` is dispatched to the Python subclass. Note the slice is cloned before conversion (`slice.clone().into_py_any`), so the error typically arises either in that conversion or inside the user's Python handler. The Rust `PyDataActor` converts the `PyErr` to an anyhow error with this message.

Source

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

    fn on_instrument_status(&mut self, data: &InstrumentStatus) -> anyhow::Result<()> {
        self.dispatch_on_instrument_status(*data)
            .map_err(|e| anyhow::anyhow!("Python on_instrument_status failed: {e}"))
    }

    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}"))

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the appended Python traceback after 'Python on_option_chain failed:' to find the raising line.
  2. Handle empty slices (`if not slice.strikes: return`) before processing.
  3. Iterate the chain defensively rather than assuming fixed indices.
  4. Verify OptionChainSlice fields against the installed nautilus_trader version.

Example fix

// before
def on_option_chain(self, slice):
    best = slice.strikes[0]

// after
def on_option_chain(self, slice):
    if not slice.strikes:
        return
    best = slice.strikes[0]
Defensive patterns

Strategy: validation

Validate before calling

strikes = getattr(slice, "strikes", []) or []
if not strikes:
    return  # empty chain snapshot

Type guard

def is_nonempty_chain(chain_slice):
    return bool(getattr(chain_slice, "strikes", None))

Try / catch

def on_option_chain(self, slice):
    try:
        self._process_chain(slice)
    except Exception as e:
        self.log.error(f"on_option_chain failed: {e}", exc_info=True)

Prevention

When it happens

Trigger: Any exception raised by the user's `on_option_chain(self, slice)` override — e.g. indexing into an empty chain slice, calling methods on individual chain entries that don't exist, or a failure converting OptionChainSlice into a Python object.

Common situations: Options strategies processing chain snapshots at expiry when the chain is empty; handlers assuming every slice has strikes and quotes; version changes to OptionChainSlice structure.

Related errors


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