{"record":{"id":"6d2c03e6e58d2dc0","repo":"BoundaryML/baml","slug":"substring-start-argument-must-be-an-int-at","errorCode":null,"errorMessage":"substring() start argument must be an int at {:?}","messagePattern":"substring\\(\\) start argument must be an int at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2958,"sourceCode":"                .map(|part| BamlValueWithMeta::String(part.to_string(), meta.clone()))\n                .collect();\n            Ok(BamlValueWithMeta::List(parts, meta.clone()))\n        }\n        \"substring\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\n                    \"substring() method only available on strings at {:?}\",\n                    meta.0\n                );\n            };\n            if args.len() != 2 {\n                bail!(\n                    \"substring() method takes exactly 2 arguments at {:?}\",\n                    meta.0\n                );\n            }\n            let BamlValueWithMeta::Int(start, _) = &args[0] else {\n                bail!(\"substring() start argument must be an int at {:?}\", meta.0);\n            };\n            let BamlValueWithMeta::Int(end, _) = &args[1] else {\n                bail!(\"substring() end argument must be an int at {:?}\", meta.0);\n            };\n\n            let start = (*start as usize).min(s.len());\n            let end = (*end as usize).min(s.len()).max(start);\n\n            Ok(BamlValueWithMeta::String(\n                s[start..end].to_string(),\n                meta.clone(),\n            ))\n        }\n        \"replace\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\"replace() method only available on strings at {:?}\", meta.0);\n            };\n            if args.len() != 2 {","sourceCodeStart":2940,"sourceCodeEnd":2976,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2940-L2976","documentation":"The first argument to substring (the start index) must be an integer. The interpreter destructures args[0] as BamlValueWithMeta::Int and bails with this message otherwise. No implicit conversion from string or float indices is performed.","triggerScenarios":"Calling s.substring(\"0\", 5), s.substring(startFloat, end), or passing a null/non-int value as the start index in a BAML expression.","commonSituations":"Indices read from parsed/config data typed as strings; using float positions from computed arithmetic; optional index variables that are null at runtime.","solutions":["Pass an integer literal or an int-typed variable as the start index; convert first if it comes as a string/float.","Check the type of the expression in argument position 0 at the failing call site.","Clamp/round computed indices to integers before the call."],"exampleFix":"// before\nlet head = s.substring(startStr, 5); // startStr: string\n\n// after\nlet head = s.substring(startStr.to_int(), 5);","handlingStrategy":"validation","validationCode":"// start index must be an int\nlet start_ok = start is int;","typeGuard":"fn is_int(v: BamlValue) -> bool { matches!(v, BamlValue::Int(_)) }","tryCatchPattern":"match result {\n  Err(e) if e.to_string().contains(\"start argument must be an int\") => use_default_start,\n  other => other,\n}","preventionTips":["Convert string/float indices to int before passing them.","Compute indices with integer arithmetic only.","Type-annotate index variables as int."],"tags":["baml","argument-type","substring","index"],"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"}