Skip to content

Commit 74500f3

Browse files
authored
fix(ai): truncated Anthropic streams error; proxies exempt from message_stop contract (#120030)
* fix(ai): enforce message_stop only for direct Anthropic models The Anthropic SSE transport threw 'stream ended before message_stop' only when a refusal buffer happened to be non-empty, so a truncated first-party stream could pass silently while compatible proxy providers that legitimately omit message_stop failed intermittently. The invariant now keys off the transport contract owner: direct Anthropic models always require message_stop; proxy providers are exempt. * test(ai): cover proxy streams ending without message_stop ClawSweeper P1: the proxy exemption had no focused test, so the lenient branch could regress silently. A non-anthropic provider through a custom endpoint now proves stopReason=stop with no error when the stream ends without message_stop.
1 parent aa7cf44 commit 74500f3

2 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎packages/ai/src/transports/anthropic-transport-stream.test.ts‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,20 @@ describe("anthropic transport stream", () => {
352352
};
353353
},
354354
});
355-
guardedFetchMock.mockResolvedValue(createSseResponse());
355+
guardedFetchMock.mockResolvedValue(
356+
createSseResponse([
357+
{
358+
type: "message_start",
359+
message: { id: "msg_default", usage: { input_tokens: 0, output_tokens: 0 } },
360+
},
361+
{
362+
type: "message_delta",
363+
delta: { stop_reason: "end_turn" },
364+
usage: { input_tokens: 0, output_tokens: 0 },
365+
},
366+
{ type: "message_stop" },
367+
]),
368+
);
356369
});
357370

358371
afterEach(() => {
@@ -1618,6 +1631,75 @@ describe("anthropic transport stream", () => {
16181631
expect(result.errorMessage).toBe("Anthropic stream ended before message_stop");
16191632
});
16201633

1634+
it("rejects ordinary Anthropic output when the stream ends before message_stop", async () => {
1635+
guardedFetchMock.mockResolvedValueOnce(
1636+
createSseResponse([
1637+
{
1638+
type: "message_start",
1639+
message: { id: "msg_partial", usage: { input_tokens: 3, output_tokens: 0 } },
1640+
},
1641+
{ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
1642+
{
1643+
type: "content_block_delta",
1644+
index: 0,
1645+
delta: { type: "text_delta", text: "truncated answer" },
1646+
},
1647+
{ type: "content_block_stop", index: 0 },
1648+
{
1649+
type: "message_delta",
1650+
delta: { stop_reason: "end_turn" },
1651+
usage: { input_tokens: 3, output_tokens: 2 },
1652+
},
1653+
]),
1654+
);
1655+
1656+
const result = await runTransportStream(
1657+
makeAnthropicTransportModel(),
1658+
{ messages: [{ role: "user", content: "hello" }] } as AnthropicStreamContext,
1659+
{ apiKey: "sk-ant-api" } as AnthropicStreamOptions,
1660+
);
1661+
1662+
expect(result.stopReason).toBe("error");
1663+
expect(result.errorMessage).toBe("Anthropic stream ended before message_stop");
1664+
});
1665+
1666+
it("accepts proxy provider streams that end without message_stop", async () => {
1667+
guardedFetchMock.mockResolvedValueOnce(
1668+
createSseResponse([
1669+
{
1670+
type: "message_start",
1671+
message: { id: "msg_proxy", usage: { input_tokens: 3, output_tokens: 0 } },
1672+
},
1673+
{ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
1674+
{
1675+
type: "content_block_delta",
1676+
index: 0,
1677+
delta: { type: "text_delta", text: "proxy answer" },
1678+
},
1679+
{ type: "content_block_stop", index: 0 },
1680+
{
1681+
type: "message_delta",
1682+
delta: { stop_reason: "end_turn" },
1683+
usage: { input_tokens: 3, output_tokens: 2 },
1684+
},
1685+
]),
1686+
);
1687+
1688+
// Proxy identity: non-anthropic provider through a custom endpoint is not
1689+
// held to the first-party message_stop framing contract.
1690+
const result = await runTransportStream(
1691+
makeAnthropicTransportModel({
1692+
provider: "openrouter",
1693+
baseUrl: "https://proxy.example.com/v1",
1694+
}),
1695+
{ messages: [{ role: "user", content: "hello" }] } as AnthropicStreamContext,
1696+
{ apiKey: "sk-proxy" } as AnthropicStreamOptions,
1697+
);
1698+
1699+
expect(result.stopReason).toBe("stop");
1700+
expect(result.errorMessage).toBeUndefined();
1701+
});
1702+
16211703
it("defers a pre-tool text block's text_end until it carries the commentary phase", async () => {
16221704
guardedFetchMock.mockResolvedValueOnce(
16231705
createSseResponse([
@@ -1817,6 +1899,7 @@ describe("anthropic transport stream", () => {
18171899
delta: { stop_reason: "tool_use" },
18181900
usage: { input_tokens: 10, output_tokens: 5 },
18191901
},
1902+
{ type: "message_stop" },
18201903
]),
18211904
);
18221905
const model = makeAnthropicTransportModel({
@@ -2171,7 +2254,7 @@ describe("anthropic transport stream", () => {
21712254
{
21722255
label: "the stream ends before content_block_stop",
21732256
response: () => createSseResponse(createInterruptedThinkingEvents()),
2174-
stopReason: "stop",
2257+
stopReason: "error",
21752258
},
21762259
{
21772260
label: "the provider errors before content_block_stop",

‎packages/ai/src/transports/anthropic-transport-stream.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,9 @@ export function createAnthropicMessagesTransportStreamFn(): StreamFn {
16861686
flushPendingTextEnds();
16871687
}
16881688
}
1689-
if (refusalBuffer && !sawMessageStop) {
1689+
// Anthropic completes every SSE response with message_stop. Compatible
1690+
// proxy providers are not held to that first-party transport contract.
1691+
if (isDirectAnthropicModel(model) && !sawMessageStop) {
16901692
throw new Error("Anthropic stream ended before message_stop");
16911693
}
16921694
if (transportOptions.signal?.aborted) {

0 commit comments

Comments
 (0)