BoundaryML/baml · error

PackageInterface artifact serialization into Vec is infallib

Error message

PackageInterface artifact serialization into Vec is infallible

What it means

When capturing package exports, the compiler serializes a `PackageInterface` artifact with `baml_artifact::encode` and `.expect`s success, asserting that encoding a PackageInterface artifact into a `Vec<u8>` cannot fail. The panic means the encoder returned an error for this artifact kind — typically an unsupported kind registration, an internal serialization fault, or an IO sink failure if the encode API can fail.

Source

Thrown at baml_language/crates/baml_compiler2_emit/src/lib.rs:420

            let root = file_package(db, *file).root;
            (spelling.of(root).clone(), root)
        })
        .collect();
    packages
        .into_iter()
        .map(|(package_name, package)| {
            let interface =
                baml_compiler2_hir_ty::package_interface::package_interface(db, package);
            // Runtime compilers already own the exact stdlib sources, so only
            // mountable packages need to carry a serialized compiler surface.
            let interface_blob = if package.kind(db) == baml_base::SourceRootKind::Stdlib {
                Vec::new()
            } else {
                baml_artifact::encode(
                    baml_artifact::ArtifactKind::PackageInterface,
                    &baml_compiler2_hir_ty::package_interface::export_interface(db, package),
                )
                .expect("PackageInterface artifact serialization into Vec is infallible")
            };
            let functions = interface
                .functions
                .iter()
                .flat_map(|(namespace, functions)| {
                    functions.iter().map(|(name, function)| {
                        (
                            bex_vm_types::types::LocalName {
                                namespace: namespace.clone(),
                                name: name.clone(),
                            },
                            external_call_target_name(spelling, &function.target),
                        )
                    })
                })
                .collect::<Vec<_>>();
            let exported_names = interface
                .types

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check that `baml_artifact::encode` supports `ArtifactKind::PackageInterface` and that the interface payload implements the required serialization trait
  2. Clean rebuild / clear stale build artifacts to remove version-skew between baml_artifact and the compiler crates
  3. Update all baml compiler crates together so artifact codecs match
  4. If encode can legitimately fail, propagate the error instead of expecting

Example fix

// before
.expect("PackageInterface artifact serialization into Vec is infallible")
// after
.map_err(|e| EmitError::ArtifactSerialization(e))?
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure encode supports the kind and payload before calling
debug_assert!(baml_artifact::supports_kind(ArtifactKind::PackageInterface));

Try / catch

std::panic::catch_unwind(|| capture_package_exports(db, package))
    .unwrap_or_else(|_| report_compiler_bug("PackageInterface serialization failed"));

Prevention

When it happens

Trigger: Calling `capture_package_exports` (via `generate_impl`) when `baml_artifact::encode` returns Err for `ArtifactKind::PackageInterface` — e.g., a codec mismatch between artifact versions or a non-in-memory sink error.

Common situations: Version skew between crates after an artifact-format change; registering/failing the PackageInterface encoder; corrupted or incompatible build cache inputs.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/832610f97f9e5252. Report an issue: GitHub.