Function streamText

  • Stateless streaming text generation with optional multi-step tool calling.

    Returns a StreamTextResult immediately; the underlying provider call begins when a consumer starts iterating textStream or fullStream, and text, usage, toolCalls and the other promises settle once that iteration has run the stream to its end or the consumer stopped reading. Awaiting a promise alone starts no request and never settles it: a caller that wants only the assembled text calls generateText, or reads the stream to its end first.

    Parameters

    Returns StreamTextResult

    A StreamTextResult with async iterables and awaitable promises.

    Example

    const { textStream } = streamText({ provider: 'openai', model: 'gpt-4o', prompt: 'Tell me a joke.' });
    for await (const chunk of textStream) {
    process.stdout.write(chunk);
    }