{"record":{"id":"851009f7c42677ea","repo":"BoundaryML/baml","slug":"expected-a-string-not-a-class-constructor","errorCode":null,"errorMessage":"Expected a string, not a class constructor","messagePattern":"Expected a string, not a class constructor","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/baml-types/src/value_expr.rs","lineNumber":533,"sourceCode":"    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    }\n\n    pub fn resolve_bool(&self, ctx: &impl GetEnvVar) -> Result<bool> {\n        match self.resolve(ctx) {\n            Ok(ResolvedValue::Bool(b, ..)) => Ok(b),\n            _ => Err(anyhow::anyhow!(\"Expected a boolean\")),\n        }\n    }\n","sourceCodeStart":515,"sourceCodeEnd":551,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/baml-types/src/value_expr.rs#L515-L551","documentation":"as_static_str rejects UnresolvedValue::ClassConstructor — a reference to a BAML class name used as a value (e.g. for polymorphic fields). A constructor reference is not a string value even though it is written like an identifier, so the accessor bails rather than returning the class name text.","triggerScenarios":"Calling as_static_str() on UnresolvedValue::ClassConstructor(..) — a BAML field whose value is a class constructor reference (e.g. MyType(...) or the class name where a dynamic value slot accepts it) but is read as a plain string.","commonSituations":"Using a class reference where a string is expected (e.g. naming a class instead of quoting it), or reading a dynamically-typed field with a string accessor without handling the constructor case.","solutions":["Quote the value as a string literal if a plain string is intended, instead of referencing the class","In consuming code, match the ClassConstructor variant explicitly and use its data (construct the class or read its name field) instead of as_static_str()","Tighten the BAML schema so the field's type is string, making constructor references a validation error earlier"],"exampleFix":"// before (baml)\nfield MyEnum // class/enum constructor reference\n// after (baml)\nfield \"MyEnum\"","handlingStrategy":"type-guard","validationCode":"// Rust\nif matches!(v, UnresolvedValue::ClassConstructor(..)) {\n    anyhow::bail!(\"constructor reference found where a string is required\");\n}\nlet s = v.as_static_str()?;","typeGuard":"fn is_plain_string<Meta>(v: &UnresolvedValue<Meta>) -> bool {\n    matches!(v, UnresolvedValue::String(StringOr::Value(_), _))\n}","tryCatchPattern":null,"preventionTips":["Quote identifiers that are meant as strings","Handle the ClassConstructor variant explicitly in dynamic-value readers","Type the field as string in the schema so constructors fail validation early"],"tags":["rust","baml","type-mismatch","class-constructor"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}