{"record":{"id":"c8e964732e18aa8b","repo":"Tencent/WeKnora","slug":"ollama-api-returned-status-d-s","errorCode":null,"errorMessage":"ollama API returned status %d: %s","messagePattern":"ollama API returned status (.+?): (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/web_search/ollama.go","lineNumber":125,"sourceCode":"\n\treturn req, nil\n}\n\nfunc (p *OllamaProvider) doSearch(ctx context.Context, req *http.Request) ([]*types.WebSearchResult, error) {\n\tresp, err := p.client.Do(req)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer resp.Body.Close()\n\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif resp.StatusCode != http.StatusOK {\n\t\tlogger.Warnf(ctx, \"[WebSearch][Ollama] API returned status %d: %s\", resp.StatusCode, string(body))\n\t\treturn nil, fmt.Errorf(\"ollama API returned status %d: %s\", resp.StatusCode, string(body))\n\t}\n\n\tvar respData ollamaSearchResponse\n\tif err := json.Unmarshal(body, &respData); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal response: %w\", err)\n\t}\n\n\tresults := make([]*types.WebSearchResult, 0, len(respData.Results))\n\tfor _, item := range respData.Results {\n\t\tresults = append(results, &types.WebSearchResult{\n\t\t\tTitle:   item.Title,\n\t\t\tURL:     item.URL,\n\t\t\tSnippet: item.Snippet,\n\t\t\tContent: item.Content,\n\t\t\tSource:  \"ollama\",\n\t\t})\n\t}\n\treturn results, nil","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/web_search/ollama.go#L107-L143","documentation":"This error is returned by the Ollama web-search provider when the Ollama search API responds with an HTTP status other than 200. The provider wraps the actual status code and the raw response body into the error message so the caller can see exactly what Ollama returned. It is a surface for upstream API failures (auth, rate limits, bad endpoints, server errors).","triggerScenarios":"Calling Search -> doSearch when the Ollama server returns e.g. 404 (wrong path), 401/403 (auth), 429 (rate limit), or 500; any non-OK status triggers it with the body included in the message.","commonSituations":"Misconfigured Ollama base URL (hitting a wrong endpoint), Ollama version without the web-search API, reverse proxy or gateway returning HTML error pages, Ollama overloaded or shutting down.","solutions":["Read the status code and body in the error message to identify the upstream cause","Verify the Ollama base URL points at the correct search API endpoint","Check Ollama server logs for the corresponding request failure","Retry with backoff for transient 5xx/429 statuses; fix config for 4xx"],"exampleFix":"// before\nbaseURL := \"http://localhost:11434/v1/search\" // wrong path\n// after\nbaseURL := os.Getenv(\"OLLAMA_BASE_URL\") // e.g. http://localhost:11434, provider appends correct path","handlingStrategy":"try-catch","validationCode":"if !strings.HasPrefix(ollamaBaseURL, \"http\") {\n    return fmt.Errorf(\"ollama base URL must start with http(s)://\")\n}","typeGuard":"func isStatusError(err error) (int, bool) {\n    var se int\n    if n, _ := fmt.Sscanf(err.Error(), \"ollama API returned status %d\", &se); n == 1 && se != http.StatusOK {\n        return se, true\n    }\n    return 0, false\n}","tryCatchPattern":"res, err := searchClient.Search(ctx, query)\nif err != nil {\n    if code, ok := isStatusError(err); ok && code >= 500 || code == http.StatusTooManyRequests {\n        // transient: retry with backoff\n    } else if ok {\n        // permanent: fail fast, surface body from err message\n        return fmt.Errorf(\"ollama search unavailable (status %d)\", code)\n    }\n    return err\n}","preventionTips":["Pin and health-check the Ollama base URL at startup","Log the status and body on failure for diagnosis","Retry only transient codes (429, 5xx) with exponential backoff","Monitor Ollama server logs and version compatibility"],"tags":["http","network","ollama","api-error"],"backgroundTag":"upstream-http-error","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}