siyuan-note/siyuan · error

streamed response text does not match the terminal response

Error message

streamed response text does not match the terminal response

What it means

At completion, the text accumulated from streamed output_text/refusal deltas must equal the terminal response's display text, or at least be a prefix of it (missing tails are backfilled). This error fires when the streamed text diverges from the final response text — the provider contradicted what it already sent.

Solutions

  1. Capture raw SSE events and verify deltas concatenate to the final output_text
  2. Update/replace the OpenAI-compatible gateway causing delta rewriting or loss
  3. Retry the request; intermittent divergence is usually an infrastructure fault
  4. Avoid mixing refusal/text expectations; check whether a content_filter refusal altered the output shape
Defensive patterns

Strategy: try-catch

Try / catch

if err != nil {
    if strings.Contains(err.Error(), "text does not match") {
        return errProviderProtocol // discard streamed text, retry or fall back
    }
    return err
}

Prevention

When it happens

Trigger: responseDisplayText(response) differs from stream.responseContent and does not start with it — e.g. terminal output_text differs from the concatenation of streamed output_text.delta values, or a refusal replaced already-streamed content.

Common situations: Gateways/proxies dropping or rewriting delta events mid-stream; providers that re-generate content between deltas and final response (non-conformant); mixing content types so refusal deltas follow text deltas; SDK schema drift altering GetOutputText parsing.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at kernel/util/openai_completion.go:618

		if current.Function.Arguments != item.Arguments && strings.HasPrefix(item.Arguments, current.Function.Arguments) {
			delta.Function.Arguments = strings.TrimPrefix(item.Arguments, current.Function.Arguments)
			changed = delta.Function.Arguments != "" || changed
		}
		if changed {
			stream.pending = append(stream.pending, openai.ChatCompletionStreamResponse{
				Object: "chat.completion.chunk",
				Choices: []openai.ChatCompletionStreamChoice{{
					Index: 0,
					Delta: openai.ChatCompletionStreamChoiceDelta{ToolCalls: []openai.ToolCall{delta}},
				}},
			})
		}
	}

	fullContent := responseDisplayText(response)
	if currentContent := stream.responseContent.String(); fullContent != currentContent &&
		!strings.HasPrefix(fullContent, currentContent) {
		return errors.New("streamed response text does not match the terminal response")
	}
	if currentContent := stream.responseContent.String(); fullContent != currentContent && strings.HasPrefix(fullContent, currentContent) {
		missing := strings.TrimPrefix(fullContent, currentContent)
		if missing != "" {
			stream.pending = append(stream.pending, openai.ChatCompletionStreamResponse{
				Object: "chat.completion.chunk",
				Choices: []openai.ChatCompletionStreamChoice{{
					Index: 0,
					Delta: openai.ChatCompletionStreamChoiceDelta{Content: missing},
				}},
			})
		}
	}

	finishReason := responseFinishReason(response)
	if hasResponseFunctionCall(response.Output) && finishReason == openai.FinishReasonStop {
		finishReason = openai.FinishReasonToolCalls
	}

View on GitHub (pinned to 9f775e8a12)