Description
The onFinish callback is defined in ToolLoopAgentSettings but is not being passed to the underlying streamText()/generateText() calls in ToolLoopAgent methods since version 6.0.135.
Affected Versions
- Working:
ai@6.0.116
- Broken:
ai@6.0.135 - ai@6.0.157 (and possibly later v6 versions)
Root Cause
PR #13698 ("chore(ai): remove all experimental agent events") was intended to remove experimental callbacks (experimental_onStart, experimental_onStepStart, experimental_onToolCallStart, experimental_onToolCallFinish) that were mistakenly merged into v6. However, the PR also incorrectly removed the onFinish callback usage, which is not an experimental callback.
Evidence
In ai@6.0.116 (working):
// packages/ai/src/agent/tool-loop-agent.ts
async stream({
onFinish, // ✅ onFinish parameter exists
...options
}) {
return streamText({
onFinish: this.mergeCallbacks(this.settings.onFinish, onFinish), // ✅ passed to streamText
});
}
In ai@6.0.157 (broken):
// packages/ai/src/agent/tool-loop-agent.ts
async stream({
onStepFinish, // ❌ only onStepFinish, no onFinish parameter
...options
}) {
return streamText({
onStepFinish: this.mergeOnStepFinishCallbacks(onStepFinish),
// ❌ onFinish is NOT passed to streamText
});
}
However, onFinish is still defined in the types:
// packages/ai/src/agent/tool-loop-agent-settings.ts (line 110)
onFinish?: ToolLoopAgentOnFinishCallback<NoInfer<TOOLS>>;
Expected Behavior
The onFinish callback should be:
- Accepted as a parameter in
AgentCallParameters and AgentStreamParameters
- Passed to the underlying
generateText() and streamText() calls
- Merged with the
onFinish from ToolLoopAgentSettings (constructor)
Actual Behavior
onFinish is defined in ToolLoopAgentSettings but never used
- Users cannot pass
onFinish to agent.stream() or agent.generate()
- The callback silently does nothing
Suggested Fix
Restore the onFinish parameter and its usage in ToolLoopAgent:
// In agent.ts - add onFinish to AgentCallParameters
export type AgentCallParameters<CALL_OPTIONS, TOOLS extends ToolSet = {}> = ... & {
onFinish?: ToolLoopAgentOnFinishCallback<TOOLS>;
};
// In tool-loop-agent.ts - restore onFinish handling
async stream({
onFinish,
...options
}) {
return streamText({
onFinish: this.mergeCallbacks(this.settings.onFinish, onFinish),
});
}
Related
AI SDK Version
ai: 6.0.157
Code of Conduct
Description
The
onFinishcallback is defined inToolLoopAgentSettingsbut is not being passed to the underlyingstreamText()/generateText()calls inToolLoopAgentmethods since version 6.0.135.Affected Versions
ai@6.0.116ai@6.0.135-ai@6.0.157(and possibly later v6 versions)Root Cause
PR #13698 ("chore(ai): remove all experimental agent events") was intended to remove experimental callbacks (
experimental_onStart,experimental_onStepStart,experimental_onToolCallStart,experimental_onToolCallFinish) that were mistakenly merged into v6. However, the PR also incorrectly removed theonFinishcallback usage, which is not an experimental callback.Evidence
In
ai@6.0.116(working):In
ai@6.0.157(broken):However, onFinish is still defined in the types:
Expected Behavior
The
onFinishcallback should be:AgentCallParametersandAgentStreamParametersgenerateText()andstreamText()callsonFinishfromToolLoopAgentSettings(constructor)Actual Behavior
onFinishis defined inToolLoopAgentSettingsbut never usedonFinishtoagent.stream()oragent.generate()Suggested Fix
Restore the
onFinishparameter and its usage inToolLoopAgent:Related
AI SDK Version
ai: 6.0.157
Code of Conduct