From 8beadaa24f59045f3ca3757cd08b7258cbf28de6 Mon Sep 17 00:00:00 2001 From: Edwin Jose Date: Thu, 18 Dec 2025 00:58:24 -0500 Subject: [PATCH] fix: Allow variable retrieval without user_id if in context (#11052) * Allow variable retrieval without user_id if in context Updated CustomComponent to first check for variables in the request context before requiring a user_id. This enables run_flow and similar operations to work without a user_id when variables are provided in the context, improving flexibility and error handling. * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * [autofix.ci] apply automated fixes (attempt 3/3) --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../custom/custom_component/custom_component.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lfx/src/lfx/custom/custom_component/custom_component.py b/src/lfx/src/lfx/custom/custom_component/custom_component.py index 61afb41696..444ff7d71a 100644 --- a/src/lfx/src/lfx/custom/custom_component/custom_component.py +++ b/src/lfx/src/lfx/custom/custom_component/custom_component.py @@ -456,24 +456,26 @@ class CustomComponent(BaseComponent): """Returns the variable for the current user with the specified name. Raises: - ValueError: If the user id is not set. + ValueError: If the user id is not set and variable not found in context. Returns: The variable for the current user with the specified name. """ - if hasattr(self, "_user_id") and not self.user_id: - msg = f"User id is not set for {self.__class__.__name__}" - raise ValueError(msg) - # Check graph context for request-level variable overrides first + # This allows run_flow to work without user_id when variables are passed if hasattr(self, "graph") and self.graph and hasattr(self.graph, "context"): context = self.graph.context if context and "request_variables" in context: request_variables = context["request_variables"] if name in request_variables: - logger.debug(f"Found context override for variable '{name}': {request_variables[name]}") + logger.debug(f"Found context override for variable '{name}'") return request_variables[name] + # Only check user_id when we need to access the database + if hasattr(self, "_user_id") and not self.user_id: + msg = f"User id is not set for {self.__class__.__name__}" + raise ValueError(msg) + variable_service = get_variable_service() # Get service instance # Retrieve and decrypt the variable by name for the current user if isinstance(self.user_id, str):