Skip to content

Comments

fix(perplexity): handle [DONE] SSE event and add stream error resilience#1526

Closed
anshulbasia27 wants to merge 1 commit intoPortkey-AI:mainfrom
anshulbasia27:fix/perplexity-sonar-streaming-done-handler
Closed

fix(perplexity): handle [DONE] SSE event and add stream error resilience#1526
anshulbasia27 wants to merge 1 commit intoPortkey-AI:mainfrom
anshulbasia27:fix/perplexity-sonar-streaming-done-handler

Conversation

@anshulbasia27
Copy link

Summary

The sonar-deep-research model from Perplexity sends a literal data: [DONE] SSE event at the end of the stream. The existing PerplexityAIChatCompleteStreamChunkTransform strips the data: prefix and passes [DONE] directly to JSON.parse, causing a SyntaxError that terminates the stream before the answer content is delivered.

This is the same fix already merged in gateway-enterprise-node via PR #1105, now ported to the open-source gateway.

Changes

  • Guard against [DONE] before JSON.parse — prevents SyntaxError crash that silently kills the stream
  • Wrap transform in try/catch — malformed chunks during the research phase no longer terminate the whole stream
  • Log raw chunk on parse errorconsole.error now includes the raw chunk value for easier debugging
  • Make usage and choices optional on PerplexityAIChatCompletionStreamChunk interface — handles intermediate research-phase chunks that omit these fields
  • Default finish_reason to null instead of undefined — conforms to OpenAI spec (prevents field being silently dropped by JSON.stringify)

Related

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant