{"record":{"id":"b266f7d0b7021df1","repo":"BoundaryML/baml","slug":"expected-a-statically-defined-string-not-a-template-string","errorCode":null,"errorMessage":"Expected a statically defined string, not a template_string call","messagePattern":"Expected a statically defined string, not a template_string call","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/value_expr.rs","lineNumber":525,"sourceCode":"        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\n    pub fn resolve_string(&self, ctx: &impl GetEnvVar) -> Result<String> {\n        match self.resolve(ctx) {\n            Ok(ResolvedValue::String(s, ..)) => Ok(s),\n            _ => Err(anyhow::anyhow!(\"Expected a string\")),\n        }\n    }","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/value_expr.rs#L507-L543","documentation":"as_static_str cannot return a value for a template_string(...) call, because the rendered text only exists after runtime evaluation with arguments and a render context. The library throws this whenever a static string accessor encounters a TemplateStringCall variant, forcing the caller to resolve the value at runtime instead.","triggerScenarios":"Calling as_static_str() on UnresolvedValue::String(StringOr::TemplateStringCall{..}) — a BAML field whose value is a template_string(\"...\", args) call expression rather than a literal.","commonSituations":"Developers build strings in BAML with template_string() for reuse (e.g. composing a model name or a header value), then a compile-time step that requires literal values (client registration, enum/tag validation) rejects it.","solutions":["Inline the rendered text as a static string literal if it is fully known at authoring time","Use resolve_string(&ctx) with an EvaluationContext whose render_template implementation can render the template","Restructure the BAML config so template_string calls are only used in runtime-evaluated positions (prompts), not static fields"],"exampleFix":"// before (baml)\noptions {\n  model template_string(\"gpt-{0}\", [\"4o\"])\n}\n// after (baml)\noptions {\n  model \"gpt-4o\"\n}","handlingStrategy":"validation","validationCode":"// Rust\nif matches!(v, UnresolvedValue::String(StringOr::TemplateStringCall { .. }, _)) {\n    anyhow::bail!(\"template_string calls cannot be used in static fields\");\n}\nlet s = v.as_static_str()?;","typeGuard":"fn is_static_string<Meta>(v: &UnresolvedValue<Meta>) -> bool {\n    matches!(v, UnresolvedValue::String(StringOr::Value(_), _))\n}","tryCatchPattern":null,"preventionTips":["Reserve template_string for runtime-evaluated prompt content","Inline fully-known values as literals","Ensure EvaluationContext implements render_template if runtime rendering is needed"],"tags":["rust","baml","template","static-analysis"],"backgroundTag":"unsupported-operation","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"}