Skip to content

Commit 9708923

Browse files
committed
fix(ai): bypass SDK MessageStream to prevent partialParse JSON crash
Switch from client.messages.stream() to client.messages.create() to avoid the Anthropic SDK's unguarded partialParse() call in MessageStream, which crashes with 'Expected , or } after property value in JSON' or 'Bad control character in string literal' when tool input JSON contains control characters or triggers the vendored partial-json tokenizer's consecutive string token bug. Pi already accumulates tool arguments with its own parseStreamingJson() which handles all these cases gracefully. The SDK's MessageStream accumulation was redundant and its partialParse crash killed the entire stream before pi ever saw the event. The raw Stream from .create() yields identical SSE events without the buggy accumulation layer. Upstream: anthropics/anthropic-sdk-typescript#971 (open, unmerged) Related: anthropics/anthropic-sdk-typescript#882 Related: anthropics/anthropic-sdk-python#1265
1 parent c623ada commit 9708923

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

packages/ai/src/providers/anthropic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages", AnthropicOpti
256256
if (nextParams !== undefined) {
257257
params = nextParams as MessageCreateParamsStreaming;
258258
}
259-
const anthropicStream = client.messages.stream({ ...params, stream: true }, { signal: options?.signal });
259+
const anthropicStream = await client.messages.create({ ...params, stream: true }, { signal: options?.signal });
260260
stream.push({ type: "start", partial: output });
261261

262262
type Block = (ThinkingContent | TextContent | (ToolCall & { partialJson: string })) & { index: number };

0 commit comments

Comments
 (0)