BoundaryML/baml · error
Expected a string
Error message
Expected a string
What it means
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.
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
Example fix
// before (baml)
options {
temperature 0.7 // numeric, read with resolve_string
}
// after (baml)
options {
model "gpt-4o"
}
// or in Rust: match value.resolve(&ctx) { ... } to see the actual variant Defensive patterns
Strategy: try-catch
Validate before calling
// Rust: pre-check resolved variant
match value.resolve(&ctx) {
Ok(ResolvedValue::String(_)) => {},
Ok(other) => anyhow::bail!("field must resolve to a string, got {:?}", std::mem::discriminant(&other)),
Err(e) => return Err(e.context("resolution failed before string check")),
} Type guard
fn resolves_to_string(v: &UnresolvedValue<()>, ctx: &EvaluationContext) -> bool {
matches!(v.resolve(ctx), Ok(ResolvedValue::String(..)))
} Try / catch
// Rust
let s = value.resolve_string(&ctx)
.with_context(|| format!("field '{name}' must be a string; check its type and any env::VAR it references"))?; Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Could not unify Bool with
- Could not unify Float with
- Expected a boolean
- Expected a map
- Expected a null value
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/db857c9e2dbba9ce.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-lib/baml-types/src/value_expr.rs:541
}
Self::String(StringOr::TemplateStringCall { .. }, ..) => {
anyhow::bail!("Expected a statically defined string, not a template_string call")
}
Self::Numeric(num, ..) => Ok(num.as_str()),
Self::Array(..) => anyhow::bail!("Expected a string, not an array"),
Self::Bool(..) => anyhow::bail!("Expected a string, not a bool"),
Self::Map(..) => anyhow::bail!("Expected a string, not a map"),
Self::Null(..) => anyhow::bail!("Expected a string, not null"),
Self::ClassConstructor(..) => {
anyhow::bail!("Expected a string, not a class constructor")
}
}
}
pub fn resolve_string(&self, ctx: &impl GetEnvVar) -> Result<String> {
match self.resolve(ctx) {
Ok(ResolvedValue::String(s, ..)) => Ok(s),
_ => Err(anyhow::anyhow!("Expected a string")),
}
}
pub fn resolve_bool(&self, ctx: &impl GetEnvVar) -> Result<bool> {
match self.resolve(ctx) {
Ok(ResolvedValue::Bool(b, ..)) => Ok(b),
_ => Err(anyhow::anyhow!("Expected a boolean")),
}
}
pub fn resolve_array(&self, ctx: &impl GetEnvVar) -> Result<Vec<ResolvedValue>> {
match self.resolve(ctx) {
Ok(ResolvedValue::Array(a, ..)) => Ok(a),
_ => Err(anyhow::anyhow!("Expected an array")),
}
}
pub fn resolve_map(&self, ctx: &impl GetEnvVar) -> Result<IndexMap<String, ResolvedValue>> {View on GitHub (pinned to bd85ce9dee)