{"record":{"id":"7814200d41d572d5","repo":"nautechsystems/nautilus_trader","slug":"failed-to-convert-to-python-e","errorCode":null,"errorMessage":"Failed to convert to Python: {e}","messagePattern":"Failed to convert to Python: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/python/data/mod.rs","lineNumber":382,"sourceCode":"            .call_method1(\"from_json\", (py_dict,))\n            .map_err(|e| anyhow::anyhow!(\"Failed to call from_json: {e}\"))?;\n\n        let wrapper = PythonCustomDataWrapper::new(py, &instance)\n            .map_err(|e| anyhow::anyhow!(\"Failed to create wrapper: {e}\"))?;\n\n        Ok(Arc::new(wrapper) as Arc<dyn crate::data::CustomDataTrait>)\n    })\n}\n\n/// Encodes `CustomData` items to `RecordBatch` via Python `encode_record_batch_py`.\n#[allow(unsafe_code)]\n#[cfg(all(feature = \"python\", feature = \"arrow\"))]\nfn py_encode_custom_data_to_record_batch(\n    items: &[std::sync::Arc<dyn crate::data::CustomDataTrait>],\n) -> Result<arrow::record_batch::RecordBatch, anyhow::Error> {\n    pyo3::Python::attach(|py| {\n        let py_items: Result<Vec<_>, _> = items.iter().map(|item| item.to_pyobject(py)).collect();\n        let py_items = py_items.map_err(|e| anyhow::anyhow!(\"Failed to convert to Python: {e}\"))?;\n        let py_list = pyo3::types::PyList::new(py, &py_items)\n            .map_err(|e| anyhow::anyhow!(\"Failed to create list: {e}\"))?;\n\n        let first = items\n            .first()\n            .ok_or_else(|| anyhow::anyhow!(\"No items to encode\"))?;\n        let first_py = first.to_pyobject(py)?;\n\n        if first_py\n            .bind(py)\n            .hasattr(\"encode_record_batch_py\")\n            .unwrap_or(false)\n        {\n            let py_batch = first_py\n                .bind(py)\n                .call_method1(\"encode_record_batch_py\", (py_list,))\n                .map_err(|e| anyhow::anyhow!(\"Failed to call encode_record_batch_py: {e}\"))?;\n","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/python/data/mod.rs#L364-L400","documentation":"When encoding custom data items to an Arrow RecordBatch, each Rust-side item (`Arc<dyn CustomDataTrait>`) is first converted to its Python representation via `to_pyobject`. This error is raised if any item fails that Rust→Python conversion; the underlying pyo3/Python error is embedded in `{e}`. It aborts the whole batch encode — no partial output is produced.","triggerScenarios":"Calling the Arrow encode path for registered custom data (via `register_custom_data_class`-registered types) when one or more items in the slice fail `to_pyobject(py)` — typically because the wrapper holds a dead/invalid Python reference or the item's conversion method raises.","commonSituations":"Items were decoded from a stream whose Python objects were garbage-collected or the interpreter state changed; a custom wrapper's `to_pyobject` implementation raises; mixing items whose underlying Python class no longer matches the registered class after hot-reload.","solutions":["Read the embedded `{e}` message to identify which item and why the conversion failed.","Ensure all items in the batch are live, valid instances of the registered custom data class before encoding.","Re-create the items (decode again from source) rather than reusing wrappers across interpreter lifecycles.","Encode items in smaller batches to isolate the failing item."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"assert all(isinstance(x, MyClass) for x in items), \"all items must be live registered-class instances\"","typeGuard":null,"tryCatchPattern":"try:\n    batch = encode_to_record_batch(items)\nexcept Exception as e:\n    logger.error(f\"item -> Python conversion failed: {e}\")\n    raise","preventionTips":["Do not reuse wrappers across interpreter lifecycles","Re-decode items from source rather than caching stale Python-backed objects","Encode homogeneous batches of one registered class"],"tags":["python","pyo3","arrow","custom-data","conversion"],"backgroundTag":"type-mismatch","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"}