{"record":{"id":"a0b3a9cadf37edf0","repo":"BoundaryML/baml","slug":"failed-to-encode-kind-message","errorCode":null,"errorMessage":"failed to encode {kind}: {message}","messagePattern":"failed to encode (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_artifact/src/lib.rs","lineNumber":144,"sourceCode":"    #[error(\"expected {expected} artifact, found {actual}\")]\n    WrongKind {\n        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>(","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_artifact/src/lib.rs#L126-L162","documentation":"`Error::Encode` wraps a Borsh serialization failure that occurred while encoding a value into an artifact envelope. The inner `message` is the underlying borsh error. This happens on the producer side, when a value cannot be serialized into bytes at all.","triggerScenarios":"Calling the crate's `encode::<T: BorshSerialize>(kind, value)` (baml_artifact/src/lib.rs:150) with a type whose Borsh serialization fails — e.g., data containing structures Borsh cannot handle or a custom Serialize impl that errors.","commonSituations":"A newly added field type in the IR/client payload lacks a correct BorshSerialize impl; extremely large or cyclic data structures; a custom type changed its serialization semantics and now fails mid-encode.","solutions":["Read the inner `message` to identify the field/type that failed to serialize.","Fix the BorshSerialize implementation (or the offending data type) for the failing field.","Ensure the payload type is plain data (no unserializable constructs) and bounded in size.","Update the baml_artifact/borsh dependency versions so serialization impls are consistent."],"exampleFix":"// before\nstruct Manifest { children: Rc<Vec<Node>> }  // Rc not Borsh-friendly\n// after\nstruct Manifest { children: Vec<Node> }  // plain owned data serializes cleanly","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Rust\nmatch baml_artifact::encode(ArtifactKind::Ir, &value) {\n    Ok(bytes) => write_artifact(bytes),\n    Err(baml_artifact::Error::Encode { message, .. }) => {\n        eprintln!(\"artifact encode failed: {message} — check BorshSerialize impls\");\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Keep serialized types plain-data (owned fields, no Rc/RefCell/cycles).","Add unit tests that round-trip encode/decode every payload type.","Keep borsh dependency versions consistent across the workspace."],"tags":["artifact","serialization","borsh","encode"],"backgroundTag":"json-marshal-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"}