{"record":{"id":"e9fe1041825dae0c","repo":"BoundaryML/baml","slug":"endswith-method-only-available-on-strings-at","errorCode":null,"errorMessage":"endsWith() method only available on strings at {:?}","messagePattern":"endsWith\\(\\) method only available on strings at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2912,"sourceCode":"                );\n            };\n            if args.len() != 1 {\n                bail!(\n                    \"startsWith() method takes exactly 1 argument at {:?}\",\n                    meta.0\n                );\n            }\n            let BamlValueWithMeta::String(prefix, _) = &args[0] else {\n                bail!(\"startsWith() argument must be a string at {:?}\", meta.0);\n            };\n            Ok(BamlValueWithMeta::Bool(\n                s.starts_with(prefix.as_str()),\n                meta.clone(),\n            ))\n        }\n        \"endsWith\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\n                    \"endsWith() method only available on strings at {:?}\",\n                    meta.0\n                );\n            };\n            if args.len() != 1 {\n                bail!(\"endsWith() method takes exactly 1 argument at {:?}\", meta.0);\n            }\n            let BamlValueWithMeta::String(suffix, _) = &args[0] else {\n                bail!(\"endsWith() argument must be a string at {:?}\", meta.0);\n            };\n            Ok(BamlValueWithMeta::Bool(\n                s.ends_with(suffix.as_str()),\n                meta.clone(),\n            ))\n        }\n        \"split\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\"split() method only available on strings at {:?}\", meta.0);","sourceCodeStart":2894,"sourceCodeEnd":2930,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2894-L2930","documentation":"The BAML expression interpreter evaluates the \"endsWith\" string method at runtime. Before calling ends_with on the receiver it pattern-matches it as a String value; if the receiver is any other type (int, list, map, null, etc.) it bails with this message. The library throws it because endsWith is only defined for string receivers, and static typing did not catch the mismatch.","triggerScenarios":"Calling <non-string>.endsWith(...) in a BAML expression, e.g. `let x = 5; x.endsWith(\"5\")` or `myList.endsWith(\"a\")`, where the receiver's value is not BamlValueWithMeta::String when evaluate_method_call runs.","commonSituations":"Variables assumed to be strings but actually produced by a prompt/extraction block as a number or list; refactors that change a variable's type without updating downstream method calls; dynamic values from maps or function results that are null or untyped at runtime.","solutions":["Inspect the receiver value at the failing expression and confirm its actual type (print it or check the type annotation in the BAML file).","Convert the receiver to a string explicitly before calling endsWith, or fix the upstream expression so it yields a string.","If the receiver may legitimately be non-string, guard the call with a type check (e.g. `x is string` / match on the type) before invoking endsWith.","Check whether type annotations on the variable are wrong so the compiler can catch this statically instead of at runtime."],"exampleFix":"// before (x is an int)\nlet flag = x.endsWith(\"5\");\n\n// after\nlet flag = x.to_string().endsWith(\"5\");","handlingStrategy":"type-guard","validationCode":"// in BAML expression code, before the call\nif (x is string) {\n  return x.endsWith(\"suffix\");\n}","typeGuard":"fn is_string(v: BamlValue) -> bool { matches!(v, BamlValue::String(_)) }","tryCatchPattern":"// interpreter-level: handle the bail\nmatch evaluate_method_call(...) {\n  Ok(v) => v,\n  Err(e) if e.to_string().contains(\"only available on strings\") => fallback_value,\n  Err(e) => return Err(e),\n}","preventionTips":["Annotate variables with explicit string types so the compiler catches non-string receivers.","Never call string methods on values returned from untyped extraction blocks without checking the type.","Print or log the value once when wiring up a new expression to confirm its runtime type."],"tags":["baml","type-mismatch","string-method","runtime"],"backgroundTag":"type-mismatch","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"}