From 5db24e4bbc74395ef4314661fb3da30bc6a744ce Mon Sep 17 00:00:00 2001 From: Zhuyi Xue <527862+zyxue@users.noreply.github.com> Date: Thu, 8 May 2025 15:09:39 -0700 Subject: [PATCH] handle data:[DONE] more robustly although https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#request-body says >stream boolean Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message but what I see is `data:[DONE]` (without space), e.g. ``` curl -q --location 'http://localhost:8080/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview' \ --header 'Authorization: Bearer ' \ --header 'X-Forwarded-Client-Cert: secret' \ --header 'Content-Type: application/json' \ --data '{ "stream": true, "messages": [ { "role": "user", "content": "hello." } ] }' data:{"choices":[],"created":0,"id":"","model":"","object":"","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}]} data:{"choices":[{"content_filter_results":{},"delta":{"refusal":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1746741184,"id":"chatcmpl-BV3SqfEtCuk3f8fW5x7azEkELkJZ0","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_ee1d74bde0"} ... data:{"choices":[{"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"delta":{"content":"?"},"finish_reason":null,"index":0}],"created":1746741184,"id":"chatcmpl-BV3SqfEtCuk3f8fW5x7azEkELkJZ0","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_ee1d74bde0"} data:{"choices":[{"content_filter_results":{},"delta":{},"finish_reason":"stop","index":0}],"created":1746741184,"id":"chatcmpl-BV3SqfEtCuk3f8fW5x7azEkELkJZ0","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_ee1d74bde0"} data:[DONE] ``` --- core/llm/stream.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/llm/stream.ts b/core/llm/stream.ts index 0a014d931..78a4b60e3 100644 --- a/core/llm/stream.ts +++ b/core/llm/stream.ts @@ -58,7 +58,7 @@ function parseDataLine(line: string): any { } function parseSseLine(line: string): { done: boolean; data: any } { - if (line.startsWith("data: [DONE]")) { + if (line.startsWith("data:[DONE]") || line.startsWith("data: [DONE]")) { return { done: true, data: undefined }; } if (line.startsWith("data:")) {