Skip to content

Commit abf53f1

Browse files
committed
refactor: make Redis an optional dependency with better error handling
- Move redis and orjson to optional dependencies under [redis] extra - Add clear ImportError messages guiding users to install with [redis] extra - Reduce base package size by making Redis dependencies opt-in
1 parent d564649 commit abf53f1

4 files changed

Lines changed: 22 additions & 13 deletions

File tree

contributing/samples/open_memory/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ for ADK agents using the community package.
1414
### 1. Install Dependencies
1515

1616
```bash
17-
pip install google-adk google-adk-community
17+
pip install google-adk-community[redis]
1818
```
1919

2020
### 2. Set Up OpenMemory Server

contributing/samples/redis_session_service/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This sample demonstrates how to use Redis as a session storage backend for ADK a
1313
### 1. Install Dependencies
1414

1515
```bash
16-
pip install google-adk google-adk-community
16+
pip install google-adk-community[redis]
1717
```
1818

1919
### 2. Set Up Redis Server
@@ -109,15 +109,15 @@ async def get_health():
109109
return {"status": "ok"}
110110

111111
# Run the server
112-
uvicorn.run(app, host="0.0.0.0", port=8080)
112+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
113113
```
114114

115115
### Direct RedisSessionService Usage
116116

117117
You can also use `RedisSessionService` directly without FastAPI:
118118

119119
```python
120-
from google.adk_community.sessions import RedisSessionService
120+
from google.adk_community.sessions.redis_session_service import RedisSessionService
121121
from google.adk.runners import Runner
122122
from google.adk.errors.already_exists_error import AlreadyExistsError
123123
from google.adk.agents import Agent

pyproject.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ dependencies = [
2828
"google-genai>=1.21.1, <2.0.0", # Google GenAI SDK
2929
"google-adk", # Google ADK
3030
"httpx>=0.27.0, <1.0.0", # For OpenMemory service
31-
"redis >= 7.0.0", # Redis for session storage
3231
# go/keep-sorted end
33-
"orjson>=3.11.4",
3432
]
3533
dynamic = ["version"]
3634

@@ -41,9 +39,13 @@ changelog = "https://github.com/google/adk-python-community/blob/main/CHANGELOG.
4139
documentation = "https://google.github.io/adk-docs/"
4240

4341
[project.optional-dependencies]
42+
redis = [
43+
"redis>=7.0.0", # Redis for session storage
44+
"orjson>=3.11.4", # Fast JSON serialization for Redis
45+
]
4446
test = [
45-
"pytest>=8.4.2",
46-
"pytest-asyncio>=1.2.0",
47+
"pytest>=8.4.2",
48+
"pytest-asyncio>=1.2.0",
4749
]
4850

4951

@@ -72,8 +74,8 @@ build-backend = "flit_core.buildapi"
7274

7375
[dependency-groups]
7476
dev = [
75-
"pytest>=8.4.2",
76-
"pytest-asyncio>=1.2.0",
77+
"pytest>=8.4.2",
78+
"pytest-asyncio>=1.2.0",
7779
]
7880

7981

src/google/adk_community/sessions/redis_session_service.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@
2020
import uuid
2121
from typing import Any, Optional
2222

23-
import orjson
24-
import redis.asyncio as redis
25-
from redis.crc import key_slot
2623
from typing_extensions import override
2724

25+
try:
26+
import orjson
27+
import redis.asyncio as redis
28+
from redis.crc import key_slot
29+
except ImportError as exc:
30+
raise ImportError(
31+
"redis and orjson are required to use RedisSessionService. "
32+
"Install it with: pip install google-adk-community[redis]"
33+
) from exc
34+
2835
from google.adk.events.event import Event
2936
from google.adk.sessions.base_session_service import (
3037
BaseSessionService,

0 commit comments

Comments
 (0)