{"record":{"id":"8537dc5ff3013ea4","repo":"wavetermdev/waveterm","slug":"invalid-accumulated-tool-input-json-w","errorCode":null,"errorMessage":"invalid accumulated tool input JSON: %w","messagePattern":"invalid accumulated tool input JSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/aiusechat/anthropic/anthropic-backend.go","lineNumber":335,"sourceCode":"\t// The stream may send empty \"\" chunks; ignore if zero-length\n\tif s == \"\" {\n\t\treturn\n\t}\n\tp.buf.WriteString(s)\n}\n\nfunc (p *partialJSON) Bytes() []byte { return p.buf.Bytes() }\n\nfunc (p *partialJSON) FinalObject() (json.RawMessage, error) {\n\traw := p.buf.Bytes()\n\t// If empty, treat as \"{}\"\n\tif len(bytes.TrimSpace(raw)) == 0 {\n\t\treturn json.RawMessage(`{}`), nil\n\t}\n\t// The accumulated content should be a valid JSON object string; parse it.\n\tvar v interface{}\n\tif err := json.Unmarshal(raw, &v); err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid accumulated tool input JSON: %w\", err)\n\t}\n\t// Ensure it's an object per Anthropic contract\n\tswitch v.(type) {\n\tcase map[string]interface{}:\n\t\treturn json.RawMessage(raw), nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"tool input is not an object\")\n\t}\n}\n\n// sanitizeHostnameInError removes the Wave cloud hostname from error messages\nfunc sanitizeHostnameInError(err error) error {\n\tif err == nil {\n\t\treturn nil\n\t}\n\terrStr := err.Error()\n\tparsedURL, parseErr := url.Parse(uctypes.DefaultAIEndpoint)\n\tif parseErr == nil && parsedURL.Host != \"\" && strings.Contains(errStr, parsedURL.Host) {","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/aiusechat/anthropic/anthropic-backend.go#L317-L353","documentation":"FinalObject on the streaming partialJSON accumulator validates the accumulated tool_use input before emitting it. The streamed input_json_delta chunks must concatenate into valid JSON; when the accumulated buffer fails json.Unmarshal, this error is returned. It indicates corrupted or incomplete tool argument streaming.","triggerScenarios":"During an Anthropic streaming response, the concatenated input_json_delta fragments for a tool_use block do not form valid JSON when FinalObject is called — e.g. deltas dropped mid-stream, invalid control characters in the JSON, or a provider-side truncation.","commonSituations":"Network interruptions truncating the SSE stream; upstream Anthropic API errors that cut tool input mid-JSON; custom/intercepting proxies mangling the body; client bug in reassembling delta chunks.","solutions":["Retry the request — a corrupted stream is usually transient.","Inspect the accumulated raw buffer in the wrapped json error to find the exact syntax problem.","Check for middleware/proxies or loggers that might alter or truncate SSE payloads.","Verify no delta events were dropped in your SSE handler before FinalObject is called.","If reproducible with a specific model/request, report/upstream since the provider emitted invalid JSON."],"exampleFix":"// handling pattern\nraw, err := acc.FinalObject()\nif err != nil {\n    log.Printf(\"tool input corrupted, retrying: %v\", err)\n    return runStep(ctx) // retry the stream\n}","handlingStrategy":"retry","validationCode":"// after accumulation, before use:\nvar probe interface{}\nif err := json.Unmarshal(raw, &probe); err != nil {\n    log.Printf(\"stream corrupted: %v\", err) // then retry the request\n}","typeGuard":"func isValidJSON(raw []byte) bool { var v interface{}; return json.Unmarshal(raw, &v) == nil }","tryCatchPattern":"input, err := acc.FinalObject()\nif err != nil {\n    var jerr *json.SyntaxError\n    if errors.As(err, &jerr) {\n        return retryChatStep(ctx) // transient stream corruption\n    }\n    return err\n}","preventionTips":["Retry streaming requests on mid-stream failures","Ensure SSE handlers never drop or reorder input_json_delta events","Audit proxies/interceptors that might rewrite response bodies","Log accumulated raw buffers on failure to detect systematic providers issues"],"tags":["streaming","json","tool-use","anthropic"],"backgroundTag":"invalid-json-payload","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}