{"record":{"id":"6b1902c8ed50bd59","repo":"micro/go-micro","slug":"failed-to-parse-response-w-6b1902","errorCode":null,"errorMessage":"failed to parse response: %w","messagePattern":"failed to parse response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/openai/openai.go","lineNumber":349,"sourceCode":"\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\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\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{InputTokens: chatResp.Usage.PromptTokens, OutputTokens: chatResp.Usage.CompletionTokens, TotalTokens: chatResp.Usage.TotalTokens},\n\t}\n\n\t// Extract tool calls\n\tfor _, tc := range choice.Message.ToolCalls {\n\t\tvar input map[string]any\n\t\tif err := json.Unmarshal([]byte(tc.Function.Arguments), &input); err != nil {\n\t\t\tinput = map[string]any{}\n\t\t}","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/openai/openai.go#L331-L367","documentation":"The library received a 2xx response body but json.Unmarshal could not decode it into the expected chatCompletion response struct. This means the payload was not valid JSON or did not match the expected schema (e.g. choices was an object instead of an array).","triggerScenarios":"callAPI got httpResp.StatusCode == 200 but respBody is not JSON or has unexpected shape: an HTML error/login page from a misconfigured proxy, a truncated body, or an OpenAI-compatible server returning a different schema.","commonSituations":"Pointing BaseURL at a non-OpenAI service or gateway that returns HTML on success, a gateway stripping/chunk-encoding the body incorrectly, using an OpenAI-compatible provider (e.g. self-hosted) whose response schema diverges.","solutions":["Print/log respBody on this error to see what the server actually returned","Confirm BaseURL points at an OpenAI-compatible endpoint (path /v1/chat/completions) and not a UI page","Disable/fix proxies or gateways that inject HTML into responses","If using a compatible-but-different provider, check its response schema against the library's struct"],"exampleFix":"// before\napiURL := cfg.OpenAIBaseURL // accidentally https://internal-portal/login\n// after\napiURL := \"https://api.openai.com\" // real OpenAI-compatible API host","handlingStrategy":"validation","validationCode":"u, err := url.Parse(cfg.BaseURL)\nif err != nil || !strings.HasSuffix(u.Path, \"/v1\") && u.Path != \"\" {\n    // ensure it points at an OpenAI-compatible API root, not a web UI\n}\n// sanity ping\nresp, err := http.Get(cfg.BaseURL + \"/v1/models\")\nct := resp.Header.Get(\"Content-Type\")\nif !strings.Contains(ct, \"application/json\") {\n    return fmt.Errorf(\"endpoint does not return JSON (Content-Type: %s)\", ct)\n}","typeGuard":null,"tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"failed to parse response\") {\n    log.Printf(\"non-JSON payload from endpoint; check BaseURL/proxy: %v\", err)\n    return err\n}","preventionTips":["Point BaseURL only at OpenAI-compatible JSON API roots","Disable intercepting proxies that inject HTML into 200 responses","Verify compatible providers against the OpenAI chat schema","Log raw bodies on parse failures to speed diagnosis"],"tags":["json","openai","response-parsing","schema-mismatch"],"backgroundTag":"response-parse-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}