BoundaryML/baml · error

Expected a string, not an array

Error message

Expected a string, not an array

What it means

as_static_str is a string-typed accessor: when the UnresolvedValue holds an Array, there is no string to return, so it bails. This guards against silently coercing a list value into a string where the BAML schema expects a string field.

Source

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

        }
    }
}

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) {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Change the BAML value to a single quoted string
  2. If a list is genuinely intended, read the field with resolve_array(ctx) and pick/serialize the element you need
  3. Fix the consuming code to match the field's declared type (string vs list)

Example fix

// before (baml)
options {
  model ["gpt-4o"]
}
// after (baml)
options {
  model "gpt-4o"
}
Defensive patterns

Strategy: validation

Validate before calling

// Rust
if matches!(v, UnresolvedValue::Array(..)) {
    anyhow::bail!("expected a single string, got a list");
}
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::Array(..) — the BAML field was authored as a list (e.g. ["a", "b"] or [1,2]) but the consuming API reads it as a single string.

Common situations: Config mistakes where a developer supplies a list where one value is expected (e.g. multiple model names, a list of stop sequences placed in a scalar field), or schema evolution changed the field 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/f89a5f6ca09431d1. Report an issue: GitHub.