{"record":{"id":"7812950304c7c874","repo":"nautechsystems/nautilus_trader","slug":"failed-to-call-encode-record-batch-py-e","errorCode":null,"errorMessage":"Failed to call encode_record_batch_py: {e}","messagePattern":"Failed to call encode_record_batch_py: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/model/src/python/data/mod.rs","lineNumber":399,"sourceCode":"        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\n            let mut ffi_array = arrow::ffi::FFI_ArrowArray::empty();\n            let mut ffi_schema = arrow::ffi::FFI_ArrowSchema::empty();\n\n            py_batch.call_method1(\n                \"_export_to_c\",\n                (\n                    (&raw mut ffi_array as usize),\n                    (&raw mut ffi_schema as usize),\n                ),\n            )?;\n\n            let schema = std::sync::Arc::new(arrow::datatypes::Schema::try_from(&ffi_schema)?);\n            let struct_array_data = unsafe {\n                arrow::ffi::from_ffi_and_data_type(\n                    ffi_array,\n                    arrow::datatypes::DataType::Struct(schema.fields().clone()),\n                )?","sourceCodeStart":381,"sourceCodeEnd":417,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/python/data/mod.rs#L381-L417","documentation":"When the first custom data item exposes the Python method `encode_record_batch_py`, the encoder calls it with the list of Python items to produce a RecordBatch-like object. This error wraps any Python exception raised during that call, including errors raised inside the user class's `encode_record_batch_py` implementation. The Python traceback text is embedded in `{e}`.","triggerScenarios":"Encoding registered custom data to Arrow when the Python class's `encode_record_batch_py(items)` raises — e.g. schema mismatch between items, missing columns, wrong item types in the list, or a bug in the user's encode implementation.","commonSituations":"User-defined data class registered with a hand-written `encode_record_batch_py` that assumes all items share identical fields; mixed item versions after a schema change so old and new objects are encoded together; typo in column names vs the declared Arrow schema.","solutions":["Read the embedded Python traceback and fix the exception inside `encode_record_batch_py`.","Ensure every item in the list is an instance of the same class with the same schema/fields.","Test `MyClass.encode_record_batch_py([instance])` directly in Python with a representative item.","If relying on generated encoders, regenerate them after any change to the data class fields."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"assert len({type(x) for x in items}) == 1, \"mixed item classes in batch\"\nbatch_out = MyClass.encode_record_batch_py(items)  # smoke-test in Python first","typeGuard":null,"tryCatchPattern":"try:\n    batch = encode_to_record_batch(items)\nexcept Exception as e:\n    logger.error(f\"encode_record_batch_py raised: {e}\")\n    raise","preventionTips":["Keep encode_record_batch_py defensive against nulls and missing columns","Never mix schema versions of the same class in one batch","Round-trip test encode/decode after every field change"],"tags":["python","arrow","custom-data","encoding"],"backgroundTag":"schema-validation-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"}