1818import json
1919import logging
2020import time
21- from typing import Any , Callable , Iterator , Optional , Sequence , Union
21+ from typing import Any , Iterator , Optional , Sequence , Tuple , Union
2222from urllib .parse import urlencode
2323
2424from google .genai import _api_module
2525from google .genai import _common
26- from google .genai import types as genai_types
2726from google .genai ._common import get_value_by_path as getv
2827from google .genai ._common import set_value_by_path as setv
2928from google .genai .pagers import Pager
@@ -2045,10 +2044,13 @@ def create(
20452044 api_async_client = AsyncAgentEngines (api_client_ = self ._api_client ),
20462045 api_resource = operation .response ,
20472046 )
2048- logger .info ("Agent Engine created. To use it in another session:" )
2049- logger .info (
2050- f"agent_engine=client.agent_engines.get('{ agent .api_resource .name } ')"
2051- )
2047+ if agent .api_resource :
2048+ logger .info ("Agent Engine created. To use it in another session:" )
2049+ logger .info (
2050+ f"agent_engine=client.agent_engines.get('{ agent .api_resource .name } ')"
2051+ )
2052+ else :
2053+ logger .warning ("The operation returned an empty response." )
20522054 if agent_engine is not None :
20532055 # If the user did not provide an agent_engine (e.g. lightweight
20542056 # provisioning), it will not have any API methods registered.
@@ -2067,13 +2069,13 @@ def _create_config(
20672069 gcs_dir_name : Optional [str ] = None ,
20682070 extra_packages : Optional [Sequence [str ]] = None ,
20692071 env_vars : Optional [dict [str , Union [str , Any ]]] = None ,
2070- context_spec : Optional [dict [ str , Any ] ] = None ,
2071- ):
2072+ context_spec : Optional [types . ReasoningEngineContextSpecDict ] = None ,
2073+ ) -> types . UpdateAgentEngineConfigDict :
20722074 import sys
20732075 from vertexai .agent_engines import _agent_engines
20742076 from vertexai .agent_engines import _utils
20752077
2076- config = {}
2078+ config : types . UpdateAgentEngineConfigDict = {}
20772079 update_masks = []
20782080 if mode not in ["create" , "update" ]:
20792081 raise ValueError (f"Unsupported mode: { mode } " )
@@ -2091,28 +2093,36 @@ def _create_config(
20912093 if context_spec is not None :
20922094 config ["context_spec" ] = context_spec
20932095 if agent_engine is not None :
2096+ project = self ._api_client .project
2097+ if project is None :
2098+ raise ValueError ("project must be set using `vertexai.Client`." )
2099+ location = self ._api_client .location
2100+ if location is None :
2101+ raise ValueError ("location must be set using `vertexai.Client`." )
20942102 sys_version = f"{ sys .version_info .major } .{ sys .version_info .minor } "
20952103 gcs_dir_name = gcs_dir_name or _agent_engines ._DEFAULT_GCS_DIR_NAME
20962104 agent_engine = _agent_engines ._validate_agent_engine_or_raise (
20972105 agent_engine = agent_engine , logger = logger
20982106 )
2099- _agent_engines ._validate_staging_bucket_or_raise (staging_bucket )
2107+ staging_bucket = _agent_engines ._validate_staging_bucket_or_raise (
2108+ staging_bucket = staging_bucket
2109+ )
21002110 requirements = _agent_engines ._validate_requirements_or_raise (
21012111 agent_engine = agent_engine ,
21022112 requirements = requirements ,
21032113 logger = logger ,
21042114 )
21052115 extra_packages = _agent_engines ._validate_extra_packages_or_raise (
2106- extra_packages
2116+ extra_packages = extra_packages ,
21072117 )
21082118 # Prepares the Agent Engine for creation/update in Vertex AI. This
21092119 # involves packaging and uploading the artifacts for agent_engine,
21102120 # requirements and extra_packages to `staging_bucket/gcs_dir_name`.
21112121 _agent_engines ._prepare (
21122122 agent_engine = agent_engine ,
21132123 requirements = requirements ,
2114- project = self . _api_client . project ,
2115- location = self . _api_client . location ,
2124+ project = project ,
2125+ location = location ,
21162126 staging_bucket = staging_bucket ,
21172127 gcs_dir_name = gcs_dir_name ,
21182128 extra_packages = extra_packages ,
@@ -2142,7 +2152,9 @@ def _create_config(
21422152 gcs_dir_name ,
21432153 _agent_engines ._REQUIREMENTS_FILE ,
21442154 )
2145- agent_engine_spec = {"package_spec" : package_spec }
2155+ agent_engine_spec : types .ReasoningEngineSpecDict = {
2156+ "package_spec" : package_spec ,
2157+ }
21462158 if env_vars is not None :
21472159 (
21482160 deployment_spec ,
@@ -2172,7 +2184,7 @@ def _generate_deployment_spec_or_raise(
21722184 self ,
21732185 * ,
21742186 env_vars : Optional [dict [str , Union [str , Any ]]] = None ,
2175- ):
2187+ ) -> Tuple [ dict [ str , Any ], Sequence [ str ]] :
21762188 deployment_spec : dict [str , Any ] = {}
21772189 update_masks = []
21782190 if env_vars :
@@ -2217,7 +2229,7 @@ def _await_operation(
22172229 * ,
22182230 operation_name : str ,
22192231 poll_interval_seconds : int = 10 ,
2220- get_operation_fn : Optional [Callable [[ str ], Any ] ] = None ,
2232+ get_operation_fn : Optional [_agent_engines_utils . GetOperationFunction ] = None ,
22212233 ) -> Any :
22222234 """Waits for the operation for creating an agent engine to complete.
22232235
@@ -2375,10 +2387,11 @@ def update(
23752387 api_async_client = AsyncAgentEngines (api_client_ = self ._api_client ),
23762388 api_resource = operation .response ,
23772389 )
2378- logger .info ("Agent Engine updated. To use it in another session:" )
2379- logger .info (
2380- f"agent_engine=client.agent_engines.get('{ agent .api_resource .name } ')"
2381- )
2390+ if agent .api_resource :
2391+ logger .info ("Agent Engine updated. To use it in another session:" )
2392+ logger .info (
2393+ f"agent_engine=client.agent_engines.get('{ agent .api_resource .name } ')"
2394+ )
23822395 return self ._register_api_methods (agent = agent )
23832396
23842397 def _stream_query (
@@ -2403,7 +2416,7 @@ def _stream_query(
24032416 path = f"{ path } ?{ urlencode (query_params )} "
24042417 # TODO: remove the hack that pops config.
24052418 request_dict .pop ("config" , None )
2406- http_options : Optional [ genai_types . HttpOptions ] = None
2419+ http_options = None
24072420 if (
24082421 parameter_model .config is not None
24092422 and parameter_model .config .http_options is not None
0 commit comments