different-ai/openwork · error · InterpreterRuntimeError
String.${name} expects argument ${index + 1} to be a string.
Error message
String.${name} expects argument ${index + 1} to be a string. What it means
The interpreter's invokeStringMethod helper defines a local `str(index)` accessor that asserts a positional argument of a String prototype method is a string before use. When a script calls a string method (e.g. replace, startsWith) with a non-string argument where the implementation requires one (1-based index in the message), it throws this InterpreterRuntimeError pointing at the call's AST node. It enforces string-typed arguments rather than relying on implicit coercion.
Source
Thrown at packages/codemode/src/interpreter/runtime.ts:340
}
}
if (rhs instanceof PromiseNamespace) return lhs instanceof SandboxPromise
// Number/String/Boolean wrap primitives in JS; no boxed values exist in CodeMode, so
// `x instanceof Number` is always false - exactly what it is for primitives in JS.
if (rhs instanceof CoercionFunction && (rhs.name === "Number" || rhs.name === "String" || rhs.name === "Boolean")) {
return false
}
throw new InterpreterRuntimeError(
"The right-hand side of 'instanceof' must be a constructor CodeMode knows: Error (or a specific error type like TypeError), Date, RegExp, Map, Set, URL, URLSearchParams, Array, Object, or Promise.",
node,
)
}
const invokeStringMethod = (value: string, name: string, args: Array<unknown>, node: AstNode): unknown => {
const str = (index: number): string => {
const arg = args[index]
if (typeof arg !== "string")
throw new InterpreterRuntimeError(`String.${name} expects argument ${index + 1} to be a string.`, node)
return arg
}
const num = (index: number): number => {
const arg = args[index]
if (typeof arg !== "number")
throw new InterpreterRuntimeError(`String.${name} expects argument ${index + 1} to be a number.`, node)
return arg
}
const optNum = (index: number): number | undefined => (args[index] === undefined ? undefined : num(index))
const optStr = (index: number): string | undefined => (args[index] === undefined ? undefined : str(index))
let result: unknown
switch (name) {
case "toLowerCase":
result = value.toLowerCase()
break
case "toUpperCase":
result = value.toUpperCase()View on GitHub (pinned to 2b7df46e8a)
Solutions
- Convert the argument explicitly with String(arg), template literals, or .toString() before passing it to the string method.
- Check the argument at the reported position (index + 1) in the failing method call and confirm its runtime type.
- For numeric inputs, pass the number to the numeric-aware variants or coerce once at the boundary of your script.
- Add a small helper that normalizes arguments before string method calls when scripts are generated dynamically.
Example fix
// before "hello world".startsWith(104) // after "hello world".startsWith(String(104))
Defensive patterns
Strategy: type-guard
Validate before calling
const isString = (v: unknown): v is string => typeof v === "string"
// guard call sites before running:
if (!isString(pattern) || !isString(replacement)) throw new Error("String method args must be strings") Type guard
const asStringArg = (v: unknown): string => typeof v === "string" ? v : String(v)
Try / catch
try {
await program(script)
} catch (e) {
const m = /String\.(\w+) expects argument (\d+) to be a string/.exec(e instanceof Error ? e.message : "")
if (m) {
console.error(`Method String.${m[1]} got a non-string for argument ${m[2]}; coerce with String() before calling.`)
} else throw e
} Prevention
- Wrap string-method arguments in String(...) when values may be numbers or objects.
- Interpolate numbers into template literals instead of passing them directly.
- Lint generated scripts for string methods called with non-literal first arguments.
- Keep script data conversions at the boundary: stringify inputs once before the script runs.
When it happens
Trigger: Calling a string method with a number, object, or undefined where a string argument is required, e.g. `"abc".startsWith(1)` or `"a-b".replace(1, "x")` in a CodeMode script.
Common situations: Concatenating numbers instead of stringifying them before passing to string methods, LLM-generated scripts mixing types, or refactors that changed an argument from string to number.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- String.${name} expects argument ${index + 1} to be a number.
- String.repeat expects a finite non-negative count.
- Expected '${key}' to be an array.
- Expected '${key}' to be a string.
- Expected '${key}' to be a boolean.
AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01).
Data as JSON: /api/errors/941246298f5a8056.
Report an issue: GitHub.