fix(ai): report the token limit instead of a schema decode error (#60795)

This commit is contained in:
Aiden Cline
2026-10-31 17:15:32 -05:00
committed by GitHub
Unverified
parent 66ee1c3ffc
commit e98d01daf6
2 changed files with 24 additions and 7 deletions
+4 -7
View File
@@ -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<LLMEvent, AIError> => {
+20
View File
@@ -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