googleapis/mcp-toolbox · error

expected item at index %d to be float, got %T

Error message

expected item at index %d to be float, got %T

What it means

ConvertAnySliceToTyped converts a []any into []float64 when itemType is "float". Every element must be a float64; otherwise the function returns this error identifying the index of the non-float element.

Source

Thrown at internal/util/parameters/common.go:53

			tempSlice[j] = s
		}
		typedSlice = tempSlice
	case "integer":
		tempSlice := make([]int64, len(s))
		for j, item := range s {
			i, ok := item.(int)
			if !ok {
				return nil, fmt.Errorf("expected item at index %d to be integer, got %T", j, item)
			}
			tempSlice[j] = int64(i)
		}
		typedSlice = tempSlice
	case "float":
		tempSlice := make([]float64, len(s))
		for j, item := range s {
			f, ok := item.(float64)
			if !ok {
				return nil, fmt.Errorf("expected item at index %d to be float, got %T", j, item)
			}
			tempSlice[j] = f
		}
		typedSlice = tempSlice
	case "boolean":
		tempSlice := make([]bool, len(s))
		for j, item := range s {
			b, ok := item.(bool)
			if !ok {
				return nil, fmt.Errorf("expected item at index %d to be boolean, got %T", j, item)
			}
			tempSlice[j] = b
		}
		typedSlice = tempSlice
	}
	return typedSlice, nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Convert all elements to float64 before calling (e.g. float64(i) for ints)
  2. Fix client input so all numbers carry decimal form or match the schema type
  3. Declare the parameter as 'integer' if fractional values are never expected
  4. Log the offending index (from the message) and correct the producer of that element

Example fix

// before
ConvertAnySliceToTyped([]any{1.5, 2}, "float") // fails: 2 is int
// after
ConvertAnySliceToTyped([]any{1.5, 2.0}, "float")
Defensive patterns

Strategy: type-guard

Validate before calling

func allFloats(v []any) bool {
    for _, item := range v {
        if _, ok := item.(float64); !ok {
            return false
        }
    }
    return true
}

Type guard

func isFloatArray(v []any) bool {
    for _, item := range v {
        switch item.(type) {
        case float64, float32:
        default:
            return false
        }
    }
    return true
}

Try / catch

typed, err := ConvertAnySliceToTyped(s, "float")
if err != nil {
    return nil, fmt.Errorf("invalid float array parameter: %w", err)
}

Prevention

When it happens

Trigger: Passing a slice containing ints, strings, or bools with itemType="float", e.g. []any{1.5, 2} where 2 is an int literal, or a client sending "3.14" as a string.

Common situations: Go code building arrays with int literals instead of float literals; agents sending numeric strings; configs mixing integers and decimals in one array.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/6eb62bec62745665. Report an issue: GitHub.