{"record":{"id":"d5310b28fe20fe8b","repo":"BoundaryML/baml","slug":"replace-method-only-available-on-strings-at","errorCode":null,"errorMessage":"replace() method only available on strings at {:?}","messagePattern":"replace\\(\\) method only available on strings at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2974,"sourceCode":"            }\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 {\n                bail!(\"replace() method takes exactly 2 arguments at {:?}\", meta.0);\n            }\n            let BamlValueWithMeta::String(search, _) = &args[0] else {\n                bail!(\"replace() search argument must be a string at {:?}\", meta.0);\n            };\n            let BamlValueWithMeta::String(replacement, _) = &args[1] else {\n                bail!(\n                    \"replace() replacement argument must be a string at {:?}\",\n                    meta.0\n                );\n            };\n            // Replace first occurrence only (matching JavaScript behavior)\n            let result = s.replacen(search.as_str(), replacement.as_str(), 1);\n            Ok(BamlValueWithMeta::String(result, meta.clone()))\n        }\n        \"to_fixed\" => {","sourceCodeStart":2956,"sourceCodeEnd":2992,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2956-L2992","documentation":"The BAML interpreter's evaluate_method_call hit the \"replace\" string method with a receiver value that is not a String. The library only defines replace() on string values, so it bails with the source location (meta.0) attached. This is a runtime type mismatch between the declared/actual receiver type and the method called.","triggerScenarios":"Calling .replace(...) on a non-string value at runtime, e.g. on an int, float, bool, null, map, or array — typically when the receiver came from an LLM response field, a media() result, or a variable whose type the typechecker did not constrain.","commonSituations":"Prompting an LLM that returns a number or null where the developer assumed a string, then calling .replace() on it inside a BAML expression; iterating mixed-type arrays and calling string methods on elements.","solutions":["Check the receiver's actual value/type at that source span and ensure it is a string before calling .replace()","Add a type annotation or cast so the value is statically a string (e.g. coalesce null to \"\" or use string interpolation)","If the value can be non-string, branch on its type or use a checked conversion instead of calling the method unconditionally"],"exampleFix":"// before (value may be int/null)\nlet cleaned = value.replace(\"-\", \"_\");\n// after\nlet cleaned = value.ToString().replace(\"-\", \"_\");","handlingStrategy":"type-guard","validationCode":"// before calling .replace\nif (value != null && value is string) { value.replace(\"a\",\"b\"); }","typeGuard":"fn is_string(v: Value) -> bool { matches!(v, Value::String(_)) }","tryCatchPattern":"try { formatted = value.replace(search, repl); } catch (e) { log(e); formatted = null; }","preventionTips":["Annotate LLM output fields as string when you intend string ops","Coalesce nullable fields before method calls","Avoid calling string methods on array/map/number values"],"tags":["runtime","type-mismatch","strings","baml"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}