wavetermdev/waveterm · error

invalid input format: %w

Error message

invalid input format: %w

What it means

After unmarshaling the input into readTextFileParams via utilfn.ReUnmarshal, any shape/type mismatch returns this wrapped error. It means the input was present but was not a valid object conforming to the read_text_file parameter schema.

Source

Thrown at pkg/aiusechat/tools_readfile.go:40

const StopReasonMaxBytes = "max_bytes"

type readTextFileParams struct {
	Filename string  `json:"filename"`
	Origin   *string `json:"origin"` // "start" or "end", defaults to "start"
	Offset   *int    `json:"offset"` // lines to skip, defaults to 0
	Count    *int    `json:"count"`  // number of lines to read, defaults to DefaultLineCount
	MaxBytes *int    `json:"max_bytes"`
}

func parseReadTextFileInput(input any) (*readTextFileParams, error) {
	result := &readTextFileParams{}

	if input == nil {
		return nil, fmt.Errorf("input is required")
	}

	if err := utilfn.ReUnmarshal(result, input); err != nil {
		return nil, fmt.Errorf("invalid input format: %w", err)
	}

	if result.Filename == "" {
		return nil, fmt.Errorf("missing filename parameter")
	}

	if result.Origin == nil {
		origin := "start"
		result.Origin = &origin
	}

	if *result.Origin != "start" && *result.Origin != "end" {
		return nil, fmt.Errorf("invalid origin value '%s': must be 'start' or 'end'", *result.Origin)
	}

	if result.Offset == nil {
		offset := 0
		result.Offset = &offset

View on GitHub (pinned to a4447c1563)

Solutions

  1. Send a JSON object matching the schema: {filename, origin?, offset?, count?, maxBytes?}.
  2. Ensure numeric fields (offset, count, maxBytes) are JSON numbers, not strings.
  3. Log the underlying %w cause to identify the exact field that failed unmarshal.

Example fix

// before
{"input": "{\"filename\": \"/a.txt\"}"}
// after
{"input": {"filename": "/a.txt", "offset": 0}}
Defensive patterns

Strategy: validation

Validate before calling

// validate shape client-side before sending
function validReadInput(o) {
  return typeof o === 'object' && o !== null &&
    (o.offset === undefined || typeof o.offset === 'number') &&
    (o.count === undefined || typeof o.count === 'number') &&
    (o.maxBytes === undefined || typeof o.maxBytes === 'number');
}

Type guard

func isReadTextFileInput(v any) bool {
    var p readTextFileParams
    return utilfn.ReUnmarshal(&p, v) == nil
}

Try / catch

params, err := parseReadTextFileInput(input)
if err != nil && strings.HasPrefix(err.Error(), "invalid input format") {
    log.Printf("schema mismatch: %v", err) // wrapped cause names the field
}

Prevention

When it happens

Trigger: Passing a string, array, or number as input; JSON with wrong types (e.g. "offset": "5" instead of a number); input not re-marshalable into the params struct.

Common situations: Agents quoting the whole argument set as a string; schema drift after library upgrades adding/renaming fields; JSON produced by a model with malformed types.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/5560ab58b1e8853d. Report an issue: GitHub.