{"record":{"id":"39b6fa1f3f9f6e50","repo":"micro/go-micro","slug":"api-error-s-s-39b6fa","errorCode":null,"errorMessage":"API error (%s): %s","messagePattern":"API error \\((.+?)\\): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/ollama/ollama.go","lineNumber":250,"sourceCode":"\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + p.chatPath()\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to create request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\tif p.opts.APIKey != \"\" {\n\t\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\n\t}\n\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"API request failed: %w\", err)\n\t}\n\tdefer httpResp.Body.Close()\n\n\trespBody, _ := io.ReadAll(httpResp.Body)\n\tif httpResp.StatusCode != http.StatusOK {\n\t\treturn nil, nil, fmt.Errorf(\"API error (%s): %s\", httpResp.Status, string(respBody))\n\t}\n\n\tvar chatResp struct {\n\t\tUsage struct {\n\t\t\tPromptTokens     int `json:\"prompt_tokens\"`\n\t\t\tCompletionTokens int `json:\"completion_tokens\"`\n\t\t\tTotalTokens      int `json:\"total_tokens\"`\n\t\t} `json:\"usage\"`\n\t\tChoices []struct {\n\t\t\tMessage struct {\n\t\t\t\tRole      string `json:\"role\"`\n\t\t\t\tContent   string `json:\"content\"`\n\t\t\t\tToolCalls []struct {\n\t\t\t\t\tID       string `json:\"id\"`\n\t\t\t\t\tFunction struct {\n\t\t\t\t\t\tName      string `json:\"name\"`\n\t\t\t\t\t\tArguments string `json:\"arguments\"`\n\t\t\t\t\t} `json:\"function\"`","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/ollama/ollama.go#L232-L268","documentation":"Raised when the Ollama server responds with a non-200 status to the OpenAI-compatible chat request. The error includes the HTTP status line and the raw response body so the caller can see the server's error message. This is a server-side rejection, not a transport failure.","triggerScenarios":"httpResp.StatusCode != http.StatusOK in callOpenAI (called from generateOpenAI) — e.g. 404 because the model is not pulled, 400 for a malformed request (bad model name, invalid tool schema), 401/403 when an API key is required but wrong, 500 from an Ollama internal error.","commonSituations":"Model name not found locally (ollama pull never run) → 404; malformed tools/messages payloads → 400; proxy in front of Ollama requiring auth → 401/403; OOM or internal errors on large prompts → 500.","solutions":["Read the error body in the message — it names the actual problem (model not found, bad request, etc.)","Run `ollama list` / `ollama pull <model>` to ensure the requested model exists locally","Check the request payload: model name, tool definitions, and message roles must be valid for the OpenAI-compatible endpoint","If a proxy/API key is involved, verify the Authorization header and proxy configuration"],"exampleFix":"// before\nresp, err := provider.Generate(ctx, ai.Request{Model: \"llama3.1:latest\"})\n// after\n// if error says 404 model not found:\nout, err := exec.Command(\"ollama\", \"pull\", \"llama3.1\").CombinedOutput()\n// then retry the request","handlingStrategy":"try-catch","validationCode":"// ensure the model is available before calling\nout, err := exec.Command(\"ollama\", \"list\").Output()\nif err != nil || !strings.Contains(string(out), \"llama3.1\") {\n    return fmt.Errorf(\"model not pulled: run ollama pull <model>\")\n}","typeGuard":"func isNotFoundStatus(err error) bool {\n    return strings.Contains(err.Error(), \"API error (404\")\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"API error (\") {\n        // parse status from message; 404 → pull model, 400 → fix payload, 401 → fix auth\n    }\n    return err\n}","preventionTips":["Read the body included in the error for the server's reason","Pull required models before deploying","Validate model names and tool schemas against the endpoint version","Check proxy/auth configuration when a proxy fronts Ollama"],"tags":["http-status","api-error","ollama"],"backgroundTag":"http-non-200","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}