-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix data-science agent deployment failure with None environment varia… #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
46c2306
cc0ffb4
643f837
bd88e1a
a003d72
11242e4
6df3321
71cc55e
c4e0e6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ def create(env_vars: dict[str, str]) -> None: | |
adk_app, | ||
requirements=[AGENT_WHL_FILE], | ||
extra_packages=[AGENT_WHL_FILE], | ||
env_vars=env_vars | ||
env_vars=env_vars, | ||
) | ||
logger.info("Created remote agent: %s", remote_agent.resource_name) | ||
print(f"\nSuccessfully created agent: {remote_agent.resource_name}") | ||
|
@@ -156,7 +156,6 @@ def delete(resource_id: str) -> None: | |
print(f"\nSuccessfully deleted agent: {resource_id}") | ||
except google_exceptions.NotFound: | ||
logger.error("Agent with resource ID %s not found.", resource_id) | ||
print(f"\nAgent{resource_id} not found.") | ||
print(f"\nAgent not found: {resource_id}") | ||
except Exception as e: | ||
logger.error( | ||
|
@@ -187,56 +186,88 @@ def main(argv: list[str]) -> None: # pylint: disable=unused-argument | |
) | ||
# Don't set "GOOGLE_CLOUD_PROJECT" or "GOOGLE_CLOUD_LOCATION" | ||
# when deploying to Agent Engine. Those are set by the backend. | ||
env_vars["ROOT_AGENT_MODEL"] = os.getenv("ROOT_AGENT_MODEL") | ||
env_vars["ANALYTICS_AGENT_MODEL"] = os.getenv("ANALYTICS_AGENT_MODEL") | ||
env_vars["BASELINE_NL2SQL_MODEL"] = os.getenv("BASELINE_NL2SQL_MODEL") | ||
env_vars["BIGQUERY_AGENT_MODEL"] = os.getenv("BIGQUERY_AGENT_MODEL") | ||
env_vars["BQML_AGENT_MODEL"] = os.getenv("BQML_AGENT_MODEL") | ||
env_vars["CHASE_NL2SQL_MODEL"] = os.getenv("CHASE_NL2SQL_MODEL") | ||
env_vars["BQ_DATASET_ID"] = os.getenv("BQ_DATASET_ID") | ||
env_vars["BQ_DATA_PROJECT_ID"] = os.getenv("BQ_DATA_PROJECT_ID") | ||
env_vars["BQ_COMPUTE_PROJECT_ID"] = os.getenv("BQ_COMPUTE_PROJECT_ID") | ||
env_vars["BQML_RAG_CORPUS_NAME"] = os.getenv("BQML_RAG_CORPUS_NAME") | ||
env_vars["CODE_INTERPRETER_EXTENSION_NAME"] = os.getenv( | ||
"CODE_INTERPRETER_EXTENSION_NAME") | ||
env_vars["NL2SQL_METHOD"] = os.getenv("NL2SQL_METHOD") | ||
# Collect environment variables, filtering out None, empty, and whitespace-only values | ||
env_var_keys = [ | ||
"ROOT_AGENT_MODEL", | ||
"ANALYTICS_AGENT_MODEL", | ||
"ROOT_AGENT_MODEL", | ||
"ANALYTICS_AGENT_MODEL", | ||
"BASELINE_NL2SQL_MODEL", | ||
"BIGQUERY_AGENT_MODEL", | ||
"BQML_AGENT_MODEL", | ||
"CHASE_NL2SQL_MODEL", | ||
"BQ_DATASET_ID", | ||
"BQ_DATA_PROJECT_ID", | ||
"BQ_COMPUTE_PROJECT_ID", | ||
"BQML_RAG_CORPUS_NAME", | ||
"CODE_INTERPRETER_EXTENSION_NAME", | ||
"NL2SQL_METHOD" | ||
] | ||
|
||
for key in env_var_keys: | ||
value = os.getenv(key) | ||
# Only add to env_vars if the value has actual content (not None, empty, or whitespace-only) | ||
if value and value.strip(): | ||
env_vars[key] = value | ||
logger.info("Including environment variable: %s", key) | ||
else: | ||
logger.info("Skipping empty/None/whitespace-only environment variable: %s (value: %s)", key, repr(value)) | ||
|
||
logger.info("Environment variables to be passed to agent: %s", list(env_vars.keys())) | ||
|
||
skipped_vars: list[str] = [] | ||
for key in env_var_keys: | ||
value = os.getenv(key) | ||
# Only add to env_vars if the value is not None/empty and not just | ||
# whitespace | ||
if value and value.strip(): | ||
env_vars[key] = value | ||
else: | ||
skipped_vars.append(key) | ||
|
||
if skipped_vars: | ||
logger.info( | ||
"Skipped empty/None/whitespace environment variables: %s", | ||
skipped_vars, | ||
) | ||
|
||
logger.info( | ||
"Environment variables to be passed to agent: %s", list(env_vars.keys()) | ||
) | ||
Comment on lines
+205
to
+234
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The environment variable processing logic is duplicated. The first loop (lines 207-214) and second loop (lines 219-226) perform identical filtering. Remove the duplication by keeping only one loop and consolidating the logging. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
logger.info("Using PROJECT: %s", project_id) | ||
logger.info("Using LOCATION: %s", location) | ||
logger.info("Using BUCKET NAME: %s", bucket_name) | ||
|
||
# --- Input Validation --- | ||
if not project_id: | ||
print("\nError: Missing required GCP Project ID.") | ||
print( | ||
"Set the GOOGLE_CLOUD_PROJECT environment variable or use --project_id flag." | ||
raise app.UsageError( | ||
"Missing required GCP Project ID. Set GOOGLE_CLOUD_PROJECT or " | ||
"use --project_id flag." | ||
) | ||
return | ||
if not location: | ||
print("\nError: Missing required GCP Location.") | ||
print( | ||
"Set the GOOGLE_CLOUD_LOCATION environment variable or use --location flag." | ||
raise app.UsageError( | ||
"Missing required GCP Location. Set GOOGLE_CLOUD_LOCATION or use " | ||
"--location flag." | ||
) | ||
return | ||
if not bucket_name: | ||
print("\nError: Missing required GCS Bucket Name.") | ||
print( | ||
"Set the GOOGLE_CLOUD_STORAGE_BUCKET environment variable or use --bucket flag." | ||
raise app.UsageError( | ||
"Missing required GCS Bucket Name. Set GOOGLE_CLOUD_STORAGE_BUCKET " | ||
"or use --bucket flag." | ||
) | ||
return | ||
if not FLAGS.create and not FLAGS.delete: | ||
print("\nError: You must specify either --create or --delete flag.") | ||
return | ||
raise app.UsageError( | ||
"You must specify either --create or --delete flag." | ||
) | ||
if FLAGS.delete and not FLAGS.resource_id: | ||
print( | ||
"\nError: --resource_id is required when using the --delete flag." | ||
raise app.UsageError( | ||
"--resource_id is required when using the --delete flag." | ||
) | ||
return | ||
# --- End Input Validation --- | ||
|
||
try: | ||
# Setup staging bucket | ||
staging_bucket_uri=None | ||
staging_bucket_uri = None | ||
if FLAGS.create: | ||
staging_bucket_uri = setup_staging_bucket( | ||
project_id, location, bucket_name | ||
|
@@ -246,7 +277,7 @@ def main(argv: list[str]) -> None: # pylint: disable=unused-argument | |
vertexai.init( | ||
project=project_id, | ||
location=location, | ||
staging_bucket=staging_bucket_uri, # Staging bucket is passed directly to create/update methods now | ||
staging_bucket=staging_bucket_uri, | ||
) | ||
|
||
if FLAGS.create: | ||
|
@@ -269,11 +300,8 @@ def main(argv: list[str]) -> None: # pylint: disable=unused-argument | |
) | ||
except Exception as e: | ||
print(f"\nAn unexpected error occurred: {e}") | ||
logger.exception( | ||
"Unhandled exception in main:" | ||
) # Log the full traceback | ||
logger.exception("Unhandled exception in main:") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
app.run(main) |
Uh oh!
There was an error while loading. Please reload this page.