Skip to content

Commit 398f28f

Browse files
wukathcopybara-github
authored andcommitted
fix: Use project and location instead of API key when deploying to agent engine
Deploying using API key has been leading to "Deploy failed: 'NoneType' object has no attribute 'before_request'" errors because creating cloud resources requires ADC credentials (project id and region) Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 910821145
1 parent 92c06fe commit 398f28f

2 files changed

Lines changed: 34 additions & 51 deletions

File tree

src/google/adk/cli/cli_deploy.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,36 +1096,18 @@ def to_agent_engine(
10961096

10971097
from ..utils._google_client_headers import get_tracking_headers
10981098

1099-
if project and region:
1100-
click.echo('Initializing Vertex AI...')
1101-
client = vertexai.Client(
1102-
project=project,
1103-
location=region,
1104-
http_options={'headers': get_tracking_headers()},
1105-
)
1106-
elif api_key:
1107-
click.echo('Initializing Vertex AI in Express Mode with API key...')
1108-
client = vertexai.Client(
1109-
api_key=api_key, http_options={'headers': get_tracking_headers()}
1110-
)
1111-
else:
1112-
click.echo(
1113-
'No project/region or api_key provided. Starting onboarding flow...'
1114-
)
1099+
if not project or not region:
1100+
click.echo('No project/region provided. Starting onboarding flow...')
11151101
auth_info = _onboarding.handle_login_with_google()
1116-
if isinstance(auth_info, _onboarding.VertexAIAuth):
1117-
click.echo('Initializing Vertex AI...')
1118-
client = vertexai.Client(
1119-
project=auth_info.project_id,
1120-
location=auth_info.region,
1121-
http_options={'headers': get_tracking_headers()},
1122-
)
1123-
elif isinstance(auth_info, _onboarding.ExpressModeAuth):
1124-
click.echo('Initializing Vertex AI in Express Mode with API key...')
1125-
client = vertexai.Client(
1126-
api_key=auth_info.api_key,
1127-
http_options={'headers': get_tracking_headers()},
1128-
)
1102+
project = auth_info.project_id
1103+
region = auth_info.region
1104+
1105+
click.echo('Initializing Vertex AI...')
1106+
client = vertexai.Client(
1107+
project=project,
1108+
location=region,
1109+
http_options={'headers': get_tracking_headers()},
1110+
)
11291111
click.echo('Vertex AI initialized.')
11301112

11311113
is_config_agent = False

tests/unittests/cli/utils/test_cli_deploy.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -702,14 +702,13 @@ def test_to_agent_engine_triggers_onboarding(
702702
agent_dir: Callable[[bool, bool], Path],
703703
) -> None:
704704
"""It should trigger onboarding when credentials are missing."""
705-
onboarding_recorder = _Recorder()
706-
707-
def mock_handle_login():
708-
onboarding_recorder()
709-
return cli_deploy._onboarding.ExpressModeAuth(
710-
api_key="fake_api_key", project_id="fake_project", region="fake_region"
711-
)
712-
705+
mock_handle_login = mock.Mock(
706+
return_value=cli_deploy._onboarding.ExpressModeAuth(
707+
api_key="fake_api_key",
708+
project_id="fake_project",
709+
region="fake_region",
710+
)
711+
)
713712
monkeypatch.setattr(
714713
cli_deploy._onboarding, "handle_login_with_google", mock_handle_login
715714
)
@@ -722,23 +721,18 @@ def mock_handle_login():
722721
)
723722

724723
fake_vertexai = types.ModuleType("vertexai")
724+
mock_client = mock.Mock()
725+
fake_vertexai.Client = mock.Mock(return_value=mock_client)
725726

726-
class _FakeAgentEngines:
727+
mock_agent_engines = mock.Mock()
728+
mock_client.agent_engines = mock_agent_engines
727729

728-
def create(self, *, config: Dict[str, Any]) -> Any:
729-
_ = config
730-
return types.SimpleNamespace(
731-
api_resource=types.SimpleNamespace(
732-
name="projects/p/locations/l/reasoningEngines/e"
733-
)
730+
mock_agent_engines.create.return_value = types.SimpleNamespace(
731+
api_resource=types.SimpleNamespace(
732+
name="projects/p/locations/l/reasoningEngines/e"
734733
)
734+
)
735735

736-
class _FakeVertexClient:
737-
738-
def __init__(self, *_args: Any, **_kwargs: Any) -> None:
739-
self.agent_engines = _FakeAgentEngines()
740-
741-
fake_vertexai.Client = _FakeVertexClient
742736
monkeypatch.setitem(sys.modules, "vertexai", fake_vertexai)
743737

744738
src_dir = agent_dir(False, False)
@@ -749,4 +743,11 @@ def __init__(self, *_args: Any, **_kwargs: Any) -> None:
749743
trace_to_cloud=True,
750744
)
751745

752-
assert len(onboarding_recorder.calls) == 1
746+
mock_handle_login.assert_called_once()
747+
748+
# Verify vertexai.Client was initialized with correct args
749+
fake_vertexai.Client.assert_called_once()
750+
kwargs = fake_vertexai.Client.call_args.kwargs
751+
assert kwargs.get("project") == "fake_project"
752+
assert kwargs.get("location") == "fake_region"
753+
assert "api_key" not in kwargs or kwargs.get("api_key") is None

0 commit comments

Comments
 (0)