dgraph-io/dgraph · error

Expected an int but got %v

Error message

Expected an int but got %v

What it means

parseValue parses a variable declared (or inferred) as type "int" using strconv.ParseInt(v.Value, 0, 64). If the string is not a valid integer literal, the parse error is wrapped with 'Expected an int but got %v' showing the offending value.

Source

Thrown at dql/parser.go:319

}

// Request stores the query text and the variable mapping.
type Request struct {
	Str       string
	Variables map[string]string
}

func parseValue(v varInfo) (types.Val, error) {
	typ := v.Type
	if v.Value == "" {
		return types.Val{}, errors.Errorf("No value found")
	}

	switch typ {
	case "int":
		{
			if i, err := strconv.ParseInt(v.Value, 0, 64); err != nil {
				return types.Val{}, errors.Wrapf(err, "Expected an int but got %v", v.Value)
			} else {
				return types.Val{
					Tid:   types.IntID,
					Value: i,
				}, nil
			}
		}
	case "float":
		{
			if i, err := strconv.ParseFloat(v.Value, 64); err != nil {
				return types.Val{}, errors.Wrapf(err, "Expected a float but got %v", v.Value)
			} else {
				return types.Val{
					Tid:   types.FloatID,
					Value: i,
				}, nil
			}
		}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure the variable's string value is a valid Go-parseable integer (digits, optional sign, optional 0x/0b prefixes since base 0 is used)
  2. Fix the declared type: if the value is a decimal like 12.5, declare it as "float" instead of "int"
  3. Validate/convert on the client with strconv.ParseInt (or equivalent) before submitting
  4. Read the wrapped inner error and the %v value in the message to see which variable value failed

Example fix

// before
vars := map[string]string{"$age": "12.5"} // declared int
// after
vars := map[string]string{"$age": "12"} // or declare $age as float
Defensive patterns

Strategy: validation

Validate before calling

if _, err := strconv.ParseInt(vars["$age"], 0, 64); err != nil {
  return fmt.Errorf("$age must be an integer: %w", err)
}

Try / catch

_, err := dql.ParseWithNeedVars(query, vars)
if err != nil && strings.Contains(err.Error(), "Expected an int") {
  return fmt.Errorf("bad int variable: %w", err)
}

Prevention

When it happens

Trigger: Passing a variable whose Type is "int" but whose Value string isn't a valid integer — e.g. "$age": "abc", "12.5", or "1e3" in the variables map of a parameterized query.

Common situations: Sending JSON numbers as raw strings from clients with locale formatting ("1,000"); passing float values where an int is declared; user input not sanitized before being placed in the variables map.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/81f250402e372864. Report an issue: GitHub.