fix(perplexity): handle [DONE] SSE event and add stream error resilience#1526
Closed
anshulbasia27 wants to merge 1 commit intoPortkey-AI:mainfrom
Closed
Conversation
The sonar-deep-research model sends a literal `data: [DONE]` SSE event which caused JSON.parse to throw a SyntaxError, terminating the stream before the answer content was delivered. Changes: - Guard against [DONE] before JSON.parse to avoid SyntaxError crash - Wrap transform in try/catch so malformed chunks don't kill the stream - Log raw chunk value alongside parse errors for easier debugging - Make `usage` and `choices` optional on stream chunk interface to handle intermediate research-phase chunks that omit these fields - Default finish_reason to null (instead of undefined) per OpenAI spec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
sonar-deep-researchmodel from Perplexity sends a literaldata: [DONE]SSE event at the end of the stream. The existingPerplexityAIChatCompleteStreamChunkTransformstrips thedata:prefix and passes[DONE]directly toJSON.parse, causing aSyntaxErrorthat terminates the stream before the answer content is delivered.This is the same fix already merged in
gateway-enterprise-nodevia PR #1105, now ported to the open-source gateway.Changes
[DONE]beforeJSON.parse— preventsSyntaxErrorcrash that silently kills the streamtry/catch— malformed chunks during the research phase no longer terminate the whole streamconsole.errornow includes the raw chunk value for easier debuggingusageandchoicesoptional onPerplexityAIChatCompletionStreamChunkinterface — handles intermediate research-phase chunks that omit these fieldsfinish_reasontonullinstead ofundefined— conforms to OpenAI spec (prevents field being silently dropped byJSON.stringify)Related
gateway-enterprise-nodePR working implementation to enable guardrails for realtime api #1105 — same fix90230fc1-2e8e-4e29-b292-463ac6b5a9e2