Skip to content

Commit 852bcc7

Browse files
nightcitybladenightcityblade
andauthored
fix(aws): recover Nova Sonic system instability errors (#6042)
Co-authored-by: nightcityblade <nightcityblade@gmail.com>
1 parent 5bdc07c commit 852bcc7

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realtime/realtime_model.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,22 @@
6969
MAX_MESSAGES = 40
7070
DEFAULT_MAX_SESSION_RESTART_ATTEMPTS = 3
7171
DEFAULT_MAX_SESSION_RESTART_DELAY = 10
72+
RECOVERABLE_VALIDATION_ERROR_MESSAGES = (
73+
"InternalErrorCode=531::RST_STREAM closed stream. HTTP/2 error code: NO_ERROR",
74+
"System instability detected. Please retry your request.",
75+
)
7276
# Session recycling: restart before 8-min AWS limit or credential expiry
7377
# Override with LK_SESSION_MAX_DURATION env var for testing (e.g., "60" for 1 minute)
7478
MAX_SESSION_DURATION_SECONDS = int(os.getenv("LK_SESSION_MAX_DURATION", 6 * 60))
7579
CREDENTIAL_EXPIRY_BUFFER_SECONDS = 3 * 60 # Restart 3 min before credential expiry
7680
BARGE_IN_SIGNAL = '{ "interrupted" : true }' # Nova Sonic's barge-in detection signal
81+
82+
83+
def _is_recoverable_validation_error(exc: object) -> bool:
84+
message = getattr(exc, "message", str(exc))
85+
return any(text in message for text in RECOVERABLE_VALIDATION_ERROR_MESSAGES)
86+
87+
7788
DEFAULT_SYSTEM_PROMPT = (
7889
"Your name is Sonic, and you are a friendly and enthusiastic voice assistant. "
7990
"You love helping people and having natural conversations. "
@@ -1528,11 +1539,10 @@ async def _process_responses(self) -> None:
15281539
self._close_current_generation()
15291540
raise
15301541
except ValidationException as ve:
1531-
# there is a 3min no-activity (e.g. silence) timeout on the stream, after which the stream is closed # noqa: E501
1532-
if (
1533-
"InternalErrorCode=531::RST_STREAM closed stream. HTTP/2 error code: NO_ERROR" # noqa: E501
1534-
in ve.message
1535-
):
1542+
# Some Bedrock ValidationException messages represent transient stream
1543+
# failures. Recover by restarting the Sonic session instead of tearing
1544+
# down the LiveKit session.
1545+
if _is_recoverable_validation_error(ve):
15361546
logger.warning(f"Validation error: {ve}\nAttempting to recover...")
15371547
await self._restart_session(ve)
15381548
elif "Tool Response parsing error" in ve.message:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from types import SimpleNamespace
2+
3+
from livekit.plugins.aws.experimental.realtime.realtime_model import (
4+
_is_recoverable_validation_error,
5+
)
6+
7+
8+
def test_system_instability_validation_error_is_recoverable() -> None:
9+
exc = SimpleNamespace(message="System instability detected. Please retry your request.")
10+
11+
assert _is_recoverable_validation_error(exc) is True
12+
13+
14+
def test_unrecognized_validation_error_is_not_recoverable() -> None:
15+
exc = SimpleNamespace(message="The provided request is invalid.")
16+
17+
assert _is_recoverable_validation_error(exc) is False

0 commit comments

Comments
 (0)