-
Notifications
You must be signed in to change notification settings - Fork 623
feat(js): durable streaming for flows (experimental) #3770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Genkit flows can be slow operations that stream intermediate progress. In scenarios with unreliable network conditions or client-side interruptions (e.g., page reloads), clients can lose their connection to the stream. Without a durability mechanism, the stream is lost, and the client cannot retrieve the in-progress or final results.
This PR introduces the concept of a `StreamManager` which is responsible for persisting the state of a stream. A `StreamManager` can be provided when defining a flow handler.
Three `StreamManager` implementations are included:
* `InMemoryStreamManager`: Stores stream state in memory. This is useful for development and testing but is not truly durable as it will lose state if the process restarts.
* `firestoreStreamManager`: A durable `StreamManager` that uses Firebase Firestore to store stream state.
* `rtdbStreamManager`: A durable `StreamManager` that uses the Firebase Realtime Database to store stream state.
To make a flow's streams durable, pass a `streamManager` instance to the handler. This is supported in both the Express (`expressHandler`) and Next.js (`appRoute`) plugins. The new `durable-streaming` sample app demonstrates this for Express:
```typescript
// From js/testapps/durable-streaming/src/index.ts
const fApp = initializeApp();
export const firestore = firestoreStreamManager({
firebaseApp: fApp,
db: getFirestore(fApp),
collection: 'streamy',
});
// ...
app.post(
'/streamyFirestore',
expressHandler(streamy, { streamManager: firestore })
);
```
The sample app includes a `streamyThrowy` flow that intentionally throws an error mid-stream to demonstrate how a durable stream manager can preserve the stream's state up to the point of failure.
A client can then connect to a durable stream and resume it if necessary:
```typescript
// Start a new stream
const result = streamFlow({
url: `http://localhost:8080/myFlowDurable`,
input: 'tell me a long story',
});
const streamId = await result.streamId; // Save this ID
// ... later, reconnect if needed ...
const reconnectedResult = streamFlow({
url: `http://localhost:8080/myFlowDurable`,
streamId: streamId,
});
```
**Beta Feature:** This is to be released as a beta feature.
ssbushi
reviewed
Dec 3, 2025
ssbushi
approved these changes
Dec 8, 2025
apascal07
reviewed
Dec 8, 2025
apascal07
reviewed
Dec 8, 2025
apascal07
reviewed
Dec 8, 2025
apascal07
approved these changes
Dec 8, 2025
This was referenced Dec 13, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Genkit flows can be slow operations that stream intermediate progress. In scenarios with unreliable network conditions or client-side interruptions (e.g., page reloads), clients can lose their connection to the stream. Without a durability mechanism, the stream is lost, and the client cannot retrieve the in-progress or final results.
This PR introduces the concept of a
StreamManagerwhich is responsible for persisting the state of a stream. AStreamManagercan be provided when defining a flow handler.Three
StreamManagerimplementations are included:InMemoryStreamManager: Stores stream state in memory. This is useful for development and testing but is not truly durable as it will lose state if the process restarts.FirestoreStreamManager: A durableStreamManagerthat uses Firebase Firestore to store stream state.RtdbStreamManager: A durableStreamManagerthat uses the Firebase Realtime Database to store stream state.To make a flow's streams durable, pass a
streamManagerinstance to the handler. This is supported in both the Express (expressHandler) and Next.js (appRoute) plugins. The newdurable-streamingsample app demonstrates this for Express:The sample app includes a
streamyThrowyflow that intentionally throws an error mid-stream to demonstrate how a durable stream manager can preserve the stream's state up to the point of failure.A client can then connect to a durable stream and resume it if necessary:
Beta Feature: This is to be released as a beta feature.
Checklist (if applicable):