{"record":{"id":"30add6dfc88f2c42","repo":"nautechsystems/nautilus_trader","slug":"canonical-result-field-key-must-be-an-array","errorCode":null,"errorMessage":"canonical result field '{key}' must be an array","messagePattern":"canonical result field '(.+?)' must be an array","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/result.rs","lineNumber":337,"sourceCode":"        \"position_snapshots\",\n        \"positions\",\n        \"run\",\n        \"schema\",\n        \"statistics\",\n        \"summary\",\n    ];\n    validate_fields(object, &fields, \"canonical result\")?;\n\n    for key in [\n        \"accounts\",\n        \"diagnostics\",\n        \"fills\",\n        \"orders\",\n        \"portfolio_snapshots\",\n        \"position_snapshots\",\n        \"positions\",\n    ] {\n        anyhow::ensure!(\n            object.get(key).is_some_and(Value::is_array),\n            \"canonical result field '{key}' must be an array\"\n        );\n    }\n    validate_components(object.get(\"components\").expect(\"validated field\"))?;\n    validate_diagnostics(object.get(\"diagnostics\").expect(\"validated field\"))?;\n    validate_run(object.get(\"run\").expect(\"validated field\"))?;\n    validate_statistics(object.get(\"statistics\").expect(\"validated field\"))?;\n    let summary = object\n        .get(\"summary\")\n        .and_then(Value::as_object)\n        .ok_or_else(|| anyhow::anyhow!(\"canonical result summary must be an object\"))?;\n    anyhow::ensure!(\n        summary.values().all(Value::is_string),\n        \"canonical result summary values must be strings\"\n    );\n    Ok(())\n}","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/result.rs#L319-L355","documentation":"`validate_document` requires each canonical list field (`accounts`, `components`, `diagnostics` items, `fills`, `orders`, `portfolio_snapshots`, `position_snapshots`, `positions`) to be present and to be a JSON array. If any of these fields is missing, null, an object, or another non-array type, this error names the offending key.","triggerScenarios":"Loading a result document where one of the required array fields was dropped by a partial export, set to `null` (e.g. by a 'no data' serializer), or replaced by an object/mapping.","commonSituations":"Custom result writers emitting `{\"orders\": null}` instead of `[]`; post-processing scripts that delete empty fields to shrink files; merging results and losing a field.","solutions":["Ensure every required field is present as an array — use `[]` for empty, never `null` or `{}`","Regenerate the document with the canonical writer so all fields are emitted","Fix any post-processing that deletes or replaces these fields","Check the field name in the error message to find the exact key to repair"],"exampleFix":"// before\n{\"schema\":\"...\",\"orders\":null,\"fills\":[]}\n// after\n{\"schema\":\"...\",\"orders\":[],\"fills\":[]}","handlingStrategy":"type-guard","validationCode":"REQUIRED = [\"accounts\",\"components\",\"fills\",\"orders\",\"portfolio_snapshots\",\"position_snapshots\",\"positions\"]\nassert all(isinstance(doc.get(k), list) for k in REQUIRED), \"missing/non-array result field\"","typeGuard":"fn has_required_arrays(doc: &serde_json::Value) -> bool {\n    [\"accounts\",\"components\",\"fills\",\"orders\",\"portfolio_snapshots\",\"position_snapshots\",\"positions\"]\n        .iter().all(|k| doc.get(*k).is_some_and(serde_json::Value::is_array))\n}","tryCatchPattern":"match BacktestResult::from_slice(&bytes) {\n    Err(e) if e.to_string().contains(\"must be an array\") => {\n        eprintln!(\"required array field missing or wrong type: {e:#}\");\n    }\n    Err(e) => return Err(e),\n    Ok(r) => r,\n}","preventionTips":["Emit `[]` for empty collections, never null","Disable 'drop empty fields' options in post-processing/compression tools","Run validate_document-style checks on any custom writer's output before saving"],"tags":["json","schema","backtest","validation"],"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"}