Repository navigation
Description
Description
When using streamText with OpenAI-compatible providers (via @ai-sdk/openai-compatible), the AI SDK may throw AI_JSONParseError during stream consumption when a provider or gateway returns malformed SSE data. Currently, this error:
- Has no
isRetryableproperty (onlyAPICallErrorhas it) - Is never retried by the SDK's built-in
retryWithExponentialBackoffmechanism - Occurs after the HTTP request succeeds (HTTP 200), so the request-level retry is already bypassed
These errors are typically transient — caused by SSE chunks being concatenated incorrectly at proxy/gateway level, network issues corrupting stream data mid-flight, or providers temporarily returning invalid JSON in stream.
Evidence from Production
We observed this error when using Kimi K2.5 via the Kilo AI Gateway (api.kilo.ai):
{
"name": "AI_JSONParseError",
"cause": {},
"text": "{\"id\":\"chatcmpl-jQugNdata:{\"id\":\"chatcmpl-iU6vkr3fItZ0Y4rTCmIyAnXO\",\"object\":\"chat.completion.chunk\",...}"
}The data: SSE prefix of the second event was embedded inside the first event's JSON, indicating SSE chunking corruption at the gateway level.
This is a known class of issue — similar reports:
- StreamText error handling still not working properly in v4.0.18 #4099: streamText error handling
- AI_JSONParseError with generateText no tools #8577: AI_JSONParseError with generateText (mentions isRetryable)
- Cloudflare Tunnel + Vercel AI SDK =
[AI_JSONParseError]: JSON parsing failedollama/ollama#5417: Cloudflare Tunnel + Vercel AI SDK = AI_JSONParseError
Current Behavior
The SDK's parsing pipeline (parseJsonEventStream) uses safeParseJSON which returns { success: false, error: JSONParseError } on parse failure. The OpenAI-compatible model handler then emits this as { type: 'error', error: ... } in the stream. There is no way for consumers to retry the stream, skip bad events, or distinguish transient parse errors from permanent ones.
How Other CLI Agents Handle This
OpenAI Codex CLI: Skips unparseable SSE events with continue; and keeps processing. JSON parse errors are logged at debug level. Stream-level errors trigger up to 5 retries with exponential backoff. CodexErr::Json is explicitly marked retryable.
Qwen Code: SDK's parseJsonLineSafe() silently skips failed lines with a warning log.
Proposed Solutions
Option A: Add isRetryable property to AI_JSONParseError for stream context.
Option B: Add onStreamParseError callback to streamText allowing consumers to skip bad events.
Option C: Allow configuring stream-level retry (streamRetries: 3).
Environment
- AI SDK Version: Observed across v4.x and v5.x
- Provider:
@ai-sdk/openai-compatible(Kilo AI Gateway) - Runtime: Bun 1.3.x
Workaround
We detect AI_JSONParseError in our error handler and classify it as retryable with exponential backoff.
Activity
docs: redo case study analysis, file upstream issues #169
lgrammel commentedon Jul 2, 2026
Identify issue type
{ "evidence": [ "The issue requests retry support for `AI_JSONParseError` occurring during streamed SSE parsing after an HTTP 200 response.", "It lists proposed API/behavior additions: `isRetryable` on `AI_JSONParseError`, an `onStreamParseError` callback, or configurable `streamRetries`.", "The described errors are caused by malformed/corrupted SSE data from providers or gateways, not a stated contradiction of documented SDK behavior." ], "rationale": "The report asks for new stream-level retry/error-handling capabilities and ergonomics around malformed streamed JSON, rather than identifying an existing promised SDK behavior that is failing.", "type": "feature", "confidence": "high" }aayush-kapoor commentedon Oct 9, 2026
this is fixed in #18718