router-for-me/CLIProxyAPI · error

weight must be an integer

Error message

weight must be an integer

What it means

Float-integrity check in credentialweight.ParseValue: a float64 weight must be a finite, integral value (math.Trunc(f) == f, not NaN, not Inf). Fractional or non-finite floats are rejected because weights must be integers.

Source

Thrown at internal/credentialweight/weight.go:78

		}
		return int64(typed), nil
	case uint8:
		return int64(typed), nil
	case uint16:
		return int64(typed), nil
	case uint32:
		if uint64(typed) > uint64(Max) {
			return 0, fmt.Errorf("weight must not exceed %d", Max)
		}
		return int64(typed), nil
	case uint64:
		if typed > uint64(Max) {
			return 0, fmt.Errorf("weight must not exceed %d", Max)
		}
		return int64(typed), nil
	case float64:
		if math.IsNaN(typed) || math.IsInf(typed, 0) || math.Trunc(typed) != typed {
			return 0, fmt.Errorf("weight must be an integer")
		}
		if typed <= 0 {
			return 0, nil
		}
		if typed > float64(Max) {
			return 0, fmt.Errorf("weight must not exceed %d", Max)
		}
		return int64(typed), nil
	case float32:
		return ParseValue(float64(typed))
	case json.Number:
		weight, errParse := typed.Int64()
		if errParse != nil {
			return 0, fmt.Errorf("weight must be an integer: %w", errParse)
		}
		return Normalize(weight)
	case string:
		return ParseString(typed)

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Use whole numbers in weight metadata: 2 instead of 2.5
  2. If fractional ratios are needed, multiply all weights by a common factor first (0.5/0.5 -> 1/1; 0.7/0.3 -> 7/3)
  3. Round at the producing side and write integers so JSON decodes them as integral float64

Example fix

// before (auth file)
{"weight": 1.5}

// after (auth file)
{"weight": 3}
Defensive patterns

Strategy: validation

Validate before calling

func integralFloat(f float64) bool {
    return !math.IsNaN(f) && !math.IsInf(f, 0) && math.Trunc(f) == f
}

Prevention

When it happens

Trigger: ParseValue receives a float64 like 2.5, -0.5 (fractional), math.NaN(), or math.Inf(1). Typically happens when JSON metadata is decoded into interface{} and numbers become float64 (the encoding/json default).

Common situations: Writing "weight": 0.5 (unquoted decimal) in an auth JSON file — encoding/json turns it into float64 and this error fires. Also scientific notation like 1e2 is accepted (integral) but 1.5e0 is not.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/ff6a75c26866258a. Report an issue: GitHub.