grafana/k6 · error

converting argument '%v': %w

Error message

converting argument '%v': %w

What it means

Before k6 sends arguments of page.evaluate / handle.evaluate / evaluateHandle to the browser, each argument passes through convertArgument: special numbers (NaN, Infinity, -Infinity, -0, int64 above int32) become CDP UnserializableValue, ElementHandle/BaseJSHandle become object references, and remaining numeric values are json.Marshal-ed into a CallArgument. This error wraps a json.Marshal failure at that boundary, so the value cannot cross into the browser. For plain finite float64 values marshal cannot fail, making this a defensive wrap that fires only for exotic, non-JSON-encodable numeric states reaching the branch.

Source

Thrown at internal/js/modules/k6/browser/common/helpers.go:80

		case math.Inf(0):
			unserVal = "Infinity"
		case math.Inf(-1):
			unserVal = "-Infinity"
		default:
			if math.IsNaN(a) {
				unserVal = "NaN"
			}
		}

		if unserVal != "" {
			return &cdpruntime.CallArgument{
				UnserializableValue: cdpruntime.UnserializableValue(unserVal),
			}, nil
		}

		b, err := json.Marshal(a)
		if err != nil {
			err = fmt.Errorf("converting argument '%v': %w", arg, err)
		}

		return &cdpruntime.CallArgument{Value: b}, err
	case *ElementHandle:
		return convertBaseJSHandleTypes(ctx, execCtx, &a.BaseJSHandle)
	case *BaseJSHandle:
		return convertBaseJSHandleTypes(ctx, execCtx, a)
	default:
		b, err := json.Marshal(a)
		return &cdpruntime.CallArgument{Value: b}, err //nolint:wrapcheck
	}
}

func call(
	ctx context.Context, fn func(context.Context, chan any, chan error), timeout time.Duration,
) (any, error) {
	var (
		result   any

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass plain JSON-safe primitives (numbers, strings, booleans, plain arrays/objects) as evaluate arguments
  2. Pre-serialize complex values yourself (JSON.stringify) and parse inside the evaluated function
  3. If the value is derived from an object graph with cycles or functions, extract only the needed scalar fields before passing
  4. Use the '%v' rendering of the argument in the message to identify which value failed conversion

Example fix

// before
const big = obj.cyclicGraph;
await page.evaluate((v) => use(v), big);

// after
const slim = JSON.stringify({ id: obj.id, n: obj.n });
await page.evaluate((s) => use(JSON.parse(s)), slim);
Defensive patterns

Strategy: validation

Validate before calling

function jsonSafe(v) {
  try { JSON.stringify(v); return v; }
  catch { return JSON.stringify(v, replacerOrNullSafe); }
}

Prevention

When it happens

Trigger: Passing numeric arguments to page.evaluate(fn, arg) or handle.evaluate whose value cannot be JSON-encoded by the runtime; in practice anything that makes encoding/json fail while still hitting the numeric conversion branch of convertArgument in helpers.go:42.

Common situations: Passing values from k6/Go-side objects or computed numbers that are not JSON-safe into evaluate calls; edge cases around very large or special numeric values produced by script math; code that assumes any value can be forwarded as an argument.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/219e42a3fa31033c. Report an issue: GitHub.