Skip to content

Commit 186a688

Browse files
committed
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 849c259 commit 186a688

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"
@@ -699,6 +706,18 @@ export function fromError(
699706
},
700707
{ cause: e },
701708
).toObject()
709+
// ai-sdk throws JSONParseError on malformed SSE chunks; classify as retryable
710+
// so the existing backoff path recovers (previously fell through to UnknownError).
711+
// See #13579.
712+
case JSONParseError.isInstance(e):
713+
return new APIError(
714+
{
715+
message: `Provider returned malformed JSON stream: ${e.message.slice(0, 200)}`,
716+
isRetryable: true,
717+
},
718+
{ cause: e },
719+
).toObject()
720+
702721
case e instanceof Error:
703722
return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
704723
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"
@@ -254,6 +254,21 @@ describe("session.retry.retryable", () => {
254254
expect(retryable).toEqual({ message: "Response decompression failed" })
255255
})
256256

257+
test("retries JSONParseError from malformed SSE streams", () => {
258+
// See #13579: malformed SSE chunks should enter retry, not terminate the turn.
259+
const parseError = new JSONParseError({
260+
text: '{"choices":[{"index":0data: {"id":"abc","choices":[{"index":0}]}}]}',
261+
cause: new SyntaxError("JSON Parse error: Expected '}'"),
262+
})
263+
264+
const error = MessageV2.fromError(parseError, { providerID })
265+
266+
expect(SessionV1.APIError.isInstance(error)).toBe(true)
267+
const retryable = SessionRetry.retryable(error, retryProvider)
268+
expect(retryable).toBeDefined()
269+
expect(retryable?.message).toInclude("malformed JSON stream")
270+
})
271+
257272
test("maps free limits to Go upsell action", () => {
258273
const error = Schema.decodeUnknownSync(SessionV1.APIError.Schema)(
259274
new SessionV1.APIError({

0 commit comments

Comments
 (0)