{"record":{"id":"ff2daa88d2cf70a0","repo":"micro/go-micro","slug":"failed-to-parse-response-w-ff2daa","errorCode":null,"errorMessage":"failed to parse response: %w","messagePattern":"failed to parse response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/mistral/mistral.go","lineNumber":200,"sourceCode":"\t}\n\n\tvar chatResp struct {\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\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{Reply: choice.Message.Content}\n\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}\n\t\tresponse.ToolCalls = append(response.ToolCalls, ai.ToolCall{\n\t\t\tID:    tc.ID,\n\t\t\tName:  tc.Function.Name,\n\t\t\tInput: input,\n\t\t})","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/mistral/mistral.go#L182-L218","documentation":"This error wraps a json.Unmarshal failure in the Mistral provider's callAPI. It is thrown when the HTTP response body returned by the Mistral chat API cannot be decoded into the expected chat completion struct. It usually means the server returned HTML, an auth/limit error page, or a response shape the struct does not expect.","triggerScenarios":"json.Unmarshal on respBody fails in callAPI — e.g. the API returned a non-JSON body (HTML error page, proxy response), malformed JSON, or a schema drift (e.g. tool_calls field with unexpected types) that no longer matches the anonymous struct.","commonSituations":"Wrong or revoked API key causing an HTML/JSON error envelope with a different shape; a corporate proxy or captive portal injecting HTML; Mistral API schema changes (new/renamed fields with incompatible types); hitting an error endpoint on api.mistral.ai.","solutions":["Log the raw respBody and httpResp.Status before unmarshalling to see what was actually returned","Verify the MISTRAL API key is valid and the request is reaching api.mistral.ai (not a proxy/error page)","Unmarshal into json.RawMessage / map[string]any first and check the shape before decoding into the struct","Check Mistral API changelog for response schema changes and update the struct fields/types"],"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 (status %s): %w: body=%s\", httpResp.Status, err, string(respBody))\n}","handlingStrategy":"try-catch","validationCode":"// Go has no pre-call validation for the response; validate the request input instead\nif req.Model == \"\" || len(req.Messages) == 0 {\n    return fmt.Errorf(\"invalid request: model and messages are required\")\n}","typeGuard":"func validParsedChatResp(c *chatCompletionResponse) bool {\n    return c != nil // parse errors are surfaced via err; empty choices handled separately\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    var parseErr *fmt.WrapError\n    if strings.Contains(err.Error(), \"failed to parse response\") {\n        // log raw body / check API key and proxy, then retry or fail fast\n    }\n    return err\n}","preventionTips":["Log raw response bodies on parse failures","Verify API keys and proxy setups before deploying","Pin and test against the current Mistral API schema","Wrap unmarshal errors with status and body context"],"tags":["json","http-response","parsing","mistral"],"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"}