{"record":{"id":"50ad6e17bd02025c","repo":"siyuan-note/siyuan","slug":"response-ended-with-an-incomplete-function-call","errorCode":null,"errorMessage":"response ended with an incomplete function call","messagePattern":"response ended with an incomplete function call","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai_completion.go","lineNumber":557,"sourceCode":"\t\t\treturn openai.ChatCompletionStreamResponse{}, responseEventError(event, \"response stream failed\")\n\t\t}\n\t\treturn response, nil\n\t}\n}\n\nfunc (stream *OpenAICompletionStream) queueResponseTerminal(response openai.CreateResponseResponse) error {\n\tif err := responseResultError(response); err != nil {\n\t\treturn err\n\t}\n\tterminalToolCalls := map[int]struct{}{}\n\tfor index, raw := range response.Output {\n\t\titem, ok := responseOutputItem(raw)\n\t\tif !ok || item.Type != \"function_call\" {\n\t\t\tcontinue\n\t\t}\n\t\tif item.Status == \"incomplete\" || item.Status == \"in_progress\" ||\n\t\t\t(response.Status == openai.ResponseStatusIncomplete && item.Status != \"completed\") {\n\t\t\treturn errors.New(\"response ended with an incomplete function call\")\n\t\t}\n\t\tterminalToolCalls[index] = struct{}{}\n\t}\n\tfor index := range stream.responseToolCalls {\n\t\tif _, ok := terminalToolCalls[index]; !ok {\n\t\t\treturn errors.New(\"streamed function call is missing from the terminal response\")\n\t\t}\n\t}\n\toutput, err := MarshalOpenAIResponseOutput(response.Output)\n\tif err != nil {\n\t\treturn err\n\t}\n\tstream.responseOutput = output\n\n\tfor index, raw := range response.Output {\n\t\titem, ok := responseOutputItem(raw)\n\t\tif !ok || item.Type != \"function_call\" {\n\t\t\tcontinue","sourceCodeStart":539,"sourceCodeEnd":575,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai_completion.go#L539-L575","documentation":"queueResponseTerminal rejects terminal responses whose function_call output items are not completed: item status incomplete/in_progress, or the overall response status is incomplete while the function call did not complete. This prevents the caller from receiving tool_calls whose arguments are truncated and unparseable.","triggerScenarios":"response.status == \"incomplete\" (e.g. IncompleteDetails.Reason = max_output_tokens or content_filter) and the response.Output contains a function_call item whose status is not \"completed\"; also function_call items still marked in_progress.","commonSituations":"max_output_tokens set too low so JSON tool arguments get cut mid-string; content filter aborting mid tool call; provider outage leaving function calls unfinished; model emitting very long arguments.","solutions":["Increase max_output_tokens so the function call arguments complete","Retry the request; content-filter or transient incompleteness may not recur","Inspect response.IncompleteDetails.Reason to decide between token-limit vs content-filter remediation","Reduce argument size (smaller tool payload, split the task) so the call fits in the budget","Handle the error by not executing the tool call — arguments may be invalid JSON"],"exampleFix":"// before: small budget truncates args\nMaxCompletionTokens: 256\n// after\nMaxCompletionTokens: 4096","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := runCompletion(); err != nil {\n    if strings.Contains(err.Error(), \"incomplete function call\") {\n        // do not execute tool call; raise token budget and retry\n        req.MaxCompletionTokens *= 4\n        return retryCompletion(ctx, req)\n    }\n    return err\n}","preventionTips":["Size max_output_tokens generously for tool-calling requests","Keep tool argument schemas compact","Check IncompleteDetails.Reason to adapt retries","Never execute tool calls from incomplete responses"],"tags":["openai","tool-calls","incomplete-response","streaming"],"backgroundTag":"truncated-tool-call","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"}