@effect/ai-anthropic: invalid streamed tool input JSON dies with a SyntaxError instead of failing with an AiError
What version of Effect is running?
4.0.0-rc.118
What steps can reproduce the bug?
Stream a response whose tool_use input, put together from its input_json_delta fragments, isn't valid JSON. The reproduction below serves a fixed SSE body through a stub HttpClient:
import { AnthropicClient, AnthropicLanguageModel } from "@effect/ai-anthropic"
import { Cause, Effect, Exit, Layer, Schema, Stream } from "effect"
import { LanguageModel, Tool, Toolkit } from "effect/ai"
import { HttpClient, HttpClientResponse } from "effect/http"
// The tool input as the model streamed it: a string left unterminated.
const partialJson = process.argv[2] ?? `{"data": {"explanation": "two real bugs: the \\"}`
const usage = { input_tokens: 10, output_tokens: 20, cache_creation_input_tokens: 0, cache_read_input_tokens: 0, server_tool_use: null }
const events = [
{ type: "message_start", message: { id: "msg_1", type: "message", role: "assistant", model: "claude-test", content: [], stop_reason: null, stop_sequence: null, usage: { ...usage, output_tokens: 0, cache_creation: null, service_tier: null } } },
{ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_1", name: "report", input: {} } },
{ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: partialJson } },
{ type: "content_block_stop", index: 0 },
{ type: "message_delta", delta: { stop_reason: "tool_use", stop_sequence: null }, usage },
{ type: "message_stop" }
]
const body = events.map((e) => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join("")
const Http = Layer.succeed(HttpClient.HttpClient, HttpClient.make((request) =>
Effect.succeed(HttpClientResponse.fromWeb(request, new Response(body, { headers: { "content-type": "text/event-stream" } })))))
const toolkit = Toolkit.make(Tool.make("report", { parameters: Schema.Struct({ data: Schema.Unknown }) }))
const exit = await LanguageModel.streamText({ prompt: "hi", toolkit, disableToolCallResolution: true }).pipe(
Stream.runCollect,
Effect.provide(AnthropicLanguageModel.layer({ model: "claude-test" })),
Effect.provide(AnthropicClient.layer({})),
Effect.provide(Http),
Effect.runPromiseExit
)
console.log(Exit.isSuccess(exit) ? "success" : Cause.hasDies(exit.cause) ? `DIE: ${Cause.squash(exit.cause)}` : `Fail: ${Cause.squash(exit.cause)}`)What is the expected behavior?
The stream fails with an AiError, the same way OpenAiLanguageModel handles unparseable tool arguments (ToolParameterValidationError, "Failed securely JSON parse tool parameters: …"). The caller can then treat it as a malformed tool call, for example by returning an error tool_result so the model retries, as Anthropic's docs recommend.
What do you see instead?
DIE: SyntaxError: JSON Parse error: Unterminated stringThe same script with a valid input ('{"data": {"explanation": "ok"}}') prints success.
In makeStreamResponse, the content_block_stop case for a tool-call block parses the accumulated input without a guard:
const params = contentBlock.providerExecuted === true
? Tool.unsafeSecureJsonParse(finalParams)
: yield* transformToolCallParams(
options.tools,
contentBlock.name,
Tool.unsafeSecureJsonParse(finalParams)
)The throw inside the generator becomes a defect, so it gets past every catch/catchTag on the AiError channel. The code_execution branch a few lines above already wraps the same call in try/catch.
Additional information
The input can be invalid JSON when fine-grained tool streaming is on (eager_input_streaming: true on a tool, or the legacy fine-grained-tool-streaming-2025-05-14 beta header). The API then skips validating the input, and Anthropic's docs say to "guard the parse". They also note that a response ending in max_tokens can cut a parameter off partway.
Source: Effect-TS/effect