diff --git a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py index 9bb868135b..54c900df9b 100644 --- a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py +++ b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py @@ -30,14 +30,22 @@ from collections.abc import Mapping from dataclasses import dataclass from decimal import Decimal as _Decimal -from typing import Any, Literal, cast +from typing import TYPE_CHECKING, Any, Literal, cast from agent_framework._workflows import ( Executor, WorkflowContext, ) from agent_framework._workflows._state import State -from powerfx import Engine + +try: + from powerfx import Engine + + _powerfx_available = True +except (ImportError, RuntimeError): + _powerfx_available = False + if TYPE_CHECKING: + from powerfx import Engine # type: ignore[assignment] if sys.version_info >= (3, 11): from typing import TypedDict # type: ignore # pragma: no cover @@ -363,6 +371,14 @@ def eval(self, expression: str) -> Any: # Replace them with their evaluated results before sending to PowerFx formula = self._preprocess_custom_functions(formula) + # Check if powerfx is available + if not _powerfx_available: + raise ImportError( + "The 'powerfx' package is required to evaluate PowerFx expressions. " + "Please install .NET and the 'powerfx' package to use PowerFx expressions. " + "See https://github.com/microsoft/agent-framework for installation instructions." + ) + engine = Engine() symbols = self._to_powerfx_symbols() try: