8000 fix: Add type checking to handle different response type of genai API client by copybara-service[bot] · Pull Request #1526 · google/adk-python · GitHub
[go: up one dir, main page]

Skip to content

fix: Add type checking to handle different response type of genai API client #1526

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

Merged
merged 1 commit into from
Jun 20, 2025
Merged
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
20 changes: 18 additions & 2 deletions src/google/adk/sessions/vertex_ai_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

import asyncio
import json
import logging
import re
from typing import Any
Expand Down Expand Up @@ -87,6 +88,7 @@ async def create_session(
path=f'reasoningEngines/{reasoning_engine_id}/sessions',
request_dict=session_json_dict,
)
api_response = _convert_api_response(api_response)
logger.info(f'Create Session response {api_response}')

session_id = api_response['name'].split('/')[-3]
Expand All @@ -100,6 +102,7 @@ async def create_session(
path=f'operations/{operation_id}',
request_dict={},
)
lro_response = _convert_api_response(lro_response)

if lro_response.get('done', None):
break
Expand All @@ -118,6 +121,7 @@ async def create_session(
path=f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}',
request_dict={},
)
get_session_api_response = _convert_api_response(get_session_api_response)

update_timestamp = isoparse(
get_session_api_response['updateTime']
Expand Down Expand Up @@ -149,6 +153,7 @@ async def get_session(
path=f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}',
request_dict={},
)
get_session_api_response = _convert_api_response(get_session_api_response)

session_id = get_session_api_response['name'].split('/')[-1]
update_timestamp = isoparse(
C252 Expand All @@ -167,9 +172,12 @@ async def get_session(
path=f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}/events',
request_dict={},
)
list_events_api_response = _convert_api_response(list_events_api_response)

# Handles empty response case
if list_events_api_response.get('httpHeaders', None):
if not list_events_api_response or list_events_api_response.get(
'httpHeaders', None
):
return session

session.events += [
Expand Down Expand Up @@ -226,9 +234,10 @@ async def list_sessions(
path=path,
request_dict={},
)
api_response = _convert_api_response(api_response)

# Handles empty response case
if api_response.get('httpHeaders', None):
if not api_response or api_response.get('httpHeaders', None):
return ListSessionsResponse()

sessions = []
Expand Down Expand Up @@ -303,6 +312,13 @@ def _get_api_client(self):
return client._api_client


def _convert_api_response(api_response):
"""Converts the API response to a JSON object based on the type."""
if hasattr(api_response, 'body'):
return json.loads(api_response.body)
return api_response


def _convert_event_to_json(event: Event) -> Dict[str, Any]:
metadata_json = {
'partial': event.partial,
Expand Down
0