{"record":{"id":"cd79ea66e642738e","repo":"BoundaryML/baml","slug":"baml-json-json-is-declared-by-the-stdlib","errorCode":null,"errorMessage":"`{BAML_JSON_JSON}` is declared by the stdlib","messagePattern":"`(.+?)` is declared by the stdlib","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm/src/package_baml/json.rs","lineNumber":40,"sourceCode":"/// FQN of the recursive `json` type alias declared in `baml.json`.\n/// Mirrors `baml_base::qualified_name::BAML_JSON_JSON`; inlined here to\n/// avoid dragging the whole `baml_base` crate into `bex_vm` deps.\nconst BAML_JSON_JSON: &str = \"baml.json.json\";\n\n/// The runtime type of an untyped `json` value: the recursive `baml.json.json`\n/// alias (`null | bool | int | float | string | json[] | map<string, json>`).\n/// Recursive aliases stay opaque in `RealizedTy`, so this is the most precise\n/// element/value type available for containers parsed from untyped JSON.\n/// The `baml.json.json` alias type, headed at its declaration.\n///\n/// A stdlib FQN constant resolving to a head — one of the three sanctioned\n/// name-to-head boundaries. The alias is compiled, so the tag is\n/// content-addressed and the pointer comes off the declaration itself.\npub(super) fn json_alias_ty(vm: &BexVm) -> RealizedTy {\n    let qtn = TypeName::from_dotted_path(BAML_JSON_JSON);\n    let head = vm\n        .declaration_head(&qtn)\n        .unwrap_or_else(|| unreachable!(\"`{BAML_JSON_JSON}` is declared by the stdlib\"));\n    RealizedTy::TypeAlias(head, baml_type::TyAttr::default())\n}\n\n/// Run `f` with `seg` appended to `path`, then restore `path` to its prior\n/// length. Used to track the JSON pointer during recursive (de)serialization\n/// without mutating the buffer's owner contract.\nfn with_path_segment<F, R>(path: &mut String, seg: std::fmt::Arguments<'_>, f: F) -> R\nwhere\n    F: FnOnce(&mut String) -> R,\n{\n    use std::fmt::Write;\n    let saved_len = path.len();\n    let _ = write!(path, \"{seg}\");\n    let r = f(path);\n    path.truncate(saved_len);\n    r\n}\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm/src/package_baml/json.rs#L22-L58","documentation":"Panic in `json_alias_ty` when the stdlib type alias `BAML_JSON_JSON` (the `json.JSON` alias) is missing from the VM's declaration table. The function resolves the declaration head for the alias to build a `RealizedTy::TypeAlias`, and assumes the stdlib always declares it. If the lookup fails, the VM environment is incomplete or corrupted.","triggerScenarios":"Running the VM without the stdlib (`baml_builtins2`) loaded, with a stripped/partial stdlib build, or with a declaration table that dropped the JSON alias; triggered through `serde_to_value` whenever a JSON value must be realized as the stdlib alias type.","commonSituations":"Custom VM embeddings that initialize BexVm without registering the full stdlib, builds with feature flags that exclude the JSON package, or version mismatches between the VM and the bundled baml_builtins2 artifacts.","solutions":["Ensure BexVm is initialized with the complete stdlib (baml_builtins2) including the json package.","Check feature flags / build configuration so the JSON stdlib declarations are not compiled out.","Align VM and stdlib versions — rebuild with matching baml_language crates.","Inspect the declaration table (vm.declaration_head for the dotted path) at startup and fail fast if the alias is absent."],"exampleFix":"// before\nlet vm = BexVm::new_minimal();\n// after\nlet vm = BexVm::new_with_stdlib(); // registers baml_builtins2 incl. json.JSON","handlingStrategy":"validation","validationCode":"fn stdlib_json_ready(vm: &BexVm) -> bool {\n    vm.declaration_head(&TypeName::from_dotted_path(BAML_JSON_JSON)).is_some()\n}","typeGuard":null,"tryCatchPattern":"// Fail fast at VM startup instead of panicking during serialization:\nassert!(stdlib_json_ready(&vm), \"stdlib json.JSON alias missing\");","preventionTips":["Always initialize BexVm with the full stdlib (baml_builtins2).","Don't compile out the json package via feature flags in production builds.","Pin VM and stdlib versions together; run a startup smoke test that resolves json.JSON."],"tags":["rust","stdlib","json","panic"],"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-14T11:17:12.474Z"}