Skip to content

agent.stream() should provide access to structured response #26

Description

@dremnik

Problem

When using agent.stream() with structured output, there's no way to get the final parsed response from the stream.

Options

Option 1: Yield as final event

for await (const event of agent.stream(prompt, options)) {
  if (event.kind === "tool-call") console.log(event.toolId);
  if (event.kind === "text-delta") process.stdout.write(event.text);
  if (event.kind === "response") {
    const result = event.response;
    // { needs_updating: boolean, reason: string }
  }
}

Option 2: Property on stream object

const stream = agent.stream(prompt, options);

for await (const event of stream) {
  if (event.kind === "tool-call") console.log(event.toolId);
  if (event.kind === "text-delta") process.stdout.write(event.text);
}

const result = await stream.response;
// { needs_updating: boolean, reason: string }

Type signature:

interface AgentStreamResult<TResult> extends AsyncIterable<ThreadStreamEvent> {
  response: Promise<TResult>;
}

Recommendation

Option 2 - the response isn't really a stream "event", it's the final output. Keeping it separate makes types cleaner.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions