Skip to content

Commit 8af076e

Browse files
luochen1990LLJY
authored andcommitted
fix(opencode): retry JSONParseError from malformed SSE streams
ai-sdk throws JSONParseError when a provider returns a malformed SSE stream (e.g. chunks spliced together by a gateway, or network-induced truncation). Previously this fell through to the generic Error branch in fromError() and was classified as NamedError.Unknown, bypassing the session retry loop and terminating the turn. Classify JSONParseError as a retryable APIError so the existing exponential backoff path recovers the turn. This mirrors how ZlibError and ECONNRESET are already handled. Closes anomalyco#13579
1 parent b24aaac commit 8af076e

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ import {
1717
} from "@opencode-ai/core/v1/session"
1818

1919
import { NamedError } from "@opencode-ai/core/util/error"
20-
import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
20+
import {
21+
APICallError,
22+
JSONParseError,
23+
convertToModelMessages,
24+
LoadAPIKeyError,
25+
type ModelMessage,
26+
type UIMessage,
27+
} from "ai"
2128
import { Database } from "@opencode-ai/core/database/database"
2229
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
2330
import { NotFoundError } from "@/storage/storage"
@@ -761,6 +768,18 @@ export function fromError(
761768
},
762769
{ cause: e },
763770
).toObject()
771+
// ai-sdk throws JSONParseError on malformed SSE chunks; classify as retryable
772+
// so the existing backoff path recovers (previously fell through to UnknownError).
773+
// See #13579.
774+
case JSONParseError.isInstance(e):
775+
return new APIError(
776+
{
777+
message: `Provider returned malformed JSON stream: ${e.message.slice(0, 200)}`,
778+
isRetryable: true,
779+
},
780+
{ cause: e },
781+
).toObject()
782+
764783
case e instanceof Error:
765784
return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
766785
default:

‎packages/opencode/test/session/retry.test.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, test } from "bun:test"
22
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
33
import { SessionV1 } from "@opencode-ai/core/v1/session"
44
import type { NamedError } from "@opencode-ai/core/util/error"
5-
import { APICallError } from "ai"
5+
import { APICallError, JSONParseError } from "ai"
66
import { setTimeout as sleep } from "node:timers/promises"
77
import { Effect, Schedule, Schema } from "effect"
88
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
@@ -344,6 +344,21 @@ describe("session.retry.retryable", () => {
344344
expect(retryable).toEqual({ message: "Response decompression failed" })
345345
})
346346

347+
test("retries JSONParseError from malformed SSE streams", () => {
348+
// See #13579: malformed SSE chunks should enter retry, not terminate the turn.
349+
const parseError = new JSONParseError({
350+
text: '{"choices":[{"index":0data: {"id":"abc","choices":[{"index":0}]}}]}',
351+
cause: new SyntaxError("JSON Parse error: Expected '}'"),
352+
})
353+
354+
const error = MessageV2.fromError(parseError, { providerID })
355+
356+
expect(SessionV1.APIError.isInstance(error)).toBe(true)
357+
const retryable = SessionRetry.retryable(error, retryProvider)
358+
expect(retryable).toBeDefined()
359+
expect(retryable?.message).toInclude("malformed JSON stream")
360+
})
361+
347362
test("maps free limits to Go upsell action", () => {
348363
const error = Schema.decodeUnknownSync(SessionV1.APIError.Schema)(
349364
new SessionV1.APIError({

0 commit comments

Comments
 (0)