{"record":{"id":"2d0ebb28ac23d590","repo":"nautechsystems/nautilus_trader","slug":"python-simulationmodule-pre-process-failed-e","errorCode":null,"errorMessage":"Python SimulationModule.pre_process failed: {e}","messagePattern":"Python SimulationModule\\.pre_process failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/python/modules.rs","lineNumber":276,"sourceCode":"impl PythonSimulationModule {\n    #[must_use]\n    pub const fn new(obj: Py<PyAny>) -> Self {\n        Self { obj }\n    }\n\n    pub(crate) fn clone_ref(&self, py: Python<'_>) -> Py<PyAny> {\n        self.obj.clone_ref(py)\n    }\n}\n\nimpl SimulationModule for PythonSimulationModule {\n    fn pre_process(&self, data: &Data) -> anyhow::Result<()> {\n        Python::attach(|py| -> anyhow::Result<()> {\n            let data = data_to_pyobject(py, data.clone())?;\n            self.obj.bind(py).call_method1(\"pre_process\", (data,))?;\n            Ok(())\n        })\n        .map_err(|e| anyhow::anyhow!(\"Python SimulationModule.pre_process failed: {e}\"))\n    }\n\n    fn process(\n        &self,\n        ts_now: nautilus_core::UnixNanos,\n        ctx: &ExchangeContext,\n    ) -> anyhow::Result<SimulationModuleResult> {\n        Python::attach(|py| -> anyhow::Result<SimulationModuleResult> {\n            let context = Py::new(py, PySimulationModuleContext::from_exchange(ctx))?;\n            let adjustments = self\n                .obj\n                .bind(py)\n                .call_method1(\"process\", (ts_now.as_u64(), context))?\n                .extract::<Option<Vec<Money>>>()?;\n            Ok(adjustments.map_or(\n                SimulationModuleResult::NotReady,\n                SimulationModuleResult::Completed,\n            ))","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/python/modules.rs#L258-L294","documentation":"A Python-side SimulationModule's pre_process method raised an exception while the backtest exchange pre-processed incoming data. The Rust wrapper converts the resulting PyErr into an anyhow error prefixed with this message.","triggerScenarios":"The Python object implementing SimulationModule.pre_process(data) raises any exception during the backtest data pipeline (buggy user code, bad data conversion, wrong data type handling in the module).","commonSituations":"User-authored simulation module assumptions broken by the data payload type; None handling errors; unhandled market-data formats in custom fill models or fee modules.","solutions":["Read the chained Python traceback in the error text and fix the exception in the Python module's pre_process method.","Add defensive handling for the data types your module can receive (use isinstance checks on the converted data object).","Test the module in isolation by calling pre_process with sample data before running the full backtest."],"exampleFix":"# before\ndef pre_process(self, data):\n    return self.state[data.venue]  # KeyError propagates\n# after\ndef pre_process(self, data):\n    return self.state.get(data.venue, None)","handlingStrategy":"try-catch","validationCode":"module.pre_process(sample_data)  # smoke-test with representative data before the backtest\n","typeGuard":null,"tryCatchPattern":"class SafeModule(SimulationModule):\n    def pre_process(self, data):\n        try:\n            return self._pre_process(data)\n        except Exception:\n            logging.exception(\"SimulationModule.pre_process failed\")\n            raise\n","preventionTips":["Unit-test custom SimulationModules with representative data payloads before running full backtests.","Handle None/unknown data types defensively inside pre_process.","Log the chained Python traceback: the anyhow message embeds the original PyErr."],"tags":["python","backtest","simulation-module","callback"],"backgroundTag":"upstream-api-error","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}