{"record":{"id":"0c6bc8887bf19cc2","repo":"BoundaryML/baml","slug":"media-types-cannot-be-default-constructed-they-require-a","errorCode":null,"errorMessage":"Media types cannot be default-constructed; they require a runtime","messagePattern":"Media types cannot be default-constructed; they require a runtime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"languages/rust/baml/src/raw_objects/media.rs","lineNumber":86,"sourceCode":"            pub fn as_url(&self) -> Option<String> {\n                self.raw.call_method(\"as_url\", ())\n            }\n\n            /// Get as base64 (if applicable)\n            pub fn as_base64(&self) -> Option<String> {\n                self.raw.call_method(\"as_base64\", ())\n            }\n        }\n\n        impl std::fmt::Debug for $name {\n            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n                f.debug_struct(stringify!($name)).finish_non_exhaustive()\n            }\n        }\n\n        impl Default for $name {\n            fn default() -> Self {\n                unreachable!(\"Media types cannot be default-constructed; they require a runtime\")\n            }\n        }\n\n        impl $crate::BamlDecode for $name {\n            fn baml_decode(\n                _holder: &$crate::__internal::CffiValueHolder\n            ) -> ::core::result::Result<Self, $crate::BamlError> {\n                // Media types come as ObjectValue which requires runtime context to decode.\n                // This path shouldn't be hit in normal usage - media decoding goes through\n                // the runtime's object handle mechanism instead.\n                Err($crate::BamlError::internal(\n                    concat!(\"Cannot decode \", stringify!($name), \" without runtime context\")\n                ))\n            }\n        }\n\n        impl serde::Serialize for $name {\n            /// See https://github.com/BoundaryML/baml/blob/canary/engine/language_client_python/src/types/media_repr.rs","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/languages/rust/baml/src/raw_objects/media.rs#L68-L104","documentation":"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.","triggerScenarios":"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).","commonSituations":"Using `..Default::default()` to fill struct literals containing media fields; wiring media types into generic containers that demand `Default`; relying on serde field defaults.","solutions":["Do not default-construct media types; obtain a value via the runtime/FFI decode path (`BamlDecode::baml_decode`)","Replace `Default::default()` usage with explicit construction from a runtime-provided value","Restructure so media fields are `Option<T>` and default the Option, not the media type"],"exampleFix":"// before\nlet media: Audio = Default::default();\n// after\nlet media: Audio = holder.into_audio()?; // constructed via BamlDecode with a runtime value","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"fn has_runtime_value(m: &Option<Audio>) -> bool { m.is_some() }","tryCatchPattern":"// unreachable!() panic; avoid by construction\nlet audio = audio_opt.unwrap_or_else(|| obtain_audio_from_runtime(&holder));","preventionTips":["Never call Default::default() on media types","Use Option<Media> fields instead of relying on Default","Construct media values only through the runtime/decode path","Audit generic code that requires T: Default before using it with media types"],"tags":["rust","panic","default","media"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}