BoundaryML/baml · error

Media types cannot be default-constructed; they require a…

Error message

Media types cannot be default-constructed; they require a runtime

What it means

Media types (Audio/Video/Image wrappers in baml's raw_objects) implement `Default` only to satisfy trait bounds, and the implementation is `unreachable!()`. A runtime value is required to construct these types, so calling `.default()` (directly or via `unwrap_or_default`, `..Default::default()`, serde defaults, etc.) panics. This is a deliberate library invariant, not an expected failure mode.

Solutions

  1. Do not default-construct media types; obtain a value via the runtime/FFI decode path (`BamlDecode::baml_decode`)
  2. Replace `Default::default()` usage with explicit construction from a runtime-provided value
  3. Restructure so media fields are `Option<T>` and default the Option, not the media type

Example fix

// before
let media: Audio = Default::default();
// after
let media: Audio = holder.into_audio()?; // constructed via BamlDecode with a runtime value
Defensive patterns

Strategy: type-guard

Type guard

fn has_runtime_value(m: &Option<Audio>) -> bool { m.is_some() }

Try / catch

// unreachable!() panic; avoid by construction
let audio = audio_opt.unwrap_or_else(|| obtain_audio_from_runtime(&holder));

Prevention

When it happens

Trigger: Any call to `Media::default()` / `<$name as Default>::default()` for a media type: explicit `Default::default()`, struct update syntax, `unwrap_or_default()`, or frameworks that require `Default` (e.g. some deserializers filling missing fields).

Common situations: Using `..Default::default()` to fill struct literals containing media fields; wiring media types into generic containers that demand `Default`; relying on serde field defaults.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at languages/rust/baml/src/raw_objects/media.rs:86

            pub fn as_url(&self) -> Option<String> {
                self.raw.call_method("as_url", ())
            }

            /// Get as base64 (if applicable)
            pub fn as_base64(&self) -> Option<String> {
                self.raw.call_method("as_base64", ())
            }
        }

        impl std::fmt::Debug for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.debug_struct(stringify!($name)).finish_non_exhaustive()
            }
        }

        impl Default for $name {
            fn default() -> Self {
                unreachable!("Media types cannot be default-constructed; they require a runtime")
            }
        }

        impl $crate::BamlDecode for $name {
            fn baml_decode(
                _holder: &$crate::__internal::CffiValueHolder
            ) -> ::core::result::Result<Self, $crate::BamlError> {
                // Media types come as ObjectValue which requires runtime context to decode.
                // This path shouldn't be hit in normal usage - media decoding goes through
                // the runtime's object handle mechanism instead.
                Err($crate::BamlError::internal(
                    concat!("Cannot decode ", stringify!($name), " without runtime context")
                ))
            }
        }

        impl serde::Serialize for $name {
            /// See https://github.com/BoundaryML/baml/blob/canary/engine/language_client_python/src/types/media_repr.rs

View on GitHub (pinned to bd85ce9dee)