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+ import asyncio
1415import logging
1516import re
16- import time
1717from typing import Any
1818from typing import Optional
1919
@@ -69,7 +69,8 @@ async def create_session(
6969 if state :
7070 session_json_dict ['session_state' ] = state
7171
72- api_response = self .api_client .request (
72+ api_client = _get_api_client (self .project , self .location )
73+ api_response = await api_client .async_request (
7374 http_method = 'POST' ,
7475 path = f'reasoningEngines/{ reasoning_engine_id } /sessions' ,
7576 request_dict = session_json_dict ,
@@ -81,7 +82,7 @@ async def create_session(
8182
8283 max_retry_attempt = 5
8384 while max_retry_attempt >= 0 :
84- lro_response = self . api_client .request (
85+ lro_response = await api_client .async_request (
8586 http_method = 'GET' ,
8687 path = f'operations/{ operation_id } ' ,
8788 request_dict = {},
@@ -90,11 +91,11 @@ async def create_session(
9091 if lro_response .get ('done' , None ):
9192 break
9293
93- time .sleep (1 )
94+ await asyncio .sleep (1 )
9495 max_retry_attempt -= 1
9596
9697 # Get session resource
97- get_session_api_response = self . api_client .request (
98+ get_session_api_response = await api_client .async_request (
9899 http_method = 'GET' ,
99100 path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
100101 request_dict = {},
@@ -124,7 +125,8 @@ async def get_session(
124125 reasoning_engine_id = _parse_reasoning_engine_id (app_name )
125126
126127 # Get session resource
127- get_session_api_response = self .api_client .request (
128+ api_client = _get_api_client (self .project , self .location )
129+ get_session_api_response = await api_client .async_request (
128130 http_method = 'GET' ,
129131 path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
130132 request_dict = {},
@@ -142,7 +144,7 @@ async def get_session(
142144 last_update_time = update_timestamp ,
143145 )
144146
145- list_events_api_response = self . api_client .request (
147+ list_events_api_response = await api_client .async_request (
146148 http_method = 'GET' ,
147149 path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } /events' ,
148150 request_dict = {},
@@ -181,7 +183,8 @@ async def list_sessions(
181183 ) -> ListSessionsResponse :
182184 reasoning_engine_id = _parse_reasoning_engine_id (app_name )
183185
184- api_response = self .api_client .request (
186+ api_client = _get_api_client (self .project , self .location )
187+ api_response = await api_client .async_request (
185188 http_method = 'GET' ,
186189 path = f'reasoningEngines/{ reasoning_engine_id } /sessions?filter=user_id={ user_id } ' ,
187190 request_dict = {},
@@ -207,7 +210,8 @@ async def delete_session(
207210 self , * , app_name : str , user_id : str , session_id : str
208211 ) -> None :
209212 reasoning_engine_id = _parse_reasoning_engine_id (app_name )
210- self .api_client .request (
213+ api_client = _get_api_client (self .project , self .location )
214+ await api_client .async_request (
211215 http_method = 'DELETE' ,
212216 path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session_id } ' ,
213217 request_dict = {},
@@ -219,15 +223,25 @@ async def append_event(self, session: Session, event: Event) -> Event:
219223 await super ().append_event (session = session , event = event )
220224
221225 reasoning_engine_id = _parse_reasoning_engine_id (session .app_name )
222- self .api_client .request (
226+ api_client = _get_api_client (self .project , self .location )
227+ await api_client .async_request (
223228 http_method = 'POST' ,
224229 path = f'reasoningEngines/{ reasoning_engine_id } /sessions/{ session .id } :appendEvent' ,
225230 request_dict = _convert_event_to_json (event ),
226231 )
227-
228232 return event
229233
230234
235+ def _get_api_client (project : str , location : str ):
236+ """Instantiates an API client for the given project and location.
237+
238+ It needs to be instantiated inside each request so that the event loop
239+ management.
240+ """
241+ client = genai .Client (vertexai = True , project = project , location = location )
242+ return client ._api_client
243+
244+
231245def _convert_event_to_json (event : Event ):
232246 metadata_json = {
233247 'partial' : event .partial ,
0 commit comments