Problem
When using dify-ai-provider with Dify API, SSE ping events (event: ping\n\n) cause a parse error in @ai-sdk/provider-utils's parseJsonEventStream. The error appears as a stream error in the console.
Root Cause
Dify API sends pings in SSE event-type format:
But @ai-sdk/provider-utils parseJsonEventStream (used by createEventSourceResponseHandler) only destructures the data field from EventSourceMessage, discarding the event field:
// @ai-sdk/provider-utils parse-json-event-stream.ts
async transform({ data }, controller) {
if (data === '[DONE]') return;
controller.enqueue(await safeParseJSON({ text: data, schema }));
}
When event: ping\n\n arrives:
- EventSourceParserStream produces
{ event: "ping", data: "" }
{ data } destructuring yields empty string ""
safeParseJSON({ text: "", schema }) fails — empty string is not valid JSON
- Transform enqueues error event
Why ping format changed
Dify uses event: ping (SSE event-type field) since commit 7753ba2d37 (April 2024). The ping string is a valid SSE event type per the SSE spec, not JSON data.
Suggested Fix
In the SSE transform within dify-chat-language-model.ts, before calling safeParseJSON, check if the message is a ping by inspecting the event field (or detecting empty data with a ping event type), and skip/enqueue a synthetic success result:
async transform({ event, data }, controller) {
if (data === '[DONE]') return;
// Handle ping events that have no JSON data
if (event === 'ping' || (data === '' && event === 'ping')) {
controller.enqueue({ success: true, value: { event: 'ping' } });
return;
}
controller.enqueue(await safeParseJSON({ text: data, schema }));
}
Reproduction
- Configure any Dify app (chat/agent/workflow/completion) with streaming enabled
- Send a message and observe the console
- The
event: ping SSE frames trigger parse errors from @ai-sdk/provider-utils
The existing switch case at dify-chat-language-model.ts:480 correctly handles { event: 'ping' }, but it's never reached because the transform fails before the case can match.
Problem
When using
dify-ai-providerwith Dify API, SSE ping events (event: ping\n\n) cause a parse error in@ai-sdk/provider-utils'sparseJsonEventStream. The error appears as a stream error in the console.Root Cause
Dify API sends pings in SSE event-type format:
But
@ai-sdk/provider-utilsparseJsonEventStream(used bycreateEventSourceResponseHandler) only destructures thedatafield fromEventSourceMessage, discarding theeventfield:When
event: ping\n\narrives:{ event: "ping", data: "" }{ data }destructuring yields empty string""safeParseJSON({ text: "", schema })fails — empty string is not valid JSONWhy ping format changed
Dify uses
event: ping(SSE event-type field) since commit7753ba2d37(April 2024). Thepingstring is a valid SSE event type per the SSE spec, not JSON data.Suggested Fix
In the SSE transform within
dify-chat-language-model.ts, before callingsafeParseJSON, check if the message is a ping by inspecting theeventfield (or detecting emptydatawith a ping event type), and skip/enqueue a synthetic success result:Reproduction
event: pingSSE frames trigger parse errors from@ai-sdk/provider-utilsThe existing switch case at
dify-chat-language-model.ts:480correctly handles{ event: 'ping' }, but it's never reached because the transform fails before the case can match.