-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Chunk level timeout on streamText
#5443
Comments
streamText
streamText
Can you describe how the chunk timeout should behave? When should it reset, when should it time out? |
Thank you for reply. As we know, Server-Sent Events of OpenAI compatible api will return data streaming:
I hope to terminate the request when the waiting time between chunks exceeds a certain threshold.
Maybe we can set a timer for the chunk, and when the chunk is received, reset the timer; when timeout, terminates the request. |
My solution: const chunkTimeoutAbortController = new AbortController();
let inactivityTimerId: NodeJS.Timeout | null = null;
const resetInactivityTimer = () => {
if (inactivityTimerId) {
clearTimeout(inactivityTimerId);
}
inactivityTimerId = setTimeout(() => {
chunkTimeoutAbortController.abort();
}, CHUNK_TIMEOUT_IN_MS);
};
const result = streamText({
onChunk: () => {
resetInactivityTimer();
},
onError: ({ error }) => {
if (inactivityTimerId) {
clearTimeout(inactivityTimerId);
}
console.error(LogFormat.error('StreamText error:' + error));
},
abortSignal: chunkTimeoutAbortController.signal,
onFinish: async ({ response }) => {
if (inactivityTimerId) {
clearTimeout(inactivityTimerId);
}
},
})
resetInactivityTimer(); |
I'll consider this when we rework timeouts and fallback providers |
Feature Description
Although
streamText
provides abortSignal option, this is for the entire http request. Can we implement a chuck level timeout (which is obviously more practical)?I guess this can be achieved with onChunk, but it would be better if the ai sdk had this feature built in
Use Cases
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: