{"record":{"id":"8a608f6e9dfed54c","repo":"siyuan-note/siyuan","slug":"streamed-response-text-does-not-match-the-terminal","errorCode":null,"errorMessage":"streamed response text does not match the terminal response","messagePattern":"streamed response text does not match the terminal response","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai_completion.go","lineNumber":618,"sourceCode":"\t\tif current.Function.Arguments != item.Arguments && strings.HasPrefix(item.Arguments, current.Function.Arguments) {\n\t\t\tdelta.Function.Arguments = strings.TrimPrefix(item.Arguments, current.Function.Arguments)\n\t\t\tchanged = delta.Function.Arguments != \"\" || changed\n\t\t}\n\t\tif changed {\n\t\t\tstream.pending = append(stream.pending, openai.ChatCompletionStreamResponse{\n\t\t\t\tObject: \"chat.completion.chunk\",\n\t\t\t\tChoices: []openai.ChatCompletionStreamChoice{{\n\t\t\t\t\tIndex: 0,\n\t\t\t\t\tDelta: openai.ChatCompletionStreamChoiceDelta{ToolCalls: []openai.ToolCall{delta}},\n\t\t\t\t}},\n\t\t\t})\n\t\t}\n\t}\n\n\tfullContent := responseDisplayText(response)\n\tif currentContent := stream.responseContent.String(); fullContent != currentContent &&\n\t\t!strings.HasPrefix(fullContent, currentContent) {\n\t\treturn errors.New(\"streamed response text does not match the terminal response\")\n\t}\n\tif currentContent := stream.responseContent.String(); fullContent != currentContent && strings.HasPrefix(fullContent, currentContent) {\n\t\tmissing := strings.TrimPrefix(fullContent, currentContent)\n\t\tif missing != \"\" {\n\t\t\tstream.pending = append(stream.pending, openai.ChatCompletionStreamResponse{\n\t\t\t\tObject: \"chat.completion.chunk\",\n\t\t\t\tChoices: []openai.ChatCompletionStreamChoice{{\n\t\t\t\t\tIndex: 0,\n\t\t\t\t\tDelta: openai.ChatCompletionStreamChoiceDelta{Content: missing},\n\t\t\t\t}},\n\t\t\t})\n\t\t}\n\t}\n\n\tfinishReason := responseFinishReason(response)\n\tif hasResponseFunctionCall(response.Output) && finishReason == openai.FinishReasonStop {\n\t\tfinishReason = openai.FinishReasonToolCalls\n\t}","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/openai_completion.go#L600-L636","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Capture raw SSE events and verify deltas concatenate to the final output_text","Update/replace the OpenAI-compatible gateway causing delta rewriting or loss","Retry the request; intermittent divergence is usually an infrastructure fault","Avoid mixing refusal/text expectations; check whether a content_filter refusal altered the output shape"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err != nil {\n    if strings.Contains(err.Error(), \"text does not match\") {\n        return errProviderProtocol // discard streamed text, retry or fall back\n    }\n    return err\n}","preventionTips":["Keep the SSE path free of transforming proxies","Update SDK when provider event schema changes","Fall back to non-streaming mode if a provider repeatedly diverges"],"tags":["openai","streaming","text-mismatch","consistency-check"],"backgroundTag":"unexpected-api-response-shape","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"}