Skip to content

Function: doStreamFetch()

@providerprotocol/ai


@providerprotocol/ai / http / doStreamFetch

doStreamFetch(url, init, config, provider, modality): Promise<Response>

Defined in: src/http/fetch.ts:314

Executes an HTTP fetch request for streaming responses.

Unlike doFetch, this function returns the response immediately without checking the HTTP status. This is necessary for Server-Sent Events (SSE) and other streaming protocols where error information may be embedded in the stream.

The caller is responsible for:

  • Checking response.ok and handling HTTP errors
  • Parsing the response stream (e.g., using parseSSEStream)
  • Handling stream-specific error conditions

Retries are not performed for streaming requests since partial data may have already been consumed by the caller.

string

The URL to fetch

RequestInit

Standard fetch RequestInit options

ProviderConfig

Provider configuration containing fetch customization and timeout

string

Provider identifier for error context

Modality

Request modality for error context

Promise<Response>

The Response object (may have non-2xx status)

NETWORK_ERROR - When a network failure occurs

TIMEOUT - When the request times out

CANCELLED - When the request is aborted via signal

const response = await doStreamFetch(
'https://api.openai.com/v1/chat/completions',
{
method: 'POST',
headers: { 'Authorization': 'Bearer sk-...' },
body: JSON.stringify({ model: 'gpt-4', messages: [], stream: true })
},
{ timeout: 120000 },
'openai',
'llm'
);
if (!response.ok) {
throw await normalizeHttpError(response, 'openai', 'llm');
}
for await (const event of parseSSEStream(response.body!)) {
console.log(event);
}