@@ -5,7 +5,7 @@ import type { AudioFrame } from '@livekit/rtc-node';
55import type { TypedEventEmitter as TypedEmitter } from '@livekit/typed-emitter' ;
66import { EventEmitter } from 'node:events' ;
77import type { ReadableStream } from 'node:stream/web' ;
8- import { APIConnectionError , APIStatusError } from '../_exceptions.js' ;
8+ import { APIConnectionError , APIError } from '../_exceptions.js' ;
99import { log } from '../log.js' ;
1010import type { TTSMetrics } from '../metrics/base.js' ;
1111import { DeferredReadableStream } from '../stream/deferred_stream.js' ;
@@ -161,7 +161,7 @@ export abstract class SynthesizeStream
161161 try {
162162 return await this . run ( ) ;
163163 } catch ( error ) {
164- if ( error instanceof APIStatusError ) {
164+ if ( error instanceof APIError ) {
165165 const retryInterval = this . _connOptions . _intervalForRetry ( i ) ;
166166
167167 if ( this . _connOptions . maxRetry === 0 || ! error . retryable ) {
@@ -174,7 +174,8 @@ export abstract class SynthesizeStream
174174 options : { retryable : false } ,
175175 } ) ;
176176 } else {
177- this . emitError ( { error, recoverable : true } ) ;
177+ // Don't emit error event for recoverable errors during retry loop
178+ // to avoid ERR_UNHANDLED_ERROR or premature session termination
178179 this . logger . warn (
179180 { tts : this . #tts. label , attempt : i + 1 , error } ,
180181 `failed to synthesize speech, retrying in ${ retryInterval } s` ,
@@ -388,7 +389,7 @@ export abstract class ChunkedStream implements AsyncIterableIterator<Synthesized
388389 try {
389390 return await this . run ( ) ;
390391 } catch ( error ) {
391- if ( error instanceof APIStatusError ) {
392+ if ( error instanceof APIError ) {
392393 const retryInterval = this . _connOptions . _intervalForRetry ( i ) ;
393394
394395 if ( this . _connOptions . maxRetry === 0 || ! error . retryable ) {
@@ -401,7 +402,8 @@ export abstract class ChunkedStream implements AsyncIterableIterator<Synthesized
401402 options : { retryable : false } ,
402403 } ) ;
403404 } else {
404- this . emitError ( { error, recoverable : true } ) ;
405+ // Don't emit error event for recoverable errors during retry loop
406+ // to avoid ERR_UNHANDLED_ERROR or premature session termination
405407 this . logger . warn (
406408 { tts : this . #tts. label , attempt : i + 1 , error } ,
407409 `failed to generate TTS completion, retrying in ${ retryInterval } s` ,
0 commit comments