{"record":{"id":"65978d253a0cf431","repo":"micro/go-micro","slug":"failed-to-parse-response-w-65978d","errorCode":null,"errorMessage":"failed to parse response: %w","messagePattern":"failed to parse response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/together/together.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/together/together.go#L182-L218","documentation":"callAPI received HTTP 200 from Together but the body could not be decoded into the expected chat-completions shape ({choices:[{message:{content,tool_calls}}]}). json.Unmarshal failed, so the response was malformed or structurally unexpected. The underlying *json.UnmarshalTypeError / *json.SyntaxError is wrapped with %w.","triggerScenarios":"The API returns 200 with HTML/error text, an unexpected JSON schema (e.g. missing or mistyped fields like choices not being an array), or a truncated body.","commonSituations":"BaseURL pointed at a compatible-but-different gateway that returns a different schema; a proxy/CDN injecting an HTML error page with status 200; API version drift changing the response shape.","solutions":["Log respBody on this error to see what was actually returned","Verify BaseURL points to the real Together API (or a gateway with an identical chat-completions schema)","Check for API version changes and pin the endpoint path /v1/chat/completions","Use errors.As(err, &json.UnmarshalTypeError) to identify the exact mismatched field"],"exampleFix":"// before\nresp, err := p.Generate(ctx, req)\n// after\nresp, err := p.Generate(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"failed to parse response\") {\n    log.Printf(\"unexpected Together response body, check BaseURL/gateway\")\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":"func validChoices(b []byte) bool {\n    var v struct { Choices []json.RawMessage `json:\"choices\"` }\n    return json.Unmarshal(b, &v) == nil && len(v.Choices) > 0\n}","tryCatchPattern":"resp, err := p.Generate(ctx, req)\nif err != nil {\n    var jsonErr *json.UnmarshalTypeError\n    if errors.As(err, &jsonErr) {\n        log.Printf(\"schema mismatch at field %s\", jsonErr.Field)\n    }\n}","preventionTips":["Log raw response bodies on parse failures","Pin the API endpoint and schema; watch provider changelogs","Avoid proxies that rewrite 200 responses","Integration-test against the real endpoint"],"tags":["json","parsing","ai-provider","together"],"backgroundTag":"schema-validation-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}