Skip to content

feat: Add Vertex Express mode compatibility for VertexAiSessionService #1472

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
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
74 changes: 57 additions & 17 deletions src/google/adk/sessions/vertex_ai_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

import asyncio
import logging
import os
import re
from typing import Any
from typing import Dict
from typing import Optional
import urllib.parse

from dateutil import parser
from google.genai.errors import ClientError
from typing_extensions import override

from google import genai
Expand Down Expand Up @@ -93,24 +95,47 @@ async def create_session(
operation_id = api_response['name'].split('/')[-1]

max_retry_attempt = 5
lro_response = None
while max_retry_attempt >= 0:
lro_response = await api_client.async_request(
http_method='GET',
path=f'operations/{operation_id}',
request_dict={},
)

if lro_response.get('done', None):
break

await asyncio.sleep(1)
max_retry_attempt -= 1

if lro_response is None or not lro_response.get('done', None):
raise TimeoutError(
f'Timeout waiting for operation {operation_id} to complete.'
)
if _is_vertex_express_mode(self._project, self._location):
# Express mode doesn't support LRO, so we need to poll
# the session resource.
# TODO: remove this once LRO polling is supported in Express mode.
while max_retry_attempt >= 0:
try:
await api_client.async_request(
http_method='GET',
path=(
f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}'
),
request_dict={},
)
break
except ClientError as e:
logger.info('Polling for session %s: %s', session_id, e)
await asyncio.sleep(1)
max_retry_attempt -= 1
continue
if max_retry_attempt < 0:
raise TimeoutError('Session creation failed.')
else:
lro_response = None
while max_retry_attempt >= 0:
lro_response = await api_client.async_request(
http_method='GET',
path=f'operations/{operation_id}',
request_dict={},
)

if lro_response.get('done', None):
break

await asyncio.sleep(1)
max_retry_attempt -= 1

if lro_response is None or not lro_response.get('done', None):
raise TimeoutError(
f'Timeout waiting for operation {operation_id} to complete.'
)

# Get session resource
get_session_api_response = await api_client.async_request(
Expand Down Expand Up @@ -300,9 +325,24 @@ def _get_api_client(self):
client = genai.Client(
vertexai=True, project=self._project, location=self._location
)
client._api_client._http_options.base_url = (
'https://staging-aiplatform.sandbox.googleapis.com'
)
return client._api_client


def _is_vertex_express_mode(
project: Optional[str], location: Optional[str]
) -> bool:
"""Check if Vertex AI and API key are both enabled, meaning the user is using the Vertex Express Mode."""
return (
os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower() in ['true', '1']
and os.environ.get('GOOGLE_API_KEY', None) is not None
and project is None
and location is None
)


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