@@ -1143,6 +1143,7 @@ def _execute_inference(
11431143 prompt_template : Optional [Union [str , types .PromptTemplateOrDict ]] = None ,
11441144 location : Optional [str ] = None ,
11451145 user_simulator_config : Optional [types .evals .UserSimulatorConfig ] = None ,
1146+ allow_cross_region_model : bool = False ,
11461147) -> pd .DataFrame :
11471148 """Executes inference on a given dataset using the specified model.
11481149
@@ -1250,6 +1251,7 @@ def _execute_inference(
12501251 agent = agent ,
12511252 prompt_dataset = prompt_dataset ,
12521253 user_simulator_config = user_simulator_config ,
1254+ allow_cross_region_model = allow_cross_region_model ,
12531255 )
12541256 end_time = time .time ()
12551257 logger .info ("Agent Run completed in %.2f seconds." , end_time - start_time )
@@ -1823,6 +1825,7 @@ def _run_agent_internal(
18231825 agent : Optional [LlmAgent ],
18241826 prompt_dataset : pd .DataFrame ,
18251827 user_simulator_config : Optional [types .evals .UserSimulatorConfig ] = None ,
1828+ allow_cross_region_model : bool = False ,
18261829) -> pd .DataFrame :
18271830 """Runs an agent."""
18281831 raw_responses = _run_agent (
@@ -1831,6 +1834,7 @@ def _run_agent_internal(
18311834 agent = agent ,
18321835 prompt_dataset = prompt_dataset ,
18331836 user_simulator_config = user_simulator_config ,
1837+ allow_cross_region_model = allow_cross_region_model ,
18341838 )
18351839 processed_intermediate_events = []
18361840 processed_responses = []
@@ -1872,6 +1876,7 @@ def _run_agent(
18721876 agent : Optional [LlmAgent ],
18731877 prompt_dataset : pd .DataFrame ,
18741878 user_simulator_config : Optional [types .evals .UserSimulatorConfig ] = None ,
1879+ allow_cross_region_model : bool = False ,
18751880) -> list [
18761881 Union [
18771882 list [dict [str , Any ]],
@@ -1880,28 +1885,60 @@ def _run_agent(
18801885 ]
18811886]:
18821887 """Internal helper to run inference using Gemini model with concurrency."""
1883- if agent_engine :
1884- return _execute_inference_concurrently (
1885- api_client = api_client ,
1886- agent_engine = agent_engine ,
1887- prompt_dataset = prompt_dataset ,
1888- progress_desc = "Agent Run" ,
1889- gemini_config = None ,
1890- user_simulator_config = None ,
1891- inference_fn = _execute_agent_run_with_retry ,
1892- )
1893- elif agent :
1894- return _execute_inference_concurrently (
1895- api_client = api_client ,
1896- agent = agent ,
1897- prompt_dataset = prompt_dataset ,
1898- progress_desc = "Local Agent Run" ,
1899- gemini_config = None ,
1900- user_simulator_config = user_simulator_config ,
1901- inference_fn = _execute_local_agent_run_with_retry ,
1902- )
1903- else :
1904- raise ValueError ("Neither agent_engine nor agent is provided." )
1888+ original_location = os .environ .get ("GOOGLE_CLOUD_LOCATION" )
1889+ location_overridden = False
1890+
1891+ if user_simulator_config and user_simulator_config .model_name :
1892+ model_name = user_simulator_config .model_name
1893+ if model_name .startswith ("gemini-3" ) and "/" not in model_name :
1894+ current_location = original_location or api_client .location or "us-central1"
1895+ if current_location != "global" and not allow_cross_region_model :
1896+ raise ValueError (
1897+ f"The model '{ model_name } ' is currently only available in the"
1898+ " 'global' region. Because this request originated in"
1899+ f" '{ current_location } ', you must explicitly set "
1900+ "allow_cross_region_model=True to allow your data to be routed outside"
1901+ " of your request's region."
1902+ )
1903+
1904+ logger .warning (
1905+ "Model %s is only available in the global region. Routing to global." ,
1906+ model_name ,
1907+ )
1908+ user_simulator_config .model_name = f"projects/{ api_client .project } /locations/global/publishers/google/models/{ model_name } "
1909+ if original_location != "global" :
1910+ os .environ ["GOOGLE_CLOUD_LOCATION" ] = "global"
1911+ location_overridden = True
1912+
1913+ try :
1914+ if agent_engine :
1915+ return _execute_inference_concurrently (
1916+ api_client = api_client ,
1917+ agent_engine = agent_engine ,
1918+ prompt_dataset = prompt_dataset ,
1919+ progress_desc = "Agent Run" ,
1920+ gemini_config = None ,
1921+ user_simulator_config = None ,
1922+ inference_fn = _execute_agent_run_with_retry ,
1923+ )
1924+ elif agent :
1925+ return _execute_inference_concurrently (
1926+ api_client = api_client ,
1927+ agent = agent ,
1928+ prompt_dataset = prompt_dataset ,
1929+ progress_desc = "Local Agent Run" ,
1930+ gemini_config = None ,
1931+ user_simulator_config = user_simulator_config ,
1932+ inference_fn = _execute_local_agent_run_with_retry ,
1933+ )
1934+ else :
1935+ raise ValueError ("Neither agent_engine nor agent is provided." )
1936+ finally :
1937+ if location_overridden :
1938+ if original_location is None :
1939+ del os .environ ["GOOGLE_CLOUD_LOCATION" ]
1940+ else :
1941+ os .environ ["GOOGLE_CLOUD_LOCATION" ] = original_location
19051942
19061943
19071944def _execute_agent_run_with_retry (
0 commit comments