{"record":{"id":"db857c9e2dbba9ce","repo":"BoundaryML/baml","slug":"expected-a-string","errorCode":null,"errorMessage":"Expected a string","messagePattern":"Expected a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/value_expr.rs","lineNumber":541,"sourceCode":"            }\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        }\n    }\n\n    pub fn resolve_array(&self, ctx: &impl GetEnvVar) -> Result<Vec<ResolvedValue>> {\n        match self.resolve(ctx) {\n            Ok(ResolvedValue::Array(a, ..)) => Ok(a),\n            _ => Err(anyhow::anyhow!(\"Expected an array\")),\n        }\n    }\n\n    pub fn resolve_map(&self, ctx: &impl GetEnvVar) -> Result<IndexMap<String, ResolvedValue>> {","sourceCodeStart":523,"sourceCodeEnd":559,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/value_expr.rs#L523-L559","documentation":"resolve_string resolves an UnresolvedValue at runtime (env vars, expressions, templates) and requires the result to be ResolvedValue::String; anything else yields this generic error. Unlike as_static_str, it reports no variant details, so the caller learns only that the resolved value was not a string.","triggerScenarios":"Calling resolve_string(ctx) where self.resolve(ctx) succeeds with a non-String ResolvedValue (Bool, Numeric, Array, Map, Null, etc.) or where resolve itself fails (e.g. env var not set, template render error).","commonSituations":"Reading a BAML option that was authored as a boolean/number/list while the code expects a string, or the resolution pipeline failing because an env::VAR reference is unset in the runtime environment.","solutions":["Check the authored .baml value and correct its type to a string (or env var resolving to a string)","Inspect the resolution error first: call self.resolve(ctx) yourself (or check underlying anyhow chain) to distinguish type mismatch from env-var resolution failure","Set the missing environment variable and pass it via EvaluationContext::new(&env_map, fill_missing_env_vars) if the failure was an unset env::VAR","Use the appropriate accessor (resolve_bool/resolve_array/resolve_map) if the value is genuinely of another type"],"exampleFix":"// before (baml)\noptions {\n  temperature 0.7 // numeric, read with resolve_string\n}\n// after (baml)\noptions {\n  model \"gpt-4o\"\n}\n// or in Rust: match value.resolve(&ctx) { ... } to see the actual variant","handlingStrategy":"try-catch","validationCode":"// Rust: pre-check resolved variant\nmatch value.resolve(&ctx) {\n    Ok(ResolvedValue::String(_)) => {},\n    Ok(other) => anyhow::bail!(\"field must resolve to a string, got {:?}\", std::mem::discriminant(&other)),\n    Err(e) => return Err(e.context(\"resolution failed before string check\")),\n}","typeGuard":"fn resolves_to_string(v: &UnresolvedValue<()>, ctx: &EvaluationContext) -> bool {\n    matches!(v.resolve(ctx), Ok(ResolvedValue::String(..)))\n}","tryCatchPattern":"// Rust\nlet s = value.resolve_string(&ctx)\n    .with_context(|| format!(\"field '{name}' must be a string; check its type and any env::VAR it references\"))?;","preventionTips":["Call resolve() first and inspect the variant for a precise error message","Ensure all env::VAR references are present in the passed env map","Use the typed accessor matching the field's declared type"],"tags":["rust","baml","type-mismatch","resolution"],"backgroundTag":"type-mismatch","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"}