{"record":{"id":"4e76f5215b65b7b4","repo":"BoundaryML/baml","slug":"expected-a-string-not-null","errorCode":null,"errorMessage":"Expected a string, not null","messagePattern":"Expected a string, not null","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/value_expr.rs","lineNumber":531,"sourceCode":"\nimpl<Meta> UnresolvedValue<Meta> {\n    pub fn as_static_str(&self) -> Result<&str> {\n        match self {\n            Self::String(StringOr::Value(v), ..) => Ok(v.as_str()),\n            Self::String(StringOr::EnvVar(..), ..) => {\n                anyhow::bail!(\"Expected a statically defined string, not env variable\")\n            }\n            Self::String(StringOr::JinjaExpression(..), ..) => {\n                anyhow::bail!(\"Expected a statically defined string, not expression\")\n            }\n            Self::String(StringOr::TemplateStringCall { .. }, ..) => {\n                anyhow::bail!(\"Expected a statically defined string, not a template_string call\")\n            }\n            Self::Numeric(num, ..) => Ok(num.as_str()),\n            Self::Array(..) => anyhow::bail!(\"Expected a string, not an array\"),\n            Self::Bool(..) => anyhow::bail!(\"Expected a string, not a bool\"),\n            Self::Map(..) => anyhow::bail!(\"Expected a string, not a map\"),\n            Self::Null(..) => anyhow::bail!(\"Expected a string, not null\"),\n            Self::ClassConstructor(..) => {\n                anyhow::bail!(\"Expected a string, not a class constructor\")\n            }\n        }\n    }\n\n    pub fn resolve_string(&self, ctx: &impl GetEnvVar) -> Result<String> {\n        match self.resolve(ctx) {\n            Ok(ResolvedValue::String(s, ..)) => Ok(s),\n            _ => Err(anyhow::anyhow!(\"Expected a string\")),\n        }\n    }\n\n    pub fn resolve_bool(&self, ctx: &impl GetEnvVar) -> Result<bool> {\n        match self.resolve(ctx) {\n            Ok(ResolvedValue::Bool(b, ..)) => Ok(b),\n            _ => Err(anyhow::anyhow!(\"Expected a boolean\")),\n        }","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/value_expr.rs#L513-L549","documentation":"as_static_str cannot return a string for a null value; encountering UnresolvedValue::Null means the BAML field is explicitly null where a string is required, so it bails. This prevents null leaking into APIs that require non-optional strings.","triggerScenarios":"Calling as_static_str() on UnresolvedValue::Null(..) — a BAML field set to null (or left as a null literal) but consumed through the string accessor.","commonSituations":"Unset configuration rendered as null, optional fields read without checking presence, or generated/config-as-code output emitting null for missing values.","solutions":["Provide the required string value in the .baml file instead of null","Check for null before reading (match on the variant or use resolve_null first) and apply a default in code","Make the field non-null in the schema so mistakes surface at validation time"],"exampleFix":"// before (baml)\noptions {\n  model null\n}\n// after (baml)\noptions {\n  model \"gpt-4o\"\n}","handlingStrategy":"validation","validationCode":"// Rust\nif matches!(v, UnresolvedValue::Null(..)) {\n    anyhow::bail!(\"required string field is null\");\n}\nlet s = v.as_static_str()?;","typeGuard":"fn is_present_string<Meta>(v: &UnresolvedValue<Meta>) -> bool {\n    matches!(v, UnresolvedValue::String(StringOr::Value(_), _))\n}","tryCatchPattern":"// Rust\nlet s = v.as_static_str()\n    .with_context(|| \"required string field missing in baml config\")?;\n// provide default on error:\nlet s = v.as_static_str().unwrap_or(\"default-value\");","preventionTips":["Never leave required string options as null","Apply defaults in code for optional fields before reading","Enforce non-null in schema validation"],"tags":["rust","baml","null","config"],"backgroundTag":"null-argument","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"}