From e98d01daf65cbad235a244c403d3ca7c3015abd6 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:15:32 -0500 Subject: [PATCH] fix(ai): report the token limit instead of a schema decode error (#60795) --- packages/ai/src/llm.ts | 11 ++++------- packages/ai/test/generate-object.test.ts | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/ai/src/llm.ts b/packages/ai/src/llm.ts index 7ba34141aaf..d664a75367c 100644 --- a/packages/ai/src/llm.ts +++ b/packages/ai/src/llm.ts @@ -238,17 +238,14 @@ const decodeObject = (output: Tool, response: LLMResponse) => { const call = response.toolCalls.find((event) => event.name === OBJECT_TOOL) const invalid = (message: string, cause?: unknown) => new AIError({ reason: new InvalidProviderOutputError({ message, cause }), partial: response }) + // A cut-off call arrives with truncated or empty input, so its decode error would hide the real cause. + if (response.finishReason.normalized === "length") + return Effect.fail(invalid("output hit the token limit before the forced tool call completed")) if (call) return output ._decode(call.input) .pipe(Effect.mapError((error) => invalid(`schema decode failed: ${error.message}`, error))) - return Effect.fail( - invalid( - response.finishReason.normalized === "length" - ? "output hit the token limit before the forced tool call completed" - : `model did not call the forced \`${OBJECT_TOOL}\` tool`, - ), - ) + return Effect.fail(invalid(`model did not call the forced \`${OBJECT_TOOL}\` tool`)) } const withObject = (output: Tool, event: Finish, state: LLMResponse.State): Effect.Effect => { diff --git a/packages/ai/test/generate-object.test.ts b/packages/ai/test/generate-object.test.ts index 43fcd856dfe..87fc93ec009 100644 --- a/packages/ai/test/generate-object.test.ts +++ b/packages/ai/test/generate-object.test.ts @@ -207,6 +207,26 @@ describe("schema on ai.stream and ai.loop", () => { execute: ({ value }) => Effect.succeed(value), }) + scripted.effect("reports the token limit instead of a decode error when output is cut off", () => + Effect.gen(function* () { + const client = yield* TestLLM.Test + yield* client.push( + TestLLM.complete( + { reason: { normalized: "length" } }, + LLMEvent.toolCall({ id: "call-1", name: "generate_object", input: {} }), + ), + ) + + const error = yield* ai + .generate({ model, prompt: "Pick a city.", schema: City, generation: { maxTokens: 1 } }) + .pipe(Effect.flip) + + expect(error.message).toContain("output hit the token limit") + expect(error.message).not.toContain("schema decode failed") + expect(error.partial?.toolCalls[0]?.name).toBe("generate_object") + }), + ) + scripted.effect("stream puts the decoded object on finish", () => Effect.gen(function* () { const client = yield* TestLLM.Test