{"record":{"id":"626e1a959a7cfcb9","repo":"BoundaryML/baml","slug":"expected-a-statically-defined-string-not-env-variable","errorCode":null,"errorMessage":"Expected a statically defined string, not env variable","messagePattern":"Expected a statically defined string, not env variable","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/value_expr.rs","lineNumber":519,"sourceCode":"        }\n    }\n}\n\nimpl Default for EvaluationContext<'_> {\n    fn default() -> Self {\n        Self {\n            env_vars: None,\n            fill_missing_env_vars: true,\n        }\n    }\n}\n\nimpl<Meta> UnresolvedValue<Meta> {\n    pub fn as_static_str(&self) -> Result<&str> {\n        match self {\n            Self::String(StringOr::Value(v), ..) => Ok(v.as_str()),\n            Self::String(StringOr::EnvVar(..), ..) => {\n                anyhow::bail!(\"Expected a statically defined string, not env variable\")\n            }\n            Self::String(StringOr::JinjaExpression(..), ..) => {\n                anyhow::bail!(\"Expected a statically defined string, not expression\")\n            }\n            Self::String(StringOr::TemplateStringCall { .. }, ..) => {\n                anyhow::bail!(\"Expected a statically defined string, not a template_string call\")\n            }\n            Self::Numeric(num, ..) => Ok(num.as_str()),\n            Self::Array(..) => anyhow::bail!(\"Expected a string, not an array\"),\n            Self::Bool(..) => anyhow::bail!(\"Expected a string, not a bool\"),\n            Self::Map(..) => anyhow::bail!(\"Expected a string, not a map\"),\n            Self::Null(..) => anyhow::bail!(\"Expected a string, not null\"),\n            Self::ClassConstructor(..) => {\n                anyhow::bail!(\"Expected a string, not a class constructor\")\n            }\n        }\n    }\n","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/value_expr.rs#L501-L537","documentation":"BAML's UnresolvedValue::as_static_str extracts a literal string from a statically-parsed BAML value (e.g. a model provider option, client name, or metadata field). It refuses to read an env-var reference (env::VAR or $VAR) because doing so requires runtime resolution with an environment context, which this zero-cost static accessor deliberately does not perform. The library throws this to force callers to either use resolve_string(ctx) or provide a literal.","triggerScenarios":"Calling as_static_str() on an UnresolvedValue::String(StringOr::EnvVar(..)) — i.e. the BAML source declares the field as an environment-variable reference like BAML_ENV_VAR or \"env.MY_KEY\" instead of a plain quoted string.","commonSituations":"Developers parameterize BAML config (model name, API key field, client id) via env vars for dev/prod portability, then a compile-time/static pass (LLM client registration, client schema validation, or tooling that reads baml_src without env vars) tries to read the value statically and fails.","solutions":["Replace the env-var reference with a static string literal in the .baml file if the value never varies between environments","Call resolve_string(ctx) with an EvaluationContext holding the env vars instead of as_static_str() in the consuming code","Ensure the env var is set and passed into the BAML runtime (EvaluationContext::new(&env_map, ...)) so resolution happens at runtime, not static parse time"],"exampleFix":"// before (baml)\nclient MyClient {\n  provider openai\n  options {\n    api_key env.OPENAI_API_KEY\n  }\n}\n// after (baml)\nclient MyClient {\n  provider openai\n  options {\n    api_key \"sk-static-or-injected-key\"\n  }\n}\n// or in Rust, resolve at runtime instead:\n// let s = value.resolve_string(&ctx)?;","handlingStrategy":"validation","validationCode":"// Rust\nfn needs_static(v: &UnresolvedValue<()>) -> Result<&str> {\n    if let UnresolvedValue::String(StringOr::EnvVar(_), _) = v {\n        anyhow::bail!(\"field must be a static string, not env::VAR\");\n    }\n    v.as_static_str()\n}","typeGuard":"fn is_static_string<Meta>(v: &UnresolvedValue<Meta>) -> bool {\n    matches!(v, UnresolvedValue::String(StringOr::Value(_), _))\n}","tryCatchPattern":null,"preventionTips":["Keep env-var references out of fields consumed by static/compile-time passes","Pass a populated env map into EvaluationContext and prefer resolve_string for runtime paths","Document which BAML fields must be literals"],"tags":["rust","baml","static-analysis","env-var","config"],"backgroundTag":"unsupported-config-value","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}