{"record":{"id":"fcfce0814d09c35d","repo":"nautechsystems/nautilus_trader","slug":"customdata-serialization-failed-e","errorCode":null,"errorMessage":"CustomData serialization failed: {e}","messagePattern":"CustomData serialization failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/redis/cache.rs","lineNumber":508,"sourceCode":"    /// # Errors\n    ///\n    /// Returns an error if the command cannot be sent to the background task channel.\n    pub fn insert(&self, key: String, payload: Option<Vec<Bytes>>) -> anyhow::Result<()> {\n        let op = DatabaseCommand::new(DatabaseOperation::Insert, key, payload);\n        match self.tx.send(op) {\n            Ok(()) => Ok(()),\n            Err(e) => anyhow::bail!(\"{FAILED_TX_CHANNEL}: {e}\"),\n        }\n    }\n\n    /// Stores custom data in Redis (key format: `custom:<ts_init_020>:<uuid>`, value: full JSON).\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if serialization fails or the insert command cannot be sent.\n    pub fn add_custom_data(&self, data: &CustomData) -> anyhow::Result<()> {\n        let json_bytes = serde_json::to_vec(data)\n            .map_err(|e| anyhow::anyhow!(\"CustomData serialization failed: {e}\"))?;\n        let ts_init = data.ts_init().as_u64();\n        let key = format!(\n            \"{CUSTOM}{REDIS_DELIMITER}{:020}{REDIS_DELIMITER}{}\",\n            ts_init,\n            UUID4::new()\n        );\n        self.insert(key, Some(vec![Bytes::from(json_bytes)]))\n    }\n\n    /// Sends an update command for `key` with optional `payload` to Redis via the background task.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the command cannot be sent to the background task channel.\n    pub fn update(&mut self, key: String, payload: Option<Vec<Bytes>>) -> anyhow::Result<()> {\n        let op = DatabaseCommand::new(DatabaseOperation::Update, key, payload);\n        match self.tx.send(op) {\n            Ok(()) => Ok(()),","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/redis/cache.rs#L490-L526","documentation":"add_custom_data serializes a CustomData record to JSON bytes before writing it to the Redis-backed cache. If serde_json cannot serialize the record, the failure is wrapped in this anyhow error. It surfaces when the payload contains types with no JSON representation (e.g. non-string map keys) or internal serialization bugs.","triggerScenarios":"Calling add_custom_data (directly or via the Python wrapper py_add_custom_data) with a CustomData whose serde serialization fails — typically data containing maps with non-string keys or unsupported field types.","commonSituations":"Users feed custom market data or metadata into the cache containing structures serde_json cannot encode; usually introduced by a custom Data subtype with exotic field types added in a recent change.","solutions":["Inspect the serde message in the error to find the field that cannot be serialized and change its type (e.g. use string keys in maps).","Ensure the CustomData type derives Serialize and all nested types have JSON-compatible representations.","Upgrade nautilus_trader if a recent version had a serialization bug affecting your data type.","If the type cannot be JSON-serialized, convert it to a supported representation before passing to add_custom_data."],"exampleFix":"// before\nlet data = MyData::new(my_map_with_int_keys);\ncache.add_custom_data(&data)?; // fails: key must be a string\n// after\nlet data = MyData::new(my_map_with_string_keys);\ncache.add_custom_data(&data)?;","handlingStrategy":"validation","validationCode":"let json = serde_json::to_vec(&data).map_err(|e| format!(\"custom data not JSON-serializable: {e}\"))?;","typeGuard":"fn is_json_serializable<T: serde::Serialize>(v: &T) -> bool { serde_json::to_vec(v).is_ok() }","tryCatchPattern":null,"preventionTips":["Use only JSON-compatible field types (string map keys, no non-string keys) in custom Data types.","Unit-test serialization of every custom data type before adding it to the cache.","Derive Serialize/Deserialize and keep nested types JSON-friendly."],"tags":["rust","redis","serialization","anyhow"],"backgroundTag":"json-marshal-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"}