Skip to content

AI_JSONParseError should support retry for mid-stream parse errors #12595

Closed

Description

@konard

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:

  1. Has no isRetryable property (only APICallError has it)
  2. Is never retried by the SDK's built-in retryWithExponentialBackoff mechanism
  3. 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:

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.

See: link-assistant/agent#169

Activity

  1. ghost added
    ai/corecore functions like generateText, streamText, etc. Provider utils, and provider spec.
    on Feb 14, 2026
  2. added
    ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` label
    provider/gatewayIssues related to the @ai-sdk/gateway provider
    and removed on Mar 6, 2026
  3. lgrammel commented on Jul 2, 2026

    @lgrammel
    Collaborator

    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"
    }
  4. added theissue type on Jul 2, 2026
  5. aayush-kapoor commented on Oct 9, 2026

    @aayush-kapoor
    Collaborator

    this is fixed in #18718

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai/corecore functions like generateText, streamText, etc. Provider utils, and provider spec.ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelprovider/gatewayIssues related to the @ai-sdk/gateway providertask-identify-issue-type-done

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions