Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-anthropic-sse-flush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@flint/adapter-anthropic": patch
---

Flush the trailing SSE buffer when the stream closes. The Anthropic adapter's `parseSSE` only emitted events split on `\n\n`, so a final event (such as `message_stop`) that arrives without a trailing blank line was left in the buffer and silently dropped — preventing the terminal `usage`/`end` chunks from being yielded. This mirrors the flush already performed by the OpenAI-compat adapter.
17 changes: 17 additions & 0 deletions packages/adapter-anthropic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,23 @@ async function* parseSSE(
}
}
}

// Flush any remaining buffer content after the stream closes. A final event
// (e.g. message_stop) may not be terminated by a trailing "\n\n".
if (buffer.trim()) {
let event = '';
let data = '';
for (const line of buffer.split('\n')) {
if (line.startsWith('event: ')) {
event = line.slice(7).trim();
} else if (line.startsWith('data: ')) {
data = line.slice(6);
}
}
if (event && data) {
yield { event, data };
}
}
} finally {
reader.releaseLock();
}
Expand Down
36 changes: 36 additions & 0 deletions packages/adapter-anthropic/test/surface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,42 @@ describe('anthropicAdapter — stream()', () => {
expect(endChunk?.reason).toBe('end');
});

it('emits the final event when the stream is not terminated by a blank line', async () => {
// Drop the trailing "\n\n" so message_stop sits in the buffer at stream close.
const sse = sseBody(
{
event: 'message_start',
data: { message: { usage: { input_tokens: 15, output_tokens: 0 } } },
},
{ event: 'content_block_start', data: { index: 0, content_block: { type: 'text' } } },
{
event: 'content_block_delta',
data: { index: 0, delta: { type: 'text_delta', text: 'Hi' } },
},
{ event: 'content_block_stop', data: { index: 0 } },
{
event: 'message_delta',
data: { delta: { stop_reason: 'end_turn' }, usage: { output_tokens: 3 } },
},
{ event: 'message_stop', data: {} },
).replace(/\n\n$/, '');

const a = anthropicAdapter({ apiKey: 'test', fetch: mockFetch(sse) });
const chunks: StreamChunk[] = [];
for await (const chunk of a.stream({
model: 'claude-3-5-haiku-20241022',
messages: [{ role: 'user', content: 'Hi' }],
})) {
chunks.push(chunk);
}

const endChunk = chunks.find((c) => c.type === 'end') as
| { type: 'end'; reason: string }
| undefined;
expect(endChunk).toBeDefined();
expect(endChunk?.reason).toBe('end');
});

it('throws AdapterError on HTTP error response (stream)', async () => {
const a = anthropicAdapter({
apiKey: 'test',
Expand Down
Loading