{"record":{"id":"575dcc90c076e031","repo":"BoundaryML/baml","slug":"touppercase-method-only-available-on-strings-at","errorCode":null,"errorMessage":"toUpperCase() method only available on strings at {:?}","messagePattern":"toUpperCase\\(\\) method only available on strings at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2849,"sourceCode":"                    meta.0\n                ),\n            }\n        }\n        \"toLowerCase\" => {\n            let BamlValueWithMeta::String(s, _) = receiver else {\n                bail!(\n                    \"toLowerCase() method only available on strings at {:?}\",\n                    meta.0\n                );\n            };\n            if !args.is_empty() {\n                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(),","sourceCodeStart":2831,"sourceCodeEnd":2867,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2831-L2867","documentation":"The BAML expression interpreter only allows calling toUpperCase() on a value that is already a string. At runtime the receiver expression evaluated to a non-string type (int, float, bool, null, map, array, etc.), so evaluate_method_call bails before performing the uppercase conversion. This mirrors JavaScript's 'toUpperCase is not a function' TypeError but surfaces at interpretation time.","triggerScenarios":"Calling `someValue.toUpperCase()` in a BAML expression block or string template where `someValue` is not a string — e.g. an int field, a bool, null, a map/array value, or the result of a function/prompt that returns a non-string type.","commonSituations":"Devs assume a field from a client-provided map or JSON input is a string when it is actually a number or bool; a function's return type changed from string to another type in a newer version of the BAML schema; optional/null values flow into a method call without a null check.","solutions":["Check the type of the receiver value and coerce it to a string before calling toUpperCase(), e.g. wrap it in string interpolation or use the appropriate string conversion function.","Fix the upstream field/variable declaration so the value is declared (and produced) as a string type.","If the value may be null or another type, branch on its type before calling the method.","Inspect the span reported in the error message ({:?} meta span) to locate the exact receiver expression and correct it."],"exampleFix":"// before\nlet upper = user.age.toUpperCase();\n// after\nlet upper = \"{{ user.age }}\".toUpperCase();  // or declare age as string","handlingStrategy":"type-guard","validationCode":"// before evaluating the expression\nif (typeof value !== 'string') {\n  throw new Error('toUpperCase() receiver must be a string, got: ' + typeof value);\n}","typeGuard":"const isStr = (v) => typeof v === 'string';","tryCatchPattern":"// wrap BAML function invocation\ntry {\n  const res = await b.Function(args);\n} catch (e) {\n  if (String(e.message).includes('toUpperCase() method only available on strings')) {\n    // coerce the offending field to string and retry once\n  }\n  throw e;\n}","preventionTips":["Declare fields you case-transform as string types in the BAML schema.","Coerce client/JSON inputs to strings at the boundary before they reach BAML expressions.","Null-check optional values before method calls.","Keep function return types in sync with expression expectations after schema changes."],"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"}