nautechsystems/nautilus_trader · error

Python on_data failed: {e}

Error message

Python on_data failed: {e}

What it means

The strategy's Python-level `on_data` callback raised an exception while processing custom data. The Rust hook clones the `CustomData` into a Python object (`Py::new`) and calls `dispatch_on_data`; a failure in either creating the Py object or in the Python handler is wrapped at strategy.rs:1115 as `Python on_data failed: {e}`.

Source

Thrown at crates/trading/src/python/strategy.rs:1115

    }

    fn on_load(&mut self, state: IndexMap<String, Vec<u8>>) -> anyhow::Result<()> {
        self.dispatch_on_load(&state)
            .map_err(|e| anyhow::anyhow!("Python on_load failed: {e}"))
    }

    fn on_time_event(&mut self, event: &TimeEvent) -> anyhow::Result<()> {
        route_time_event(self, event);
        self.dispatch_on_time_event(event)
            .map_err(|e| anyhow::anyhow!("Python on_time_event failed: {e}"))
    }

    #[allow(unused_variables)]
    fn on_data(&mut self, data: &CustomData) -> anyhow::Result<()> {
        Python::attach(|py| {
            let py_data: Py<PyAny> = Py::new(py, data.clone())?.into_any();
            self.dispatch_on_data(py_data)
                .map_err(|e| anyhow::anyhow!("Python on_data failed: {e}"))
        })
    }

    fn on_signal(&mut self, signal: &Signal) -> anyhow::Result<()> {
        self.dispatch_on_signal(signal)
            .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}"))
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read `{e}`: if it mentions object creation, check the custom data type's Python class registration; otherwise fix the handler parsing code.
  2. Validate/defensively parse the custom data fields in `on_data` before use.
  3. Confirm the published data schema matches what `on_data` unpacks (producer and consumer versions agree).
  4. Wrap handler logic in try/except with logging so one bad message doesn't stop processing subsequent data.

Example fix

// before
def on_data(self, data):
    price, qty = data.value.split(",")  # ValueError when upstream adds a field

// after
def on_data(self, data):
    parts = data.value.split(",")
    if len(parts) < 2:
        self.log.warning(f"malformed custom data: {data.value!r}")
        return
    price, qty = parts[0], parts[1]
Defensive patterns

Strategy: validation

Validate before calling

def validate_custom_data(data, required_fields):
    missing = [f for f in required_fields if not hasattr(data, f)]
    if missing:
        raise AttributeError(f"custom data missing fields: {missing}")

Type guard

def is_parseable(data):
    return hasattr(data, "value") and isinstance(data.value, (str, bytes)) and len(data.value) > 0

Try / catch

def on_data(self, data):
    try:
        self._handle(data)
    except Exception as e:
        self.log.error(f"custom data handling failed for {data!r}: {e}")

Prevention

When it happens

Trigger: Custom data (e.g. from a user-defined data type subscribed via `subscribe_data`) arrives and is delivered to `on_data`; the Python method raises while unpacking/parsing the payload, or `Py::new` fails if the data type is not correctly registered as a pyo3-compatible class.

Common situations: Parsing raw custom-data payloads with a schema that changed upstream; assuming fields exist on the custom object; incorrect msgspec/dataclass definition of the custom data type causing conversion failures; exceptions in ML inference or external calls inside `on_data`.

Related errors


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