Skip to content

Commit e26b411

Browse files
bugfix: Throw upstream error when invalid token in Astra DB Component (#6045)
* bugfix: Throw upstream error when invalid token * [autofix.ci] apply automated fixes * Update astradb.py * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * Small cleanup of dynamic config * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent e15fddd commit e26b411

File tree

2 files changed

+62
-70
lines changed

2 files changed

+62
-70
lines changed

src/backend/base/langflow/components/vectorstores/astradb.py

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,9 @@ def get_database_object(self, api_endpoint: str | None = None):
386386
token=self.token,
387387
keyspace=self.get_keyspace(),
388388
)
389-
except Exception as e: # noqa: BLE001
390-
self.log(f"Error getting database: {e}")
391-
392-
return None
389+
except Exception as e:
390+
msg = f"Error fetching database object: {e}"
391+
raise ValueError(msg) from e
393392

394393
def collection_data(self, collection_name: str, database: Database | None = None):
395394
try:
@@ -447,61 +446,63 @@ def _initialize_database_options(self):
447446
}
448447
for name, info in self.get_database_list().items()
449448
]
450-
except Exception as e: # noqa: BLE001
451-
self.log(f"Error fetching databases: {e}")
452-
453-
return []
449+
except Exception as e:
450+
msg = f"Error fetching database options: {e}"
451+
raise ValueError(msg) from e
454452

455453
def _initialize_collection_options(self, api_endpoint: str | None = None):
454+
# Retrieve the database object
456455
database = self.get_database_object(api_endpoint=api_endpoint)
457-
if database is None:
458-
return []
459456

460-
try:
461-
collection_list = list(database.list_collections(keyspace=self.get_keyspace()))
457+
# Get the list of collections
458+
collection_list = list(database.list_collections(keyspace=self.get_keyspace()))
459+
460+
# Return the list of collections and metadata associated
461+
return [
462+
{
463+
"name": col.name,
464+
"records": self.collection_data(collection_name=col.name, database=database),
465+
"provider": (
466+
col.options.vector.service.provider if col.options.vector and col.options.vector.service else None
467+
),
468+
"icon": "",
469+
"model": (
470+
col.options.vector.service.model_name if col.options.vector and col.options.vector.service else None
471+
),
472+
}
473+
for col in collection_list
474+
]
462475

463-
return [
464-
{
465-
"name": col.name,
466-
"records": self.collection_data(collection_name=col.name, database=database),
467-
"provider": (
468-
col.options.vector.service.provider
469-
if col.options.vector and col.options.vector.service
470-
else None
471-
),
472-
"icon": "",
473-
"model": (
474-
col.options.vector.service.model_name
475-
if col.options.vector and col.options.vector.service
476-
else None
477-
),
478-
}
479-
for col in collection_list
480-
]
481-
except Exception as e: # noqa: BLE001
482-
self.log(f"Error fetching collections: {e}")
476+
def reset_build_config(self, build_config: dict):
477+
# Reset the list of databases we have based on the token provided
478+
build_config["api_endpoint"]["options"] = []
479+
build_config["api_endpoint"]["options_metadata"] = []
480+
build_config["api_endpoint"]["value"] = ""
481+
build_config["api_endpoint"]["name"] = "Database"
483482

484-
return []
483+
# Reset the list of collections and metadata associated
484+
build_config["collection_name"]["options"] = []
485+
build_config["collection_name"]["options_metadata"] = []
486+
build_config["collection_name"]["value"] = ""
487+
488+
return build_config
485489

486490
def update_build_config(self, build_config: dict, field_value: str, field_name: str | None = None):
487-
# TODO: Remove special astra flags when overlays are out
488-
# TODO: Better targeting of this field
489-
dslf = os.getenv("AWS_EXECUTION_ENV") == "AWS_ECS_FARGATE"
491+
# When the component first executes, this is the update refresh call
492+
first_run = field_name == "collection_name" and not field_value
493+
494+
# If the token has not been provided, simply return
495+
if not self.token or field_name == "environment":
496+
return self.reset_build_config(build_config)
490497

