From 35fa93942c95ec2283236b6077d0f52c40725506 Mon Sep 17 00:00:00 2001 From: alliscode Date: Mon, 27 Apr 2026 16:32:27 -0700 Subject: [PATCH] Coerce Enum values when serializing PowerFx symbols MessageRole and other str-subclass Enums passed isinstance(v, str) and were forwarded to pythonnet unchanged. pythonnet then raised 'MessageRole value cannot be converted to System.String' for every PowerFx primitive when ConditionGroup/Expr eval walked the symbol table containing Conversation.messages. Reduce Enum members to their underlying value before the primitive check so eval sees plain strings/ints. --- .../_workflows/_declarative_base.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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 c7793dc6fc..14ea3f7fc1 100644 --- a/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py +++ b/python/packages/declarative/agent_framework_declarative/_workflows/_declarative_base.py @@ -29,6 +29,7 @@ import locale import logging import sys import uuid +from enum import Enum from collections.abc import Mapping from dataclasses import dataclass from decimal import Decimal as _Decimal @@ -121,7 +122,20 @@ def _make_powerfx_safe(value: Any) -> Any: Returns: A PowerFx-safe representation of the value """ - if value is None or isinstance(value, _POWERFX_SAFE_TYPES): + if value is None: + return value + + # Enum coercion must run BEFORE the primitive type check: many MAF + # enums (e.g. MessageRole) are ``str``-subclass enums, so they pass + # ``isinstance(v, str)`` but pythonnet refuses to convert them to + # ``System.String`` and raises ``'MessageRole' value cannot be + # converted to System.'`` for every PowerFx primitive type. Reduce + # to the underlying value (or its string form) so PowerFx sees a + # plain ``str``/``int``. + if isinstance(value, Enum): + return _make_powerfx_safe(value.value) + + if isinstance(value, _POWERFX_SAFE_TYPES): return value if isinstance(value, dict):