|
69 | 69 | MAX_MESSAGES = 40 |
70 | 70 | DEFAULT_MAX_SESSION_RESTART_ATTEMPTS = 3 |
71 | 71 | 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 | +) |
72 | 76 | # Session recycling: restart before 8-min AWS limit or credential expiry |
73 | 77 | # Override with LK_SESSION_MAX_DURATION env var for testing (e.g., "60" for 1 minute) |
74 | 78 | MAX_SESSION_DURATION_SECONDS = int(os.getenv("LK_SESSION_MAX_DURATION", 6 * 60)) |
75 | 79 | CREDENTIAL_EXPIRY_BUFFER_SECONDS = 3 * 60 # Restart 3 min before credential expiry |
76 | 80 | 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 | + |
77 | 88 | DEFAULT_SYSTEM_PROMPT = ( |
78 | 89 | "Your name is Sonic, and you are a friendly and enthusiastic voice assistant. " |
79 | 90 | "You love helping people and having natural conversations. " |
@@ -1528,11 +1539,10 @@ async def _process_responses(self) -> None: |
1528 | 1539 | self._close_current_generation() |
1529 | 1540 | raise |
1530 | 1541 | 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): |
1536 | 1546 | logger.warning(f"Validation error: {ve}\nAttempting to recover...") |
1537 | 1547 | await self._restart_session(ve) |
1538 | 1548 | elif "Tool Response parsing error" in ve.message: |
|
0 commit comments