siyuan-note/siyuan · error

response ended with an incomplete function call

Error message

response ended with an incomplete function call

What it means

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.

Solutions

  1. Increase max_output_tokens so the function call arguments complete
  2. Retry the request; content-filter or transient incompleteness may not recur
  3. Inspect response.IncompleteDetails.Reason to decide between token-limit vs content-filter remediation
  4. Reduce argument size (smaller tool payload, split the task) so the call fits in the budget
  5. Handle the error by not executing the tool call — arguments may be invalid JSON

Example fix

// before: small budget truncates args
MaxCompletionTokens: 256
// after
MaxCompletionTokens: 4096
Defensive patterns

Strategy: try-catch

Try / catch

if err := runCompletion(); err != nil {
    if strings.Contains(err.Error(), "incomplete function call") {
        // do not execute tool call; raise token budget and retry
        req.MaxCompletionTokens *= 4
        return retryCompletion(ctx, req)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/50ad6e17bd02025c. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/openai_completion.go:557

			return openai.ChatCompletionStreamResponse{}, responseEventError(event, "response stream failed")
		}
		return response, nil
	}
}

func (stream *OpenAICompletionStream) queueResponseTerminal(response openai.CreateResponseResponse) error {
	if err := responseResultError(response); err != nil {
		return err
	}
	terminalToolCalls := map[int]struct{}{}
	for index, raw := range response.Output {
		item, ok := responseOutputItem(raw)
		if !ok || item.Type != "function_call" {
			continue
		}
		if item.Status == "incomplete" || item.Status == "in_progress" ||
			(response.Status == openai.ResponseStatusIncomplete && item.Status != "completed") {
			return errors.New("response ended with an incomplete function call")
		}
		terminalToolCalls[index] = struct{}{}
	}
	for index := range stream.responseToolCalls {
		if _, ok := terminalToolCalls[index]; !ok {
			return errors.New("streamed function call is missing from the terminal response")
		}
	}
	output, err := MarshalOpenAIResponseOutput(response.Output)
	if err != nil {
		return err
	}
	stream.responseOutput = output

	for index, raw := range response.Output {
		item, ok := responseOutputItem(raw)
		if !ok || item.Type != "function_call" {
			continue

View on GitHub (pinned to 9f775e8a12)