491498
# Refresh the database name options
492-
if not dslf and (field_name in ["token", "environment"] or not build_config["api_endpoint"]["options"]):
499+
if first_run or field_name == "token":
500+
# Reset the build config to ensure we are starting fresh
501+
build_config = self.reset_build_config(build_config)
502+
493503
# Get the list of options we have based on the token provided
494504
database_options = self._initialize_database_options()
495505

496-
# Reset the collection values selected
497-
build_config["collection_name"]["options"] = []
498-
build_config["collection_name"]["options_metadata"] = []
499-
build_config["collection_name"]["value"] = ""
500-
501-
# Scenario #1: We have database options from the provided token
502-
build_config["api_endpoint"]["value"] = ""
503-
build_config["api_endpoint"]["name"] = "Database"
504-
505506
# If we retrieved options based on the token, show the dropdown
506507
build_config["api_endpoint"]["options"] = [db["name"] for db in database_options]
507508
build_config["api_endpoint"]["options_metadata"] = [
@@ -521,6 +522,8 @@ def update_build_config(self, build_config: dict, field_value: str, field_name:
521522
] = self.map_cloud_providers()[cloud_provider]["regions"]
522523
"""
523524

525+
return build_config
526+
524527
# Refresh the collection name options
525528
if field_name == "api_endpoint":
526529
# Reset the selected collection
@@ -537,7 +540,7 @@ def update_build_config(self, build_config: dict, field_value: str, field_name:
537540

538541
# Reload the list of collections and metadata associated
539542
collection_options = self._initialize_collection_options(
540-
api_endpoint=build_config["d_api_endpoint"]["value"] if not dslf else None
543+
api_endpoint=build_config["d_api_endpoint"]["value"]
541544
)
542545

543546
# If we have collections, show the dropdown
@@ -546,31 +549,20 @@ def update_build_config(self, build_config: dict, field_value: str, field_name:
546549
{k: v for k, v in col.items() if k not in ["name"]} for col in collection_options
547550
]
548551

552+
return build_config
553+
549554
# Hide embedding model option if opriona_metadata provider is not null
550555
if field_name == "collection_name" and field_value:
551556
# Set the options for collection name to be the field value if its a new collection
552557
if field_value not in build_config["collection_name"]["options"]:
553-
# If this is running in DSLF, we may need to initialize the options again
554-
if dslf:
555-
# Reload the list of collections and metadata associated
556-
collection_options = self._initialize_collection_options(
557-
api_endpoint=build_config["d_api_endpoint"]["value"] if not dslf else None
558-
)
559-
560-
# If we have collections, show the dropdown
561-
build_config["collection_name"]["options"] = [col["name"] for col in collection_options]
562-
build_config["collection_name"]["options_metadata"] = [
563-
{k: v for k, v in col.items() if k not in ["name"]} for col in collection_options
564-
]
565-
else:
566-
# Add the new collection to the list of options
567-
build_config["collection_name"]["options"].append(field_value)
568-
build_config["collection_name"]["options_metadata"].append(
569-
{"records": 0, "provider": None, "icon": "", "model": None}
570-
)
571-
572-
# Ensure that autodetect collection is set to False, since its a new collection
573-
build_config["autodetect_collection"]["value"] = False
558+
# Add the new collection to the list of options
559+
build_config["collection_name"]["options"].append(field_value)
560+
build_config["collection_name"]["options_metadata"].append(
561+
{"records": 0, "provider": None, "icon": "", "model": None}
562+
)
563+
564+
# Ensure that autodetect collection is set to False, since its a new collection
565+
build_config["autodetect_collection"]["value"] = False
574566
else:
575567
build_config["autodetect_collection"]["value"] = True
576568

0 commit comments

Comments
 (0)