stream_from_api: Stream from API

View source: R/utils_http.R

stream_from_apiR Documentation

Stream from API

Description

Makes a streaming POST request and processes Server-Sent Events (SSE) using httr2. Implements robust error recovery for malformed SSE data.

Usage

stream_from_api(
  url,
  headers,
  body,
  callback,
  max_retries = 5,
  initial_delay_ms = 2000,
  backoff_factor = 2,
  timeout_seconds = NULL,
  total_timeout_seconds = NULL,
  first_byte_timeout_seconds = NULL,
  connect_timeout_seconds = NULL,
  idle_timeout_seconds = NULL
)

Arguments

url

The API endpoint URL.

headers

A named list of HTTP headers.

body

The request body (will be converted to JSON).

callback

A function called for each parsed SSE data chunk.

max_retries

Maximum number of connection/first-event retries before any stream chunk has been delivered (default: 5).

initial_delay_ms

Initial delay in milliseconds (default: 2000).

backoff_factor

Multiplier for delay on each retry (default: 2).

timeout_seconds

Legacy alias for total_timeout_seconds.

total_timeout_seconds

Optional total stream timeout in seconds. Defaults to getOption("aisdk.http_total_timeout_seconds"), then AISDK_HTTP_TOTAL_TIMEOUT_SECONDS, then legacy aisdk.http_timeout_seconds / AISDK_HTTP_TIMEOUT_SECONDS. Streams do not apply a default total timeout when none is configured.

first_byte_timeout_seconds

Optional time-to-first-byte timeout in seconds. Defaults to getOption("aisdk.http_first_byte_timeout_seconds"), then AISDK_HTTP_FIRST_BYTE_TIMEOUT_SECONDS, then 300.

connect_timeout_seconds

Optional connection-establishment timeout in seconds. Defaults to getOption("aisdk.http_connect_timeout_seconds"), then AISDK_HTTP_CONNECT_TIMEOUT_SECONDS, then 10.

idle_timeout_seconds

Optional stall timeout in seconds. Streams default to getOption("aisdk.http_idle_timeout_seconds"), then AISDK_HTTP_IDLE_TIMEOUT_SECONDS, then 120. As long as bytes keep arriving, the stream is considered healthy.


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