Skip to content

Commit 888c408

Browse files
yeesiancopybara-github
authored andcommitted
chore: Look up the agent engine specific env var for location if available
PiperOrigin-RevId: 859273451
1 parent d1735ab commit 888c408

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

  • vertexai
    • agent_engines/templates
    • preview/reasoning_engines/templates

vertexai/agent_engines/templates/adk.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,10 @@ def _warn_missing_dependency(
318318
return None
319319

320320
def _detect_cloud_resource_id(project_id: str) -> Optional[str]:
321-
location = os.getenv("GOOGLE_CLOUD_LOCATION", None)
322-
agent_engine_id = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", None)
321+
location = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", "") or os.getenv(
322+
"GOOGLE_CLOUD_LOCATION", ""
323+
)
324+
agent_engine_id = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID")
323325
if all(v is not None for v in (location, agent_engine_id)):
324326
return f"//aiplatform.googleapis.com/projects/{project_id}/locations/{location}/reasoningEngines/{agent_engine_id}"
325327
return None
@@ -359,7 +361,10 @@ def _detect_cloud_resource_id(project_id: str) -> Optional[str]:
359361
"cloud.platform": "gcp.agent_engine",
360362
"service.name": os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", ""),
361363
"service.instance.id": f"{uuid.uuid4().hex}-{os.getpid()}",
362-
"cloud.region": os.getenv("GOOGLE_CLOUD_LOCATION", ""),
364+
"cloud.region": (
365+
os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", "")
366+
or os.getenv("GOOGLE_CLOUD_LOCATION", "")
367+
),
363368
}
364369
| (
365370
{"cloud.resource_id": cloud_resource_id}
@@ -772,11 +777,15 @@ def set_up(self):
772777
os.environ["GOOGLE_CLOUD_PROJECT"] = project
773778
location = self._tmpl_attrs.get("location")
774779
if location:
775-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
780+
if "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION" not in os.environ:
781+
os.environ["GOOGLE_CLOUD_AGENT_ENGINE_LOCATION"] = location
782+
if "GOOGLE_CLOUD_LOCATION" not in os.environ:
783+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
776784
express_mode_api_key = self._tmpl_attrs.get("express_mode_api_key")
777785
if express_mode_api_key and not project:
778786
os.environ["GOOGLE_API_KEY"] = express_mode_api_key
779787
# Clear location and project env vars if express mode api key is provided.
788+
os.environ.pop("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", None)
780789
os.environ.pop("GOOGLE_CLOUD_LOCATION", None)
781790
os.environ.pop("GOOGLE_CLOUD_PROJECT", None)
782791
location = None

vertexai/preview/reasoning_engines/templates/adk.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ def _warn_missing_dependency(
320320
return None
321321

322322
def _detect_cloud_resource_id(project_id: str) -> Optional[str]:
323-
location = os.getenv("GOOGLE_CLOUD_LOCATION", None)
323+
location = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", "") or os.getenv(
324+
"GOOGLE_CLOUD_LOCATION", ""
325+
)
324326
agent_engine_id = os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", None)
325327
if all(v is not None for v in (location, agent_engine_id)):
326328
return f"//aiplatform.googleapis.com/projects/{project_id}/locations/{location}/reasoningEngines/{agent_engine_id}"
@@ -361,7 +363,10 @@ def _detect_cloud_resource_id(project_id: str) -> Optional[str]:
361363
"cloud.platform": "gcp.agent_engine",
362364
"service.name": os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", ""),
363365
"service.instance.id": f"{uuid.uuid4().hex}-{os.getpid()}",
364-
"cloud.region": os.getenv("GOOGLE_CLOUD_LOCATION", ""),
366+
"cloud.region": (
367+
os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_LOCATION", "")
368+
or os.getenv("GOOGLE_CLOUD_LOCATION", "")
369+
),
365370
}
366371
| (
367372
{"cloud.resource_id": cloud_resource_id}
@@ -688,7 +693,11 @@ def set_up(self):
688693
project = self._tmpl_attrs.get("project")
689694
os.environ["GOOGLE_CLOUD_PROJECT"] = project
690695
location = self._tmpl_attrs.get("location")
691-
os.environ["GOOGLE_CLOUD_LOCATION"] = location
696+
if location:
697+
if "GOOGLE_CLOUD_AGENT_ENGINE_LOCATION" not in os.environ:
698+
os.environ["GOOGLE_CLOUD_AGENT_ENGINE_LOCATION"] = location
699+
if "GOOGLE_CLOUD_LOCATION" not in os.environ:
700+
os.environ["GOOGLE_CLOUD_LOCATION"] = location
692701

693702
# Disable content capture in custom ADK spans unless user enabled
694703
# tracing explicitly with the old flag

0 commit comments

Comments
 (0)