{"record":{"id":"dfb576bad6bee92f","repo":"nautechsystems/nautilus_trader","slug":"failed-to-convert-instrumentany-to-python-e-dfb576","errorCode":null,"errorMessage":"Failed to convert InstrumentAny to Python: {e}","messagePattern":"Failed to convert InstrumentAny to Python: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/python/strategy.rs","lineNumber":1137,"sourceCode":"    fn on_signal(&mut self, signal: &Signal) -> anyhow::Result<()> {\n        self.dispatch_on_signal(signal)\n            .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}\"))","sourceCodeStart":1119,"sourceCodeEnd":1155,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/python/strategy.rs#L1119-L1155","documentation":"Converting an `InstrumentAny` (a Rust enum of instrument definitions) into a Python object failed inside `on_instrument`, before the Python callback was even invoked. `instrument_any_to_pyobject` maps each instrument variant to its pyo3-exposed Python type; a conversion error (unsupported variant or an inner conversion failure) is wrapped at strategy.rs:1137 as `Failed to convert InstrumentAny to Python: {e}`.","triggerScenarios":"An instrument definition event is delivered to the strategy's `on_instrument`; the variant cannot be converted, typically when a new/less-common instrument type (or an instrument carrying fields the Python bindings don't yet support) flows through, or a downstream pyo3 conversion of a field errors.","commonSituations":"Subscribing to instruments from an adapter that yields an instrument kind whose Python wrapper is missing in the installed version; version mismatch between core crates and the Python package; exotic instrument definitions (unusual option/Spread types) not covered by the conversion.","solutions":["Read `{e}` to see which instrument type/field failed conversion.","Upgrade the Python package and core to matching versions so the instrument variant has a Python binding.","Filter or skip the offending instrument type in your subscription/universe selection until support exists.","If persistent, report/patch `instrument_any_to_pyobject` to cover the missing variant."],"exampleFix":"// before (subscribing to every instrument in the venue)\nfor inst in venue.instruments:\n    self.subscribe_instrument(inst)\n\n// after\nfrom nautilus_trader.model.instruments import Instrument  # ensure bindings exist\nfor inst in venue.instruments:\n    if isinstance(inst, SUPPORTED_INSTRUMENT_TYPES):\n        self.subscribe_instrument(inst)\n    else:\n        self.log.warning(f\"skipping unsupported instrument type: {type(inst)}\")","handlingStrategy":"type-guard","validationCode":"from nautilus_trader.model.instruments import (\n    Equity, ForexPair, CryptoPerpetual, FuturesContract, OptionsContract,\n)\nSUPPORTED = (Equity, ForexPair, CryptoPerpetual, FuturesContract, OptionsContract)\ndef convertible(instrument):\n    return isinstance(instrument, SUPPORTED)","typeGuard":"def is_python_convertible(instrument) -> bool:\n    return isinstance(instrument, SUPPORTED_INSTRUMENT_TYPES)","tryCatchPattern":"try:\n    self.subscribe_instrument(inst)\nexcept Exception as e:\n    self.log.warning(f\"instrument {inst.id} not convertible to Python, skipping: {e}\")","preventionTips":["Keep core Rust crates and the Python package on matching versions.","Restrict subscriptions to instrument types with Python bindings in your installed version.","Filter the universe before subscribing; log and skip unsupported kinds.","Check release notes for newly added instrument types before upgrading."],"tags":["rust","python","conversion","instrument","pyo3"],"backgroundTag":"incompatible-source-type","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"}