{"record":{"id":"1b33f7dde2119722","repo":"micro/go-micro","slug":"failed-to-parse-stream-chunk-w","errorCode":null,"errorMessage":"failed to parse stream chunk: %w","messagePattern":"failed to parse stream chunk: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":308,"sourceCode":"\t\t\tType  string `json:\"type\"`\n\t\t\tDelta struct {\n\t\t\t\tType       string `json:\"type\"`\n\t\t\t\tText       string `json:\"text\"`\n\t\t\t\tStopReason string `json:\"stop_reason\"`\n\t\t\t} `json:\"delta\"`\n\t\t\tMessage struct {\n\t\t\t\tUsage struct {\n\t\t\t\t\tInputTokens  int `json:\"input_tokens\"`\n\t\t\t\t\tOutputTokens int `json:\"output_tokens\"`\n\t\t\t\t} `json:\"usage\"`\n\t\t\t} `json:\"message\"`\n\t\t\tUsage *struct {\n\t\t\t\tInputTokens  int `json:\"input_tokens\"`\n\t\t\t\tOutputTokens int `json:\"output_tokens\"`\n\t\t\t} `json:\"usage\"`\n\t\t}\n\t\tif err := json.Unmarshal([]byte(data), &chunk); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse stream chunk: %w\", err)\n\t\t}\n\t\tswitch chunk.Type {\n\t\tcase \"content_block_delta\":\n\t\t\tif chunk.Delta.Type == \"text_delta\" && chunk.Delta.Text != \"\" {\n\t\t\t\treturn &ai.Response{Reply: chunk.Delta.Text}, nil\n\t\t\t}\n\t\tcase \"message_start\":\n\t\t\tif chunk.Message.Usage.InputTokens > 0 || chunk.Message.Usage.OutputTokens > 0 {\n\t\t\t\treturn &ai.Response{Usage: usage(chunk.Message.Usage.InputTokens, chunk.Message.Usage.OutputTokens)}, nil\n\t\t\t}\n\t\tcase \"message_delta\":\n\t\t\tif chunk.Delta.StopReason != \"\" || chunk.Usage != nil {\n\t\t\t\tresponse := &ai.Response{StopReason: chunk.Delta.StopReason}\n\t\t\t\tif chunk.Usage != nil {\n\t\t\t\t\tresponse.Usage = usage(chunk.Usage.InputTokens, chunk.Usage.OutputTokens)\n\t\t\t\t}\n\t\t\t\treturn response, nil\n\t\t\t}","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L290-L326","documentation":"Returned by streamReader.Recv when a JSON data line from the Anthropic SSE stream cannot be unmarshaled into the expected chunk structure. This means the server sent a data payload that doesn't match the expected event schema, or the SSE line was truncated/corrupted.","triggerScenarios":"json.Unmarshal of an SSE \"data:\" payload fails: an event type with an unexpected shape (e.g. a new Anthropic event like \"ping\" carrying fields not in the struct is fine, but a changed schema or a non-JSON body from a proxy is not), or bufio.Scanner tokenizing an oversized/malformed line.","commonSituations":"Routing the stream through a proxy/gateway that rewrites responses, an API version change altering chunk fields, or the endpoint returning an HTML error page inside the stream.","solutions":["Log the raw `data` line alongside the unmarshal error to see the unexpected payload shape.","Pin anthropic-version header to a known-good version and confirm the library struct matches that version's schema.","Bypass any HTTP proxy/middleware to test direct connectivity to api.anthropic.com.","Update the library to the latest version so chunk structs match current Anthropic streaming events."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func parseChunk(data []byte) (chunkType string, err error) {\n\tvar probe struct{ Type string `json:\"type\"` }\n\tif err := json.Unmarshal(data, &probe); err != nil {\n\t\treturn \"\", fmt.Errorf(\"unparsable SSE chunk %q: %w\", data, err)\n\t}\n\treturn probe.Type, nil\n}","tryCatchPattern":"resp, err := stream.Recv()\nif err != nil {\n\tvar parseErr *json.UnmarshalTypeError\n\tif errors.As(err, &parseErr) {\n\t\tlog.Printf(\"skipping malformed chunk: %v\", err) // log & skip or abort stream\n\t}\n}","preventionTips":["Keep the library updated so chunk structs match current Anthropic SSE events","Test streams through your exact proxy chain, not just direct","Log raw SSE lines in debug mode for diagnosis"],"tags":["json","parsing","sse","anthropic","streaming"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}