{"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":560,"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":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/afa823b6b4e4f183511e0bc0a3be93caa94c7c97/kernel/util/openai_completion.go#L542-L578","documentation":"After the terminal response, the adapter compares responseDisplayText(response) with the text accumulated from content deltas. The final text must be identical to, or a strict extension of, what was streamed (an extension is flushed as a final delta). If the final text does not start with the streamed text, the visible conversation would silently change on completion, so the stream fails.","triggerScenarios":"Providers that apply filtering, deduplication, or rewriting only at finalization; gateways assembling the final message from a different path than the deltas; safety systems trimming or editing text post-hoc.","commonSituations":"Content-moderation rewrites on gateways; providers stitching outputs from multiple regions; custom response-assembly middleware between client and model.","solutions":["Retry the request once — finalization rewrites are often transient.","Switch that provider to non-streaming requests so the terminal text is the only text.","Upgrade or replace the provider that rewrites streamed content.","Capture an SSE trace and report the divergence."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"On this error, discard the streamed text and rerun the request non-streaming so the user sees only the terminal text; do not merge the two versions.","preventionTips":["Avoid providers that rewrite content at finalization for streamed UIs","Prefer non-streaming when post-hoc filtering is active","Monitor streamed-versus-final text divergence per provider"],"tags":["streaming","openai","responses-api","content-mismatch","consistency"],"backgroundTag":"stream-state-mismatch","analyzedSha":"afa823b6b4e4f183511e0bc0a3be93caa94c7c97","analyzedAt":"2026-08-18T17:04:10.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}