ai icon indicating copy to clipboard operation
ai copied to clipboard

WorkflowAgent: onStepEnd is not called in stream mode for a step that pauses on a tool without execute

Open robertyates opened this issue 3 weeks ago • 4 comments

Description

WorkflowAgent.stream() does not call onStepEnd for a step that pauses the agent because one of its tool calls has no execute (a client-side tool). The telemetry onStepEnd is skipped for that step too.

WorkflowAgent.generate() does call onStepEnd for the same step, and stream() calls it for every step that does not pause. So in stream mode the step that pauses is the only step whose onStepEnd never runs, even though the step is returned in result.steps.

This matters when onStepEnd is used to persist each step's work as it completes: the paused step's content, including results of tools that did execute in that step, never reaches the callback.

Expected: onStepEnd runs once for every step in result.steps, in both modes.

Where: packages/workflow/src/workflow-agent.ts, the branch that returns when tool calls are left for the client:

https://github.com/vercel/ai/blob/6141e6fefbc557f049618477b8fe263dd504605f/packages/workflow/src/workflow-agent.ts#L2977-L2984

if (mode === 'generate' && step != null) {
  await mergedOnStepEnd?.(step as StepResult<TTools, TRuntimeContext>);
  await telemetryDispatcher.onStepEnd?.(normalizeStepForTelemetry(step));
}

Possible fix: drop the mode check on that branch (if (step != null)). With that change applied locally, the callback receives the paused step with the executed tool's result in step.response.messages, the same shape the step loop gives for other steps.

Reproduction

Gist: https://gist.github.com/robertyates/310523706ab8b866790b6272b79268aa

npm install
node repro.mjs

One model call returns two tool calls in the same step: lookUp (has execute) and askUser (no execute). The script runs it once with generate() and once with stream() and counts onStepEnd calls.

Output:

generate: steps returned = 1, onStepEnd calls = 1
stream: steps returned = 1, onStepEnd calls = 0
MISMATCH: onStepEnd ran for the paused step in generate mode but not in stream mode

As a control, a stream-mode run whose tools all have execute (one tool step, then a text step) reports steps returned = 2, onStepEnd calls = 2.

AI SDK Version

  • @ai-sdk/workflow: 2.0.100 (also seen on 2.0.94)
  • ai: 7.0.168
  • Node: v26.8.2

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

robertyates avatar Nov 10 '26 20:11 robertyates

Identify issue type

Identified as Bug
Confidence: High

Rationale

The report demonstrates a concrete callback failure in existing stream behavior, with a reproduction and a specific code path responsible.

Evidence

  • WorkflowAgent.stream() returns a paused step in result.steps but never invokes its onStepEnd callback or telemetry callback.
  • The reproduction shows generate() returns one step and calls onStepEnd once, while stream() returns one step and calls it zero times.
  • The identified branch explicitly restricts callback invocation to generate mode.

ai-sdk-factory[bot] avatar Nov 10 '26 20:11 ai-sdk-factory[bot]

Bug discovery clarification

@robertyates, how did you discover this bug? Did you encounter it while using the AI SDK in production, find it while auditing or reviewing AI SDK source code, or discover it another way? Please describe the original discovery context.

The report establishes the bug but not its original discovery source. It provides a reproduction and source-code analysis without establishing production use or that code inspection led to discovery. Ask the original author how they first encountered it.

ai-sdk-factory[bot] avatar Nov 10 '26 20:11 ai-sdk-factory[bot]

Found while building on the SDK: our app persists each step in onStepEnd, and a step that paused on a client-side tool never got written.

robertyates avatar Nov 13 '26 18:11 robertyates

Bug discovery clarification

@robertyates, how did you discover this bug? Did you encounter it while using the AI SDK in production, find it while auditing or reviewing AI SDK source code, or discover it another way? Please describe the original discovery context.

The author encountered the bug while building an application, but did not establish that it was in production. The source inspection appears to be subsequent investigation, not the discovery source. Ask whether the affected app was in production when the missing step was first observed.

ai-sdk-factory[bot] avatar Nov 13 '26 18:11 ai-sdk-factory[bot]