{"record":{"id":"c7672cbeee92c7ea","repo":"BoundaryML/baml","slug":"failed-to-decode-kind-message","errorCode":null,"errorMessage":"failed to decode {kind}: {message}","messagePattern":"failed to decode (.+?): (.+?)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_artifact/src/lib.rs","lineNumber":146,"sourceCode":"        expected: ArtifactKind,\n        actual: ArtifactKind,\n    },\n    #[error(\"{kind} artifact failed integrity validation\")]\n    InvalidPayloadHash { kind: ArtifactKind },\n    #[error(\n        \"{kind}: toolchain {artifact_fingerprint} / format {artifact_format}; this runtime: {runtime_fingerprint} / format {runtime_format} — {remediation}\"\n    )]\n    Incompatible {\n        kind: ArtifactKind,\n        artifact_fingerprint: String,\n        artifact_format: u32,\n        runtime_fingerprint: &'static str,\n        runtime_format: u32,\n        remediation: &'static str,\n    },\n    #[error(\"failed to encode {kind}: {message}\")]\n    Encode { kind: ArtifactKind, message: String },\n    #[error(\"failed to decode {kind}: {message}\")]\n    Decode { kind: ArtifactKind, message: String },\n}\n\n/// Serialize `value` and wrap it in a versioned artifact envelope.\npub fn encode<T: BorshSerialize>(kind: ArtifactKind, value: &T) -> Result<Vec<u8>, Error> {\n    let payload = borsh::to_vec(value).map_err(|error| Error::Encode {\n        kind,\n        message: error.to_string(),\n    })?;\n    encode_payload(kind, &payload)\n}\n\n/// Encode a value with an explicit artifact format for cross-crate skew tests.\n#[cfg(feature = \"test-support\")]\n#[doc(hidden)]\npub fn encode_with_format_for_test<T: BorshSerialize>(\n    artifact_format: u32,\n    kind: ArtifactKind,","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_artifact/src/lib.rs#L128-L164","documentation":"`Error::Decode` wraps a Borsh deserialization failure that occurred after the artifact envelope (magic, header, hash) validated successfully but the payload bytes could not be interpreted as the expected type `T`. The inner `message` describes the deserialization problem.","triggerScenarios":"Calling the crate's decode path with a type `T: BorshDeserialize` whose in-memory layout expectation does not match the serialized payload — e.g., decoding with a different struct version than was encoded.","commonSituations":"A struct gained/lost fields between toolchain versions while the artifact format version wasn't bumped; decoding the payload with the wrong type parameter; artifacts produced by a mismatched generator reaching an updated runtime.","solutions":["Regenerate the artifact with the toolchain matching the runtime so the payload schema matches the decode type.","Check the inner `message` to find which field offsets/types diverge and align the Rust type with the writer's schema.","Ensure the format-version constant is bumped whenever the serialized struct changes, so stale artifacts are rejected with Incompatible instead of Decode errors.","Verify you are decoding with the same type `T` the artifact was encoded with."],"exampleFix":"// before\nlet manifest: ManifestV2 = decode(ManifestKind::Ir, &bytes)?; // payload written as V1\n// after\nlet manifest: ManifestV1 = decode(ManifestKind::Ir, &bytes)?; // decode with the writer's schema","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Rust\nmatch baml_artifact::decode::<Manifest>(&bytes) {\n    Ok(manifest) => use(manifest),\n    Err(baml_artifact::Error::Decode { message, .. }) => {\n        eprintln!(\"artifact payload unreadable: {message} — regenerate with matching toolchain\");\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Bump the artifact format version whenever serialized structs change.","Decode with the same type the artifact was encoded with.","Add round-trip encode/decode tests to the pipeline.","Regenerate artifacts after any toolchain or schema upgrade."],"tags":["artifact","serialization","borsh","decode"],"backgroundTag":"json-decode-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}