{"record":{"id":"dba10c42a76d1da4","repo":"BoundaryML/baml","slug":"replace-replacement-argument-must-be-a-string-at","errorCode":null,"errorMessage":"replace() replacement argument must be a string at {:?}","messagePattern":"replace\\(\\) replacement argument must be a string at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2983,"sourceCode":"            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\" => {\n            let value = match receiver {\n                BamlValueWithMeta::Float(v, _) => *v,\n                BamlValueWithMeta::Int(v, _) => *v as f64,\n                _ => bail!(\n                    \"to_fixed() method only available on floats and ints at {:?}\",\n                    meta.0\n                ),\n            };\n","sourceCodeStart":2965,"sourceCodeEnd":3001,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2965-L3001","documentation":"For the \"replace\" string method, the second argument (the replacement) must be a String value. The interpreter pattern-matches BamlValueWithMeta::String on args[1] and bails otherwise. Note the method replaces only the first occurrence (JavaScript-like behavior).","triggerScenarios":"Calling s.replace(search, nonString), e.g. replace(\"a\", 3), replace(\"a\", null), or passing a numeric LLM output as the replacement.","commonSituations":"Substituting in a numeric value (count, id, price) without converting it; a null field from a model response used as replacement text.","solutions":["Convert the replacement to a string (ToString() or string interpolation)","Ensure the second argument is a string literal or string-typed variable","If replacing all occurrences is desired, be aware this method only replaces the first; chain calls or check for a replace-all variant"],"exampleFix":"// before\nlet out = s.replace(\"{{n}}\", count);\n// after\nlet out = s.replace(\"{{n}}\", count.ToString());","handlingStrategy":"type-guard","validationCode":"// arg1 must be a string: if (repl is string) { s.replace(search, repl); }","typeGuard":"fn is_string(v: Value) -> bool { matches!(v, Value::String(_)) }","tryCatchPattern":"try { out = s.replace(search, repl); } catch (e) { log(e); }","preventionTips":["Convert numeric replacements with ToString()","Coalesce null fields to \"\" first","Keep replacements as string literals where possible"],"tags":["runtime","type-mismatch","arguments","strings"],"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"}