Skip to content

[Bug] Anthropic SSE streaming silently drops malformed JSON frames #307

Description

@404-Page-Found

Description

The Anthropic SSE parser silently ignores malformed JSON for message_start and content_block_delta events. A corrupted/incomplete event can therefore disappear from the stream without any error, leaving the consumer with a truncated response.

This is inconsistent with the OpenAI-compatible SSE parser, which explicitly throws on malformed JSON.

Location

src/providers/sse.ts:154-184parseAnthropicSseLine()

Relevant code

if (state.currentEvent === 'content_block_delta') {
  try {
    const parsed = JSON.parse(payload) as { delta?: { text?: string } };
    if (parsed.delta?.text) {
      return { kind: 'text', text: parsed.delta.text };
    }
  } catch {
    // Skip malformed JSON
  }
  return null;
}

The same silent catch exists for message_start.

By contrast, parseOpenAiSseLine() throws Malformed OpenAI SSE data: invalid JSON.

Steps to reproduce

  1. Run commit-echo suggest --stream with the Anthropic provider.
  2. Make the SSE stream contain a valid content_block_delta event followed by a malformed data: JSON payload and then message_stop.
  3. The malformed event is ignored and the stream continues as if it were absent.
  4. Depending on the remaining content, the command may end with truncated suggestions or a generic parse failure instead of identifying the malformed provider response.

This can be reproduced with a mocked fetch() response in the existing tests/stream-sse.test.mjs setup.

Expected behavior

Malformed provider frames should fail the stream with a clear error and release the response body.

Actual behavior

Malformed Anthropic frames in these event types are swallowed, so the caller cannot distinguish “provider sent bad JSON” from “provider sent no content.”

Suggested fix

Let JSON parse failures propagate with an Anthropic-specific error (matching the OpenAI behavior), and add regression tests for malformed message_start and content_block_delta frames.

Impact

Streaming responses can be silently truncated, producing misleading or unparseable commit suggestions and making provider-side protocol failures difficult to diagnose.

Reviewed against current main at c67ff967a018d5fd4b032f6ef6a4981ac9d76a12.

Activity

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

Metadata

Metadata

Labels

bugSomething isn't workingp2Medium priority; affects normal use

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions