{"record":{"id":"a3a30a15c0e3138d","repo":"siyuan-note/siyuan","slug":"tool-arguments-are-not-valid-json-w","errorCode":null,"errorMessage":"tool arguments are not valid JSON: %w","messagePattern":"tool arguments are not valid JSON: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/tools.go","lineNumber":342,"sourceCode":"\t\treturn joined\n\t}\n\tif result.HasStructuredContent() {\n\t\tif data, err := json.Marshal(result.StructuredContent); err == nil {\n\t\t\treturn string(data)\n\t\t}\n\t}\n\treturn \"(empty result)\"\n}\n\n// parseToolArgs 在流结束后解析完整的工具参数，避免把损坏的 JSON 误报为缺少 schema 字段。\nfunc parseToolArgs(argsJSON string) (map[string]any, error) {\n\tif strings.TrimSpace(argsJSON) == \"\" {\n\t\treturn map[string]any{}, nil\n\t}\n\n\tvar args map[string]any\n\tif err := json.Unmarshal([]byte(argsJSON), &args); err != nil {\n\t\treturn nil, fmt.Errorf(\"tool arguments are not valid JSON: %w\", err)\n\t}\n\tif args == nil {\n\t\treturn nil, fmt.Errorf(\"tool arguments must be a JSON object\")\n\t}\n\treturn args, nil\n}\n","sourceCodeStart":324,"sourceCodeEnd":349,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/tools.go#L324-L349","documentation":"parseToolArgs unmarshals the tool_call function arguments string produced by the model; if json.Unmarshal fails it wraps the JSON syntax error. Empty/whitespace arguments are tolerated (treated as an empty object), so only genuinely malformed JSON reaches this error.","triggerScenarios":"The model's streamed arguments JSON is truncated (stream cut off), contains a syntax error (unterminated string/brace, trailing comma, single quotes), or the arguments field was serialized incorrectly by a proxy/adapter.","commonSituations":"Network interruption mid-stream truncating the arguments buffer, weak small models emitting invalid JSON, middleman API adapters rewriting or chunking tool_call arguments incorrectly.","solutions":["Check the wrapped %w JSON syntax error to locate the malformed position","Retry the request — truncation is often transient (stream cut)","Inspect the raw arguments string logged from the model to confirm whether output was truncated","Use a model/provider with more reliable tool-call JSON generation, or enable the provider's structured tool-call mode"],"exampleFix":"// before: model emits truncated JSON\n{\"query\": \"siyuan notes\", \"limit\": 1\n// after: complete valid JSON must be produced\n{\"query\": \"siyuan notes\", \"limit\": 1}","handlingStrategy":"try-catch","validationCode":"var probe map[string]any\nif strings.TrimSpace(argsJSON) != \"\" && json.Unmarshal([]byte(argsJSON), &probe) != nil {\n    // malformed arguments — repair or reject before invoking the tool\n}","typeGuard":"func validArgsJSON(s string) bool {\n    if strings.TrimSpace(s) == \"\" { return true }\n    var m map[string]any\n    return json.Unmarshal([]byte(s), &m) == nil && m != nil\n}","tryCatchPattern":"args, err := parseToolArgs(tc.Function.Arguments)\nif err != nil {\n    return executedToolResult{Text: \"invalid capability arguments: \" + err.Error(), IsError: true}\n}","preventionTips":["Log raw model arguments to detect provider-side truncation","Retry once on truncation-shaped errors (unexpected EOF)","Prefer providers/modes with native structured tool-call output"],"tags":["ai","json","tool-calling","parsing"],"backgroundTag":"json-parse-error","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}