stream_text: Stream Text

View source: R/core_api.R

stream_textR Documentation

Stream Text

Description

Generate text using a language model with streaming output. This function provides a real-time stream of tokens through a callback.

Usage

stream_text(
  model = NULL,
  prompt,
  callback = NULL,
  system = NULL,
  temperature = 0.7,
  max_tokens = NULL,
  tools = NULL,
  max_steps = 1,
  max_tool_result_errors = 2,
  require_post_tool_protocol = FALSE,
  sandbox = FALSE,
  skills = NULL,
  session = NULL,
  hooks = NULL,
  registry = NULL,
  renderer = NULL,
  .stream_event_callback = NULL,
  ...
)

Arguments

model

Either a LanguageModelV1 object, or a string ID like "openai:gpt-4o".

prompt

A character string prompt, or a list of messages.

callback

A function called for each text chunk: callback(text, done).

system

Optional system prompt.

temperature

Sampling temperature (0-2). Default 0.7.

max_tokens

Maximum tokens to generate.

tools

Optional list of Tool objects for function calling.

max_steps

Number of model/tool steps in one execution window. Default 1. The runtime treats this as a budget checkpoint, not as a hard task stop.

max_tool_result_errors

Historical compatibility option. Tool result errors are recorded as task observations; runtime policy decides whether to continue, finalize, ask the user, or block.

require_post_tool_protocol

Logical. If TRUE, after any tool results are returned the model must either make another tool call or wrap its final answer in a ⁠<final_answer>...</final_answer>⁠ block. This is enabled automatically for text-based tool fallback.

sandbox

Logical. If TRUE, enables R-native programmatic sandbox mode. See generate_text for details. Default FALSE.

skills

Optional path to skills directory, or a SkillRegistry object.

session

Optional ChatSession object for shared state.

hooks

Optional HookHandler object.

registry

Optional ProviderRegistry to use.

renderer

Optional Renderer for agent output. Defaults to the cli terminal backend (create_stream_renderer()); pass any Renderer-conforming object (e.g. from a web UI, create_capture_renderer(), or create_null_renderer()) to render agent output elsewhere.

.stream_event_callback

Internal callback for typed stream events.

...

Additional arguments passed to the model.

Value

A GenerateResult object (accumulated from the stream).

Examples


if (interactive()) {
  model <- create_openai()$language_model("gpt-4o")
  stream_text(model, "Tell me a story", callback = function(text, done) {
    if (!done) cat(text)
  })
}


aisdk documentation built on May 29, 2026, 9:07 a.m.