Registry / ai-ml / fetch-stream-observable

fetch-stream-observable

JSON →
library1.1.2jsnpmunverified

A lightweight library that wraps the Fetch API to return an Observable from the zen-observable package, enabling stream-based responses (e.g., OpenAI chat completions). Current stable version 1.1.2. Actively maintained with TypeScript support and a small bundle (3.7 KB min+gzip). Key differentiators: built-in OpenAI stream parsing, automatic fallback to non-streaming responses, and additive text concatenation. Ideal for real-time data consumption in frontend frameworks like React.

npm install fetch-stream-observable
INSTALL
IMPORT
SIG · FETCH-STREAM-OBSER
F
fetch-stream-observable
ai-mljavascriptv1.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetchObservable
import { fetchObservable } from 'fetch-stream-observable'
const { fetchObservable } = require('fetch-stream-observable')
The package exports only fetchObservable as a named export. CommonJS require works but is discouraged in TypeScript bundlers.
Observable
import { Observable } from 'zen-observable'
import { Observable } from 'fetch-stream-observable'
Observable type is from zen-observable, not re-exported by this package.
FetchObservableOptions
import type { FetchObservableOptions } from 'fetch-stream-observable'
import { FetchObservableOptions } from 'fetch-stream-observable'
Options type is available as a TypeScript type export; use type import to avoid runtime errors.

Demonstrates fetching a streaming response from OpenAI's API using fetchObservable, subscribing to chunks, and unsubscribing after timeout.

import { fetchObservable } from 'fetch-stream-observable'; const apiKey = process.env.OPENAI_API_KEY ?? ''; const subscription = fetchObservable('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${apiKey}`, }, body: JSON.stringify({ model: 'gpt-4', messages: [{ role: 'user', content: 'Hello' }], stream: true, }), }).subscribe((data: string) => { console.log('Received chunk:', data); }); // To unsubscribe after 10 seconds setTimeout(() => subscription.unsubscribe(), 10000);
Debug
Known issues
gotchaThe 'subscribe' callback receives the entire accumulated response string when doNotConcatTextStream is false (default) and streaming is used. This may cause performance issues with large responses.
fix
Set doNotConcatTextStream: true to receive partial chunks instead.
affects: >=1.0.0
gotchaIf the response is not a stream (e.g., JSON), the observable calls subscribe only once with the full response. Do not assume multiple emissions.
fix
Handle non-streaming responses in the same subscribe callback, but only expect one emission.
affects: >=1.0.0
gotchaThe package depends on zen-observable. If you use a different Observable library (e.g., RxJS), you cannot directly chain operators without converting.
fix
Convert to RxJS observable: import { from } from 'rxjs'; const rxObs = from(fetchObservable(...));
affects: >=1.0.0
deprecatedThe option 'doNotParseOpenAIStream' is poorly named and may be confusing. It is not deprecated in terms of removal, but its usage is recommended to be left as false for typical OpenAI usage.
fix
Set doNotParseOpenAIStream: false (default) to enable automatic parsing of OpenAI stream deltas.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'zen-observable'
Missing peerDependency zen-observable when using npm <7 or yarn without auto-install.
fix
Install zen-observable explicitly: npm install zen-observable
TypeError: fetchObservable is not a function
Incorrect import syntax, e.g., using default import instead of named import.
fix
Use named import: import { fetchObservable } from 'fetch-stream-observable'
Property 'subscribe' does not exist on type 'FetchObservable'
Missing zen-observable import or incorrect TypeScript type inference.
fix
Ensure zen-observable is installed and its types are available (usually shipped with package).
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
zen-observablerequiredCore dependency providing the Observable implementation used by the returned fetch observable.
Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
2
Resources
fetch-stream-observable — npm install fetch-stream-observable · libregistry