{"record":{"id":"7d94b5fa3e57d652","repo":"BoundaryML/baml","slug":"type-error-expected-expected-got-got","errorCode":null,"errorMessage":"type error: expected {expected}, got {got}","messagePattern":"type error: expected (.+?), got (.+?)","errorType":"validation","errorClass":"BamlError","httpStatus":null,"severity":"error","filePath":"languages/rust/baml/src/error.rs","lineNumber":14,"sourceCode":"use std::collections::HashMap;\n\n/// BAML runtime errors\n///\n/// Note: This is intentionally minimal. Expand with specific variants\n/// (`InitError`, `CallError`, etc.) once the core functionality works.\n#[derive(Debug, thiserror::Error, Clone)]\npub enum BamlError {\n    /// Internal/unexpected errors - bugs in BAML that should never happen\n    #[error(\"internal error: {0}\")]\n    Internal(String),\n\n    /// Type check errors - expected runtime failures (type mismatches)\n    #[error(\"type error: expected {expected}, got {got}\")]\n    TypeCheck { expected: String, got: String },\n}\n\n/// Trait for types that can report their full type name for error messages.\n/// Used by `BamlValue` variants to provide descriptive \"got\" values.\npub trait FullTypeName {\n    fn full_type_name(&self) -> String;\n}\n\nimpl BamlError {\n    /// Create an internal error for unexpected bugs\n    pub fn internal(msg: impl Into<String>) -> Self {\n        BamlError::Internal(msg.into())\n    }\n\n    /// Create a type check error for expected runtime type mismatches.\n    ///\n    /// - `T`: The expected type (must implement `BamlTypeName`)","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/languages/rust/baml/src/error.rs#L1-L32","documentation":"BamlError::TypeCheck signals an expected runtime type mismatch in the Rust BAML bindings: a BamlValue had a different type than required. The FullTypeName trait supplies the descriptive \"got\" value, rendering as \"type error: expected <X>, got <Y>\".","triggerScenarios":"Calling runtime APIs that downcast or match on BamlValue variants (string vs class vs list, etc.) with a value of the wrong type — e.g. passing a string where a class instance or list is expected by a generated function signature.","commonSituations":"Hand-constructed BamlValue inputs that don't match the .baml function's parameter types; schema drift after editing a .baml file without regenerating the Rust client; passing a JSON value with the wrong shape into the runtime.","solutions":["Compare the 'expected' and 'got' in the message and fix the value's type at the call site.","Regenerate the BAML client after any .baml schema change so generated types match the runtime.","Validate input JSON shapes (e.g. with serde typed structs) before handing them to the BAML runtime."],"exampleFix":"// before\nlet args = baml_types::BamlMap::from([(\"user\", BamlValue::String(\"alice\".into()))]);\n// .baml expects a class User\n// after\nlet args = baml_types::BamlMap::from([(\"user\", BamlValue::Class(\"User\".into(), user_fields))]);","handlingStrategy":"type-guard","validationCode":"// validate input JSON shape before calling the runtime\nlet user: User = serde_json::from_value(input_json)?; // fails early on wrong types","typeGuard":"matches!(err, BamlError::TypeCheck { .. })\n// or narrow a value:\nfn as_string(v: &BamlValue) -> Option<&str> { if let BamlValue::String(s) = v { Some(s) } else { None } }","tryCatchPattern":"match call {\n    Err(BamlError::TypeCheck { expected, got }) => {\n        eprintln!(\"fix input: expected {expected}, got {got}\");\n    }\n    r => r?,\n}","preventionTips":["Regenerate clients after every .baml schema edit.","Deserialize inputs into typed structs before passing to BAML.","Check 'expected' vs 'got' in the message to fix the exact field."],"tags":["rust","type-error","baml","runtime"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}