Skip to content

InvalidEventBytes exception should be caught and handled as recoverable #4530

@S-W-Leong

Description

@S-W-Leong

Bug Description

When using aws.realtime.RealtimeModel (Nova Sonic), the InvalidEventBytes exception from smithy_aws_event_stream is treated as an unrecoverable error, causing the entire AgentSession to terminate. This exception should be handled as recoverable and trigger a session restart, similar to how ModelStreamErrorException and ModelTimeoutException are handled.

Expected Behavior

The InvalidEventBytes exception should be caught and handled as a recoverable error, triggering _restart_session() to attempt recovery, similar to how other stream errors are handled.

Reproduction Steps

Create an agent using aws.realtime.RealtimeModel with Nova Sonic
Start a conversation session
Wait for the InvalidEventBytes error to occur (typically happens during barge-in events or network interruptions)

Operating System

macOS

Models Used

Nova 2 Sonic (AWS)

Package Versions

livekit-agents: 1.3.11
livekit-plugins-aws: 1.3.11
smithy-aws-event-stream: 0.2.1
aws_sdk_bedrock_runtime: 0.3.0
Python: 3.13

Session/Room/Call IDs

No response

Proposed Solution

from smithy_aws_event_stream.exceptions import InvalidEventBytes

# In _process_responses():
except (
    ThrottlingException,
    ModelNotReadyException,
    ModelErrorException,
    ModelStreamErrorException,
    InvalidEventBytes,  # Add this
) as re:
    logger.warning(f"Retryable error: {re}\nAttempting to recover...")
    await self._restart_session(re)
    break

Additional Context

The InvalidEventBytes error typically occurs when:

The user interrupts the model (barge-in)
Network connection is interrupted mid-stream
The bidirectional stream is closed unexpectedly
The log message "Nova Sonic handles interruption automatically via barge-in detection" appears right after the error, suggesting this may be related to barge-in handling where the stream is intentionally interrupted but the exception isn't being handled gracefully.

Workaround (temp)

As a temporary workaround, I've created a monkey-patch that intercepts InvalidEventBytes at the EventReceiver.receive() level and converts it to ModelStreamErrorException:

from smithy_aws_event_stream.exceptions import InvalidEventBytes
from aws_sdk_bedrock_runtime.models import ModelStreamErrorException
from smithy_aws_event_stream.aio import EventReceiver

original_receive = EventReceiver.receive

async def patched_receive(self):
    try:
        return await original_receive(self)
    except InvalidEventBytes as e:
        raise ModelStreamErrorException(
            message=f"Stream error (InvalidEventBytes): {e}",
            fault="server",
            is_retry_safe=True,
        ) from e

EventReceiver.receive = patched_receive

Screenshots and Recordings

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions