BoundaryML/baml · error

Expected a string, not a bool

Error message

Expected a string, not a bool

What it means

as_static_str only yields strings; encountering UnresolvedValue::Bool means the field holds true/false where a string is required, so the accessor errors. This prevents accidental boolean-to-string coercion in statically read BAML fields.

Source

Thrown at engine/baml-lib/baml-types/src/value_expr.rs:529

    }
}

impl<Meta> UnresolvedValue<Meta> {
    pub fn as_static_str(&self) -> Result<&str> {
        match self {
            Self::String(StringOr::Value(v), ..) => Ok(v.as_str()),
            Self::String(StringOr::EnvVar(..), ..) => {
                anyhow::bail!("Expected a statically defined string, not env variable")
            }
            Self::String(StringOr::JinjaExpression(..), ..) => {
                anyhow::bail!("Expected a statically defined string, not expression")
            }
            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),

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Change the BAML value from a boolean literal to the required string literal (e.g. "true" only if the schema truly wants text)
  2. Use resolve_bool(ctx) in the consuming code if the value should stay boolean
  3. Correct whichever side (schema or accessor) is wrong so types agree

Example fix

// before (baml)
options {
  some_flag true
}
// after (baml)
options {
  some_flag "enabled"
}
Defensive patterns

Strategy: validation

Validate before calling

// Rust
if matches!(v, UnresolvedValue::Bool(..)) {
    anyhow::bail!("expected a string field, got a boolean");
}
let s = v.as_static_str()?;

Type guard

fn is_string_value<Meta>(v: &UnresolvedValue<Meta>) -> bool {
    matches!(v, UnresolvedValue::String(_, _))
}

Prevention

When it happens

Trigger: Calling as_static_str() on UnresolvedValue::Bool(..) — e.g. a BAML field set to true or false but consumed via a string accessor.

Common situations: Misconfiguring a string option (like a model name, header, or tag) with a boolean value, or calling the wrong accessor after a config refactor changed the field's type.

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


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/fdbb623e21dd6ee4. Report an issue: GitHub.