{"record":{"id":"1d10c61dcb1c0ac6","repo":"nautechsystems/nautilus_trader","slug":"python-on-instrument-failed-e","errorCode":null,"errorMessage":"Python on_instrument failed: {e}","messagePattern":"Python on_instrument failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/python/actor.rs","lineNumber":1097,"sourceCode":"            .map_err(|e| anyhow::anyhow!(\"Python on_signal failed: {e}\"))\n    }\n\n    fn on_queue_state(&mut self, event: &QueueStateChanged) -> anyhow::Result<()> {\n        self.dispatch_on_queue_state(event)\n            .map_err(|e| anyhow::anyhow!(\"Python on_queue_state failed: {e}\"))\n    }\n\n    fn on_socket_state(&mut self, event: &SocketStateChanged) -> anyhow::Result<()> {\n        self.dispatch_on_socket_state(event)\n            .map_err(|e| anyhow::anyhow!(\"Python on_socket_state failed: {e}\"))\n    }\n\n    fn on_instrument(&mut self, instrument: &InstrumentAny) -> anyhow::Result<()> {\n        Python::attach(|py| {\n            let py_instrument = instrument_any_to_pyobject(py, instrument.clone())\n                .map_err(|e| anyhow::anyhow!(\"Failed to convert InstrumentAny to Python: {e}\"))?;\n            self.dispatch_on_instrument(py_instrument)\n                .map_err(|e| anyhow::anyhow!(\"Python on_instrument failed: {e}\"))\n        })\n    }\n\n    fn on_quote(&mut self, quote: &QuoteTick) -> anyhow::Result<()> {\n        self.dispatch_on_quote(*quote)\n            .map_err(|e| anyhow::anyhow!(\"Python on_quote failed: {e}\"))\n    }\n\n    fn on_trade(&mut self, tick: &TradeTick) -> anyhow::Result<()> {\n        self.dispatch_on_trade(*tick)\n            .map_err(|e| anyhow::anyhow!(\"Python on_trade failed: {e}\"))\n    }\n\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","sourceCodeStart":1079,"sourceCodeEnd":1115,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/python/actor.rs#L1079-L1115","documentation":"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.","triggerScenarios":"An instrument update arrives (after subscribe_instruments/subscribe_instrument) and the user's Python `on_instrument(self, instrument)` override raises an exception.","commonSituations":"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.","solutions":["Inspect the wrapped `{e}` traceback and fix the exception inside `on_instrument`","Confirm the signature `on_instrument(self, instrument)` accepting the converted instrument object","Guard against instrument-type-specific attribute access (check the instrument class before reading specialized fields)","Ensure actor state used in the handler is initialized in on_start before data can arrive"],"exampleFix":"// before\ndef on_instrument(self, instrument):\n    strike = instrument.strike_price  # fails for non-options\n// after\ndef on_instrument(self, instrument):\n    if hasattr(instrument, 'strike_price'):\n        strike = instrument.strike_price","handlingStrategy":"try-catch","validationCode":"def _check_on_instrument(self, instrument):\n    try:\n        self.on_instrument(instrument)\n    except Exception as e:\n        self.log.error(f'on_instrument failed: {e!r}')","typeGuard":"def has_option_fields(instrument) -> bool:\n    return hasattr(instrument, 'strike_price') and hasattr(instrument, 'expiry')","tryCatchPattern":"try:\n    self.dispatch_on_instrument(py_instrument)\nexcept Exception as e:\n    self.log.error(f'Python on_instrument failed: {e}', exc_info=True)","preventionTips":["Branch on instrument type before reading asset-class-specific fields","Initialize actor state before subscribing to instruments","Test on_instrument with instruments from every subscribed venue"],"tags":["python","actor","callback","instrument"],"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-14T05:17:10.506Z"}