{"record":{"id":"f60cf692d459ee9b","repo":"BoundaryML/baml","slug":"environment-variable-key-not-set","errorCode":null,"errorMessage":"Environment variable {key} not set","messagePattern":"Environment variable (.+?) not set","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/value_expr.rs","lineNumber":482,"sourceCode":"\npub struct EvaluationContext<'a> {\n    env_vars: Option<&'a HashMap<String, String>>,\n    fill_missing_env_vars: bool,\n}\n\nimpl GetEnvVar for EvaluationContext<'_> {\n    fn get_env_var(&self, key: &str) -> Result<String> {\n        match self\n            .env_vars\n            .as_ref()\n            .and_then(|env_vars| env_vars.get(key))\n        {\n            Some(v) => Ok(v.to_string()),\n            None => {\n                if self.fill_missing_env_vars {\n                    Ok(format!(\"${key}\"))\n                } else {\n                    Err(anyhow::anyhow!(\"Environment variable {key} not set\"))\n                }\n            }\n        }\n    }\n\n    fn set_allow_missing_env_var(&self, allow: bool) -> Self {\n        Self {\n            env_vars: self.env_vars,\n            fill_missing_env_vars: allow,\n        }\n    }\n}\n\nimpl<'a> EvaluationContext<'a> {\n    pub fn new(env_vars: &'a HashMap<String, String>, fill_missing_env_vars: bool) -> Self {\n        Self {\n            env_vars: Some(env_vars),\n            fill_missing_env_vars,","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/value_expr.rs#L464-L500","documentation":"The GetEnvVar implementation used by ValueExpr::resolve looks up an environment variable and, when it is unset, either returns a \"$KEY\" placeholder (if fill_missing_env_vars is enabled) or raises this error. It means a required environment variable was not present in the process environment at resolution time.","triggerScenarios":"Calling resolve() on a ValueExpr::EnvVar whose key is missing from the environment and whose resolver has fill_missing_env_vars = false.","commonSituations":"Deployments where .env files were not loaded (dotenv not initialized), CI/CD secrets not injected, renamed variables after config updates, or running locally without exporting required keys.","solutions":["Set the environment variable before running (export KEY=value or add it to .env and load it at startup).","Check the variable exists with std::env::var before resolving, and give a clear setup message if absent.","Enable fill_missing_env_vars if a \"$KEY\" placeholder is acceptable for your use case.","Document/validate required env vars at application startup instead of at first use."],"exampleFix":"// before\nlet key = expr.resolve(&env)?;\n// after\nif std::env::var(\"MY_KEY\").is_err() {\n    anyhow::bail!(\"Set MY_KEY in your environment (see .env.example)\");\n}\nlet key = expr.resolve(&env)?;","handlingStrategy":"validation","validationCode":"std::env::var(\"MY_KEY\").map_err(|_| anyhow!(\"MY_KEY is not set; add it to your environment or .env\"))?;","typeGuard":null,"tryCatchPattern":"let key = expr.resolve(&env).map_err(|e| anyhow!(\"missing env var: {}\", e))?;","preventionTips":["Load .env files at startup and validate all required keys","Inject secrets in CI/CD before the process starts","Fail fast with a checklist of required env vars at boot"],"tags":["environment","config","rust"],"backgroundTag":"missing-env-var","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}