{"record":{"id":"fb68bbb01fdd5356","repo":"BoundaryML/baml","slug":"startswith-argument-must-be-a-string-at","errorCode":null,"errorMessage":"startsWith() argument must be a string at {:?}","messagePattern":"startsWith\\(\\) argument must be a string at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2903,"sourceCode":"                s.contains(search.as_str()),\n                meta.clone(),\n            ))\n        }\n        \"startsWith\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\n                    \"startsWith() method only available on strings at {:?}\",\n                    meta.0\n                );\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);","sourceCodeStart":2885,"sourceCodeEnd":2921,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2885-L2921","documentation":"The argument to startsWith() must be a string; a non-string prefix (int, bool, null, etc.) makes evaluate_method_call bail. The check is implemented with Rust's str::starts_with, which requires a string prefix.","triggerScenarios":"Calling `s.startsWith(x)` where `x` evaluates to a non-string — e.g. startsWith(version) with an int version number, or a null optional as the prefix.","commonSituations":"Checking a numeric prefix without quoting it; passing a field from JSON/map input assumed to be a string but typed as a number or bool; a null optional flowing in without a guard.","solutions":["Convert the argument to a string before passing it: `s.startsWith(\"{{ prefix }}\")`.","Fix the prefix variable's declaration so it is a string type.","Guard with a type/null check when the prefix may not be a string.","Use the error span to locate the argument expression and correct it."],"exampleFix":"// before\nlet isBeta = build.startsWith(majorVersion);  // int\n// after\nlet isBeta = build.startsWith(\"{{ majorVersion }}\");","handlingStrategy":"type-guard","validationCode":"if (typeof prefix !== 'string') {\n  throw new Error('startsWith() argument must be a string, got: ' + typeof prefix);\n}","typeGuard":"const isStr = (v) => typeof v === 'string';","tryCatchPattern":"try {\n  const res = await b.Function(args);\n} catch (e) {\n  if (String(e.message).includes('startsWith() argument must be a string')) {\n    // coerce the prefix with string interpolation and retry\n  }\n  throw e;\n}","preventionTips":["Quote numeric prefixes (e.g. \"2\" not 2) in startsWith() calls.","Interpolate non-string values into strings before prefix comparisons.","Guard nullable prefix fields before use.","Declare prefix sources as string-typed fields in the schema."],"tags":["baml","interpreter","string-methods","argument-type"],"backgroundTag":"invalid-argument-value","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"}