Skip to content

Implement reconnection logic#24

Closed
mkmeral wants to merge 20 commits into
mehtarac:mainfrom
mkmeral:bidi-reconnection
Closed

Implement reconnection logic#24
mkmeral wants to merge 20 commits into
mehtarac:mainfrom
mkmeral:bidi-reconnection

Conversation

@mkmeral
Copy link
Copy Markdown

@mkmeral mkmeral commented Nov 10, 2025

This PR branches off of event types PR, therefore there are soo many changes right now. It will be manageable once other PR gets merged: #20


Pending integ testing

Description

This PR adds automatic reconnection support to bidirectional streaming sessions, allowing agents to recover from transient connection failures without manual intervention.

Key Changes

Reconnection Feature:

  • Added enable_reconnection (default: True) and max_reconnection_attempts (default: 3) parameters to BidirectionalAgent
  • Implemented automatic reconnection logic that detects connection errors and attempts to reconnect using the agent's existing state (messages, system_prompt, tools)
  • Reconnection preserves conversation history and resumes event processing seamlessly

Error Handling:

  • Added _is_reconnectable_error() to identify connection-related errors (ConnectionError, ConnectionResetError, BrokenPipeError)
  • Added _handle_connection_error() to manage reconnection attempts with proper error propagation
  • Added _reconnect_session() to handle the reconnection process with configurable retry attempts

Code Quality Improvements:

  • Refactored _process_model_stream() by extracting _handle_model_event() for better separation of concerns
  • Simplified audio queue clearing logic in _handle_interruption()
  • Added logging for invalid events (no more silent skipping)
  • Changed tool execution logging from print() to logger.info()
  • Fixed typo: "conycurrent" → "concurrent"

Type System Updates:

  • Added turn_id parameter to InterruptionEvent (was missing, providers were passing None)
  • Removed unused InputEvent union type in favor of explicit type unions
  • Cleaned up Nova Sonic event handling (removed unused completion tracking)

Behavior

When a connection error occurs during streaming:

  1. The error is caught in _process_model_stream()
  2. If the error is reconnectable and reconnection is enabled, the system:
    • Closes the old connection
    • Attempts to reconnect up to max_reconnection_attempts times with 1-second pauses
    • Reconnects using the agent's existing state (preserves conversation history)
    • Resumes event processing if successful
  3. If reconnection fails or is disabled, the error is propagated and the session ends

Example Usage

# Default behavior - reconnection enabled with 3 attempts
agent = BidirectionalAgent(
    model=model,
    system_prompt="You are a helpful assistant"
)

# Custom configuration
agent = BidirectionalAgent(
    model=model,
    system_prompt="You are a helpful assistant",
    enable_reconnection=True,
    max_reconnection_attempts=5
)

# Disable reconnection
agent = BidirectionalAgent(
    model=model,
    system_prompt="You are a helpful assistant",
    enable_reconnection=False
)

Murat Kaan Meral added 20 commits October 31, 2025 11:37
- Resolved class naming conflicts (kept new names: GeminiLiveModel, NovaSonicModel, OpenAIRealtimeModel)
- Resolved Protocol vs ABC conflict (kept Protocol implementation)
- Resolved event type conflicts (kept OutputEvent from bidi-event-types branch)
- All tests passing
Resolved conflicts in novasonic.py and test_novasonic.py by keeping the current
SessionStartEvent/SessionEndEvent naming convention (with session_id) instead of
the older BidirectionalConnectionStart/End format (with connectionId).

This aligns with the naming used in gemini_live.py and openai.py implementations.
@mkmeral mkmeral marked this pull request as draft November 10, 2025 12:52
@mkmeral mkmeral closed this Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant