charmbracelet/crush · error

invalid response format: missing search field

Error message

invalid response format: missing search field

What it means

Raised by sourcegraphSearchResults when data["search"] is missing or not a map (internal/agent/tools/sourcegraph.go:214-217). The expected successful envelope is data.search.results; a missing search key means the GraphQL query resolved but the Search connection was absent — typically because the query failed validation server-side and returned errors/null alongside a partially shaped data object.

Source

Thrown at internal/agent/tools/sourcegraph.go:216

			continue
		}
		message, ok := errMap["message"].(string)
		if ok {
			fmt.Fprintf(buffer, "- %s\n", message)
		}
	}
	return true
}

func sourcegraphSearchResults(result map[string]any) (map[string]any, error) {
	data, ok := result["data"].(map[string]any)
	if !ok {
		return nil, fmt.Errorf("invalid response format: missing data field")
	}

	search, ok := data["search"].(map[string]any)
	if !ok {
		return nil, fmt.Errorf("invalid response format: missing search field")
	}

	searchResults, ok := search["results"].(map[string]any)
	if !ok {
		return nil, fmt.Errorf("invalid response format: missing results field")
	}
	return searchResults, nil
}

func writeSourcegraphHeader(buffer *strings.Builder, searchResults map[string]any) {
	matchCount, _ := searchResults["matchCount"].(float64)
	resultCount, _ := searchResults["resultCount"].(float64)
	limitHit, _ := searchResults["limitHit"].(bool)

	buffer.WriteString("# Sourcegraph Search Results\n\n")
	fmt.Fprintf(buffer, "Found %d matches across %d results\n", int(matchCount), int(resultCount))

	if limitHit {

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Print the raw response to check for a top-level "errors" array explaining why data.search is absent.
  2. Simplify and re-validate the query (start with a plain term search) to rule out query syntax problems.
  3. Check the Sourcegraph GraphQL schema for changes to the search field and update the hardcoded query string in the tool if it drifted.
  4. For self-hosted endpoints, confirm the instance version still supports the V2 search API used in the query.

Example fix

null
Defensive patterns

Strategy: type-guard

Validate before calling

search, ok := data["search"].(map[string]any)
if !ok {
    if errs, ok := result["errors"].([]any); ok && len(errs) > 0 {
        return fmt.Errorf("search failed: %v", errs)
    }
}

Type guard

func extractSearch(data map[string]any) (map[string]any, bool) {
    search, ok := data["search"].(map[string]any)
    return search, ok
}

Try / catch

formatted, err := formatSourcegraphResults(result, ctxWindow, count)
if err != nil {
    // surface err text to the model; suggest simplifying the query
    return fantasy.NewTextErrorResponse("Failed to format results: " + err.Error()), nil
}

Prevention

When it happens

Trigger: formatSourcegraphResults -> sourcegraphSearchResults sees data without a "search" map: invalid or unsupported query syntax causing Sourcegraph to omit the search field, GraphQL returning {"data":{}} with a separate errors array, or an API schema change renaming/moving the search connection.

Common situations: Malformed Sourcegraph query strings (bad patternType combos, unbalanced quotes) that yield errors plus a degenerate data object; deprecated query syntax after a Sourcegraph upgrade; self-hosted instances running versions whose schema differs from sourcegraph.com.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/3eca63eb8804de59. Report an issue: GitHub.