Skip to content

feat: Make websockets an optional dependency for live module #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions google/genai/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,24 @@

import google.auth
import pydantic
from websockets import ConnectionClosed

# MODIFIED: Wrap all websockets imports in a single try-except ImportError block
try:
from websockets import ConnectionClosed

# Original try-except block for different websockets APIs/versions is preserved inside
try:
from websockets.asyncio.client import ClientConnection
from websockets.asyncio.client import connect
except ModuleNotFoundError:
# This try/except is for TAP, mypy complains about it which is why we have the type: ignore
from websockets.client import ClientConnection # type: ignore
from websockets.client import connect # type: ignore
except ImportError:
raise ImportError(
"The `websockets` library is required for live, interactive sessions. "
"Please install it using `pip install google-genai[live]`."
)

from . import _api_module
from . import _common
Expand All @@ -40,14 +57,6 @@
from .models import _Content_to_vertex


try:
from websockets.asyncio.client import ClientConnection
from websockets.asyncio.client import connect
except ModuleNotFoundError:
# This try/except is for TAP, mypy complains about it which is why we have the type: ignore
from websockets.client import ClientConnection # type: ignore
from websockets.client import connect # type: ignore

logger = logging.getLogger('google_genai.live')

_FUNCTION_RESPONSE_REQUIRES_ID = (
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ dependencies = [
"httpx>=0.28.1, <1.0.0",
"pydantic>=2.0.0, <3.0.0",
"requests>=2.28.1, <3.0.0",
"websockets>=13.0.0, <15.1.0",
"typing-extensions>=4.11.0, <5.0.0",
]

[project.optional-dependencies]
live = ["websockets>=13.0.0, <15.1.0"]

[project.urls]
Homepage = "https://github.com/googleapis/python-genai"

Expand Down