8000 chore: Minor improvement to session service · gadda00/adk-python@0127c3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0127c3f

Browse files
google-genai-botcopybara-github
authored andcommitted
chore: Minor improvement to session service
- Add missing override. - Add warning to failed actions. - Remove unused import. - Remove unused fields. - Add type checking. PiperOrigin-RevId: 766913196
1 parent 0724a83 commit 0127c3f

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

src/google/adk/sessions/in_memory_session_service.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from __future__ import annotations
1514

1615
import copy
1716
import logging
@@ -224,7 +223,6 @@ def _list_sessions_impl(
224223
sessions_without_events.append(copied_session)
225224
return ListSessionsResponse(sessions=sessions_without_events)
226225

227-
@override
228226
async def delete_session(
229227
self, *, app_name: str, user_id: str, session_id: str
230228
) -> None:
@@ -249,7 +247,7 @@ def _delete_session_impl(
249247
)
250248
is None
251249
):
252-
return
250+
return None
253251

254252
self.sessions[app_name][user_id].pop(session_id)
255253

@@ -263,20 +261,11 @@ async def append_event(self, session: Session, event: Event) -> Event:
263261
app_name = session.app_name
264262
user_id = session.user_id
265263
session_id = session.id
266-
267-
def _warning(message: str) -> None:
268-
logger.warning(
269-
f'Failed to append event to session {session_id}: {message}'
270-
)
271-
272264
if app_name not in self.sessions:
273-
_warning(f'app_name {app_name} not in sessions')
274265
return event
275266
if user_id not in self.sessions[app_name]:
276-
_warning(f'user_id {user_id} not in sessions[app_name]')
277267
return event
278268
if session_id not in self.sessions[app_name][user_id]:
279-
_warning(f'session_id {session_id} not in sessions[app_name][user_id]')
280269
return event
281270

282271
if event.actions and event.actions.state_delta:

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import asyncio
1717
import logging
1818
import re
19+
import time
1920
from typing import Any
20-
from typing import Dict
2121
from typing import Optional
2222
import urllib.parse
2323

@@ -50,6 +50,9 @@ def __init__(
5050
self.project = project
5151
self.location = location
5252

53+
client = genai.Client(vertexai=True, project=project, location=location)
54+
self.api_client = client._api_client
55+
5356
@override
5457
async def create_session(
5558
self,
@@ -83,7 +86,6 @@ async def create_session(
8386
operation_id = api_response['name'].split('/')[-1]
8487

8588
max_retry_attempt = 5
86-
lro_response = None
8789
while max_retry_attempt >= 0:
8890
lro_response = await api_client.async_request(
8991
http_method='GET',
@@ -97,11 +99,6 @@ async def create_session(
9799
await asyncio.sleep(1)
98100
max_retry_attempt -= 1
99101

100-
if lro_response is None or not lro_response.get('done', None):
101-
raise TimeoutError(
102-
f'Timeout waiting for operation {operation_id} to complete.'
103-
)
104-
105102
# Get session resource
106103
get_session_api_response = await api_client.async_request(
107104
http_method='GET',
@@ -238,15 +235,11 @@ async def delete_session(
238235
) -> None:
239236
reasoning_engine_id = _parse_reasoning_engine_id(app_name)
240237
api_client = _get_api_client(self.project, self.location)
241-
try:
242-
await api_client.async_request(
243-
http_method='DELETE',
244-
path=f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}',
245-
request_dict={},
246-
)
247-
except Exception as e:
248-
logger.error(f'Error deleting session {session_id}: {e}')
249-
raise e
238+
await api_client.async_request(
239+
http_method='DELETE',
240+
path=f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}',
241+
request_dict={},
242+
)
250243

251244
@override
252245
async def append_event(self, session: Session, event: Event) -> Event:
@@ -273,7 +266,7 @@ def _get_api_client(project: str, location: str):
273266
return client._api_client
274267

275268

276-
def _convert_event_to_json(event: Event) -> Dict[str, Any]:
269+
def _convert_event_to_json(event: Event):
277270
metadata_json = {
278271
'partial': event.partial,
279272
'turn_complete': event.turn_complete,
@@ -325,7 +318,7 @@ def _convert_event_to_json(event: Event) -> Dict[str, Any]:
325318
return event_json
326319

327320

328-
def _from_api_event(api_event: Dict[str, Any]) -> Event:
321+
def _from_api_event(api_event: dict) -> Event:
329322
event_actions = EventActions()
330323
if api_event.get('actions', None):
331324
event_actions = EventActions(

0 commit comments

Comments
 (0)
0