Skip to content

Add onError, onChunk, and onAbort support to ToolLoopAgent.stream #15864

@akkadaska

Description

@akkadaska

Description

streamText accepts the streaming lifecycle callbacks onError, onChunk, and onAbort. However, ToolLoopAgent.stream() does not expose or forward these callbacks.

This is particularly problematic for onError, because streamText defaults to calling console.error(error) when no callback is provided. Applications using ToolLoopAgent cannot replace this behavior with their own structured logger. Depending on the log collection provider, the resulting output can be noisy or difficult to search and correlate.

For example, the following works with streamText:

const result = streamText({
  model,
  prompt: 'Hello',
  onError: ({ error }) => {
    logger.error({ error }, 'AI stream failed');
  },
  onChunk: ({ chunk }) => {
    logger.debug({ chunk }, 'AI stream chunk');
  },
  onAbort: ({ steps }) => {
    logger.info({ steps }, 'AI stream aborted');
  },
});

The equivalent configuration is not accepted by ToolLoopAgent:

const agent = new ToolLoopAgent({
  model,
});

const result = await agent.stream({
  prompt: 'Hello',
  // These options are not supported:
  onError: ({ error }) => {
    logger.error({ error }, 'AI stream failed');
  },
  onChunk: ({ chunk }) => {
    logger.debug({ chunk }, 'AI stream chunk');
  },
  onAbort: ({ steps }) => {
    logger.info({ steps }, 'AI stream aborted');
  },
});

AgentStreamParameters only adds experimental_transform to the common agent call parameters. ToolLoopAgent.stream() also does not extract or forward onError, onChunk, or onAbort to its internal streamText call.

Telemetry integrations can observe errors and aborts, and experimental_transform can observe chunks to some extent. However, a telemetry onError integration does not replace streamText's default console.error handler, so it does not solve the logging issue.

It would be useful for ToolLoopAgent.stream() to support these callbacks:

  • Add onError, onChunk, and onAbort to AgentStreamParameters.
  • Allow them to be configured in ToolLoopAgentSettings, consistent with the other lifecycle callbacks.
  • Merge callbacks configured on the agent and on individual stream() calls.
  • Forward the resulting callbacks to the internal streamText call.

AI SDK Version

6.0.197

This is also reproducible on 7.0.0-canary.165.

Code of Conduct

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions