10000 feat: Add Vertex Express mode compatibility for VertexAiSessionService · google/adk-python@767a4d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 767a4d0

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Add Vertex Express mode compatibility for VertexAiSessionService
PiperOrigin-RevId: 772552211
1 parent c04adaa commit 767a4d0

File tree

1 file changed

+57
-17
lines changed

1 file changed

+57
-17
lines changed

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
import asyncio
1717
import logging
18+
import os
1819
import re
1920
from typing import Any
2021
from typing import Dict
2122
from typing import Optional
2223
import urllib.parse
2324

2425
from dateutil import parser
26+
from google.genai.errors import ClientError
2527
from typing_extensions import override
2628

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

9597
max_retry_attempt = 5
96-
lro_response = None
97-
while max_retry_attempt >= 0:
98-
lro_response = await api_client.async_request(
99-
http_method='GET',
100-
path=f'operations/{operation_id}',
101-
request_dict={},
102-
)
103-
104-
if lro_response.get('done', None):
105-
break
106-
107-
await asyncio.sleep(1)
108-
max_retry_attempt -= 1
10998

110-
if lro_response is None or not lro_response.get('done', None):
111-
raise TimeoutError(
112-
f'Timeout waiting for operation {operation_id} to complete.'
113-
)
99+
if _is_vertex_express_mode(self._project, self._location):
100+
# Express mode doesn't support LRO, so we need to poll
101+
# the session resource.
102+
# TODO: remove this once LRO polling is supported in Express mode.
103+
while max_retry_attempt >= 0:
104+
try:
105+
await api_client.async_request(
106+
http_method='GET',
107+
path=(
108+
f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}'
109+
),
110+
request_dict={},
111+
)
112+
break
113+
except ClientError as e:
114+
logger.info('Polling for session %s: %s', session_id, e)
115+
await asyncio.sleep(1)
116+
max_retry_attempt -= 1
117+
continue
118+
if max_retry_attempt < 0:
119+
raise TimeoutError('Session creation failed.')
120+
else:
121+
lro_response = None
122+
while max_retry_attempt >= 0:
123+
lro_response = await api_client.async_request(
124+
http_method='GET',
125+
path=f'operations/{operation_id}',
126+
request_dict={},
127+
)
128+
129+
if lro_response.get('done', None):
130+
break
131+
132+
await asyncio.sleep(1)
133+
max_retry_attempt -= 1
134+
135+
if lro_response is None or not lro_response.get('done', None):
136+
raise TimeoutError(
137+
f'Timeout waiting for operation {operation_id} to complete.'
138+
)
114139

115140
# Get session resource
116141
get_session_api_response = await api_client.async_request(
@@ -300,9 +325,24 @@ def _get_api_client(self):
300325
client = genai.Client(
301326
vertexai=True, project=self._project, location=self._location
302327
)
328+
client._api_client._http_options.base_url = (
329+
'https://staging-aiplatform.sandbox.googleapis.com'
330+
)
303331
return client._api_client
304332

305333

334+
def _is_vertex_express_mode(
335+
project 8326 : Optional[str], location: Optional[str]
336+
) -> bool:
337+
"""Check if Vertex AI and API key are both enabled, meaning the user is using the Vertex Express Mode."""
338+
return (
339+
os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower() in ['true', '1']
340+
and os.environ.get('GOOGLE_API_KEY', None) is not None
341+
and project is None
342+
and location is None
343+
)
344+
345+
306346
def _convert_event_to_json(event: Event) -> Dict[str, Any]:
307347
metadata_json = {
308348
'partial': event.partial,

0 commit comments

Comments
 (0)
0