{"record":{"id":"d85484d20cf1d90c","repo":"BoundaryML/baml","slug":"trim-method-only-available-on-strings-at","errorCode":null,"errorMessage":"trim() method only available on strings at {:?}","messagePattern":"trim\\(\\) method only available on strings at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2861,"sourceCode":"                bail!(\"toLowerCase() method takes no arguments at {:?}\", meta.0);\n            }\n            Ok(BamlValueWithMeta::String(s.to_lowercase(), meta.clone()))\n        }\n        \"toUpperCase\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\n                    \"toUpperCase() method only available on strings at {:?}\",\n                    meta.0\n                );\n            };\n            if !args.is_empty() {\n                bail!(\"toUpperCase() method takes no arguments at {:?}\", meta.0);\n            }\n            Ok(BamlValueWithMeta::String(s.to_uppercase(), meta.clone()))\n        }\n        \"trim\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\"trim() method only available on strings at {:?}\", meta.0);\n            };\n            if !args.is_empty() {\n                bail!(\"trim() method takes no arguments at {:?}\", meta.0);\n            }\n            Ok(BamlValueWithMeta::String(\n                s.trim().to_string(),\n                meta.clone(),\n            ))\n        }\n        \"includes\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\n                    \"includes() method only available on strings at {:?}\",\n                    meta.0\n                );\n            };\n            if args.len() != 1 {\n                bail!(\"includes() method takes exactly 1 argument at {:?}\", meta.0);","sourceCodeStart":2843,"sourceCodeEnd":2879,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2843-L2879","documentation":"The interpreter requires that trim() be called only on string values; a non-string receiver triggers this bail. trim() removes leading and trailing whitespace and returns a new string, so the operation is undefined for other BAML types.","triggerScenarios":"Calling `x.trim()` where `x` evaluates to a non-string at runtime — an int, float, bool, null, map, array, or an enum/media value — in a BAML expression.","commonSituations":"Trimming user input fetched from a map/JSON payload that arrives as a number; applying trim to the result of a function whose return type changed to non-string; forgetting that null values are not strings in BAML.","solutions":["Coerce the value to a string first (string interpolation or an explicit string conversion) before calling trim().","Fix the declaration/producer of the value so it is actually a string.","Guard with a type/null check before invoking trim() when the value is optional.","Use the reported span in the error to find the offending receiver expression."],"exampleFix":"// before\nlet clean = record.id.trim();\n// after\nlet clean = \"{{ record.id }}\".trim();  // or make id a string field","handlingStrategy":"type-guard","validationCode":"if (typeof value !== 'string') {\n  throw new Error('trim() receiver must be a string, got: ' + typeof value);\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('trim() method only available on strings')) {\n    // stringify the offending field and retry\n  }\n  throw e;\n}","preventionTips":["Trim user input in the host language before passing it into BAML if types are uncertain.","Declare trim targets as string fields in the schema.","Null-check optionals before calling string methods.","Convert numeric/bool map values to strings explicitly at ingestion."],"tags":["baml","interpreter","string-methods","type-mismatch"],"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"}