Task stream

A task’s answer as it is written, read piece by piece or all at once.

A task’s answer as the model writes it, from run() with stream: true. Read it with for await, or call text() or done() to wait for the whole answer.

interface TaskStream extends AsyncIterable<TaskChunk> {
  readonly requestId: string | undefined
  readonly processedIn: string | undefined
  text(): Promise<string>
  done(): Promise<TaskDone>
}

interface TaskChunk {
  text: string
}

interface TaskDone {
  text: string
  json: unknown
  stopReason: StopReason
  usage: TaskUsage
}

Fields and methods

for awaitTaskChunk

Each piece of the answer as it arrives, in text. A reasoning model’s reasoning is left out.

text()Promise<string>

Reads the rest of the stream and gives the whole answer.

done()Promise<TaskDone>

Reads the rest of the stream and gives the whole answer with json, stopReason, and usage, as in a task reply. After a for await loop, it gives them straight away.

requestIdstring or undefined

The request’s ID, for support.

processedInstring or undefined

The country the model ran in.

A stream can be read once. A second for await fails with stream_consumed; keep the text as you read it, or call done().

When the run fails after the answer started, reading fails with the service’s code, such as model_unavailable. A stream cut off before the end fails with stream_interrupted.

Last updated