{"record":{"id":"5264679214e5c4b6","repo":"micro/go-micro","slug":"failed-to-parse-response-w-526467","errorCode":null,"errorMessage":"failed to parse response: %w","messagePattern":"failed to parse response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/ollama/ollama.go","lineNumber":275,"sourceCode":"\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\"`\n\t\t\t\t} `json:\"tool_calls\"`\n\t\t\t} `json:\"message\"`\n\t\t} `json:\"choices\"`\n\t}\n\n\tif err := json.Unmarshal(respBody, &chatResp); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to parse response: %w\", err)\n\t}\n\tif len(chatResp.Choices) == 0 {\n\t\treturn nil, nil, fmt.Errorf(\"no response from API\")\n\t}\n\n\tchoice := chatResp.Choices[0]\n\tresponse := &ai.Response{\n\t\tReply: choice.Message.Content,\n\t\tUsage: ai.Usage{\n\t\t\tInputTokens:  chatResp.Usage.PromptTokens,\n\t\t\tOutputTokens: chatResp.Usage.CompletionTokens,\n\t\t\tTotalTokens:  chatResp.Usage.TotalTokens,\n\t\t},\n\t}\n\n\tvar rawToolCalls []map[string]any\n\tfor _, tc := range choice.Message.ToolCalls {\n\t\tvar input map[string]any","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/ollama/ollama.go#L257-L293","documentation":"This error wraps a json.Unmarshal failure when decoding the Ollama OpenAI-compatible chat response in callOpenAI. It means the response body could not be parsed into the expected choices struct — commonly because the server returned an error page, plain-text error, or a schema the struct does not match.","triggerScenarios":"json.Unmarshal(respBody, &chatResp) fails in callOpenAI (called from generateOpenAI) — non-JSON body (HTML from a proxy), truncated response, or Ollama version whose response shape differs from the struct.","commonSituations":"Hitting a non-Ollama endpoint (e.g. the base URL points to a web root returning HTML); an old Ollama version predating the OpenAI-compatibility layer; a reverse proxy error page; response body truncated by a proxy timeout.","solutions":["Log respBody and the status code before unmarshalling to see the actual payload","Verify BaseURL points at the Ollama OpenAI-compatible API (…/v1), not the root or native /api endpoint","Upgrade Ollama to a version supporting the OpenAI-compatible chat completions endpoint","Decode into json.RawMessage/map first and validate the shape before struct decoding"],"exampleFix":"// before\nif err := json.Unmarshal(respBody, &chatResp); err != nil {\n    return nil, nil, fmt.Errorf(\"failed to parse response: %w\", err)\n}\n// after\nif err := json.Unmarshal(respBody, &chatResp); err != nil {\n    return nil, nil, fmt.Errorf(\"failed to parse response: %w: body=%.200s\", err, string(respBody))\n}","handlingStrategy":"try-catch","validationCode":"// verify the endpoint returns JSON before calling\nresp, err := http.Get(strings.TrimRight(baseURL, \"/\") + \"/v1/models\")\nif err == nil {\n    ct := resp.Header.Get(\"Content-Type\")\n    if !strings.Contains(ct, \"application/json\") {\n        return fmt.Errorf(\"endpoint did not return JSON (content-type %s)\", ct)\n    }\n}","typeGuard":"func looksLikeJSON(b []byte) bool {\n    t := bytes.TrimSpace(b)\n    return len(t) > 0 && (t[0] == '{' || t[0] == '[')\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to parse response\") {\n        // inspect raw body: proxy HTML? wrong endpoint? old Ollama?\n    }\n    return err\n}","preventionTips":["Point BaseURL at the /v1 OpenAI-compatible path","Log response bodies on parse errors","Keep Ollama updated to a version with the OpenAI layer","Check for reverse proxies injecting HTML error pages"],"tags":["json","parsing","ollama","http-response"],"backgroundTag":"json-response-parse-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}