Skip to content

feat(ai): add onStepFinish to agent.generate and agent.stream#11980

Merged
lgrammel merged 1 commit intomainfrom
lg/RYtTHc7u
Jan 23, 2026
Merged

feat(ai): add onStepFinish to agent.generate and agent.stream#11980
lgrammel merged 1 commit intomainfrom
lg/RYtTHc7u

Conversation

@lgrammel
Copy link
Copy Markdown
Collaborator

Background

For things such as token tracking, it would be helpful if callbacks can be registered on a per-call basis. See #11468

Summary

Add onStepFinish callback support to Agent.generate() and Agent.stream()

Related Issues

Resolves #11468

@ghost ghost added the ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. label Jan 23, 2026
Copy link
Copy Markdown
Contributor

@vercel vercel Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

The onStepFinish callback parameter is accepted in function signatures but not passed to downstream functions, causing callbacks to be silently dropped and never executed.

Fix on Vercel

@lgrammel
Copy link
Copy Markdown
Collaborator Author

Additional Suggestion:

The onStepFinish callback parameter is accepted in function signatures but not passed to downstream functions, causing callbacks to be silently dropped and never executed.

Fix on Vercel

image

@lgrammel lgrammel merged commit ded661b into main Jan 23, 2026
25 checks passed
@lgrammel lgrammel deleted the lg/RYtTHc7u branch January 23, 2026 12:18
B-Step62 pushed a commit to B-Step62/ai that referenced this pull request Jan 28, 2026
…#11980)

## Background

For things such as token tracking, it would be helpful if callbacks can
be registered on a per-call basis. See vercel#11468

## Summary

Add `onStepFinish` callback support to `Agent.generate()` and
`Agent.stream()`

## Related Issues

Resolves vercel#11468
rovo89 added a commit to rovo89/ai that referenced this pull request Feb 25, 2026
This changes the type of the `onStepFinish` callback from
`ToolLoopAgentOnStepFinishCallback<TOOLS>` to
`UIMessageStreamOnStepFinishCallback<UI_MESSAGE>`
for methods which create a `UIMessageChunk` stream for an Agent, giving
the implementation access to the generated`UIMessage` so far instead of
just the raw parts.

This partly reverts vercel#11980, which explicitly added the `onStepFinish`
callback to the method signatures. Instead, `onStepFinish` is now added
to the `UIMessageStreamOptions` type in the same way as `onFinish`
already was, and is passed through via `options`.

Affected methods are:
- `createAgentUIStream()`
- `createAgentUIStreamResponse()`
- `pipeAgentUIStreamToResponse()`

For even more consistency, the `onStepFinish` callback is also added to:
- `DefaultStreamTextResult.toUIMessageStream()` (used by the methods above)
- `DefaultStreamTextResult.toUIMessageStreamResponse()`
- `DefaultStreamTextResult.pipeUIMessageStreamToResponse()`

Callers which want to receive the `ToolLoopAgentOnStepFinishCallback`
can easily do that with the `onStepFinish` callback to the constructor
of the `ToolLoopAgent` that they're passing to the methods above.

Fixes vercel#12841.
rovo89 added a commit to rovo89/ai that referenced this pull request Feb 25, 2026
This changes the type of the `onStepFinish` callback from
`ToolLoopAgentOnStepFinishCallback<TOOLS>` to
`UIMessageStreamOnStepFinishCallback<UI_MESSAGE>`
for methods which create a `UIMessageChunk` stream for an Agent, giving
the implementation access to the generated`UIMessage` so far instead of
just the raw parts.

This partly reverts vercel#11980, which explicitly added the `onStepFinish`
callback to the method signatures. Instead, `onStepFinish` is now added
to the `UIMessageStreamOptions` type in the same way as `onFinish`
already was, and is passed through via `options`.

Affected methods are:
- `createAgentUIStream()`
- `createAgentUIStreamResponse()`
- `pipeAgentUIStreamToResponse()`

For even more consistency, the `onStepFinish` callback is also added to:
- `DefaultStreamTextResult.toUIMessageStream()` (used by the methods above)
- `DefaultStreamTextResult.toUIMessageStreamResponse()`
- `DefaultStreamTextResult.pipeUIMessageStreamToResponse()`

Callers which want to receive the `ToolLoopAgentOnStepFinishCallback`
can easily do that with the `onStepFinish` callback to the constructor
of the `ToolLoopAgent` that they're passing to the methods above.

Fixes vercel#12841.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: add onStepFinish to agent.generate and agent.stream()

2 participants