redis/go-redis · error

invalid score format

Error message

invalid score format

What it means

When WithScores is set, FT.SEARCH returns each document's score as a string right after the ID. The string must parse as a float64 via strconv.ParseFloat; if it doesn't (empty, garbage, or non-numeric text), parsing aborts with this error.

Source

Thrown at search_commands.go:2638

			return FTSearchResult{}, fmt.Errorf("invalid document ID format")
		}

		doc := Document{
			ID:     docID,
			Fields: make(map[string]string),
		}
		i++

		if noContent {
			results = append(results, doc)
			continue
		}

		if withScores && i < len(data) {
			if scoreStr, ok := data[i].(string); ok {
				score, err := strconv.ParseFloat(scoreStr, 64)
				if err != nil {
					return FTSearchResult{}, fmt.Errorf("invalid score format")
				}
				doc.Score = &score
				i++
			}
		}

		if withPayloads && i < len(data) {
			if payload, ok := data[i].(string); ok {
				doc.Payload = &payload
				i++
			}
		}

		if withSortKeys && i < len(data) {
			if sortKey, ok := data[i].(string); ok {
				doc.SortKey = &sortKey
				i++
			}

View on GitHub (pinned to c5cad058c7)

Solutions

  1. Inspect the raw reply to see the actual score string
  2. Fix the proxy/mock producing the bad score
  3. Confirm the server is genuine RediSearch of a supported version
  4. Update go-redis in case your module version changed score encoding

Example fix

// before: mock with bad score
mock := []interface{}{int64(1), "doc:1", "high", []interface{}{}}

// after: score must parse as float
cmd := rdb.FTSearch(ctx, "idx", "q", &redis.FTSearchOptions{WithScores: true})
mock := []interface{}{int64(1), "doc:1", "0.98", []interface{}{}}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check that scores parse in captured/mock replies
for _, s := range scoreStrings {
	if _, err := strconv.ParseFloat(s, 64); err != nil {
		t.Fatalf("mock score %q is not a float", s)
	}
}

Type guard

func isParseableFloat(s interface{}) bool {
	str, ok := s.(string)
	if !ok {
		return false
	}
	_, err := strconv.ParseFloat(str, 64)
	return err == nil
}

Try / catch

res, err := rdb.FTSearch(ctx, "idx", q, &redis.FTSearchOptions{WithScores: true}).Result()
if err != nil {
	if strings.Contains(err.Error(), "invalid score format") {
		log.Printf("bad score in reply: %v", err)
		return resultWithoutScores, nil
	}
	return err
}

Prevention

When it happens

Trigger: FTSearch(ctx, idx, q, &redis.FTSearchOptions{WithScores: true}) where the score element is a string that isn't a valid float — e.g. "NAN" variants from a patched server, a proxy substituting the value, or a mock with a bad score.

Common situations: Fault-injection proxies corrupting score fields; hand-written test replies with non-numeric scores; module versions emitting unexpected score encodings.

Related errors


AI-assisted analysis of redis/go-redis@c5cad058c7 (2026-09-01). Data as JSON: /api/errors/c1ec012789961503. Report an issue: GitHub.