BoundaryML/baml · error · anyhow::Error

Failed to resolve expression {:?} with error: {:?}

Error message

Failed to resolve expression {:?} with error: {:?}

What it means

resolve_expression failed to evaluate a runtime context expression (typically an environment-variable interpolation like ${MY_VAR}) into the requested type T, and wraps the resolver error with the expression text for diagnosis. The library throws this whenever expr.resolve_serde against the EvaluationContext (env vars, with the given strict mode) fails.

Source

Thrown at engine/baml-runtime/src/types/runtime_context.rs:128

            type_alias_overrides,
            recursive_type_alias_overrides,
            call_id_stack,
            recursive_class_overrides,
            is_modular_api: false,
        }
    }

    pub fn resolve_expression<T: serde::de::DeserializeOwned>(
        &self,
        expr: &UnresolvedValue<()>,
        // If true, will return an error if any environment variables are not set
        // otherwise, will return a value with the missing environment variables replaced with the string "${key}"
        strict: bool,
    ) -> Result<T> {
        let ctx = EvaluationContext::new(&self.env, strict);
        match expr.resolve_serde::<T>(&ctx) {
            Ok(v) => Ok(v),
            Err(e) => anyhow::bail!(
                "Failed to resolve expression {:?} with error: {:?}",
                expr,
                e
            ),
        }
    }
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Set the missing environment variable the expression references
  2. Check the expression's value type matches the target parameter (string vs int)
  3. Fix the expression syntax in the BAML file
  4. Run with strict resolution to surface all unresolved variables early

Example fix

// before
export BAML_LOG=info
// after
export OPENAI_API_KEY=sk-...
export BAML_LOG=info
Defensive patterns

Strategy: validation

Validate before calling

// ensure all env vars referenced in expressions exist before resolving
const required = ['OPENAI_API_KEY', 'MODEL_NAME'];
for (const k of required) {
  if (!process.env[k]) throw new Error(`missing env var for BAML expression: ${k}`);
}

Try / catch

match ctx.resolve_expression::<String>(&expr) {
  Ok(v) => v,
  Err(e) => { eprintln!("check env vars / expression syntax: {e}"); return Err(e); }
}

Prevention

When it happens

Trigger: Calling RuntimeContext::resolve_expression::<T> with an expression referencing a missing environment variable (non-strict mode leaves placeholders), or an expression whose resolved value cannot deserialize into T.

Common situations: BAML clients referencing ${OPENAI_API_KEY} or similar in config where the env var is unset or the value type doesn't match what the client expects (e.g. number vs string).

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


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