uranusjr commented on code in PR #61394:
URL: https://github.com/apache/airflow/pull/61394#discussion_r2780883274


##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -957,13 +957,35 @@ def sort_dict_recursively(obj: Any) -> Any:
             return tuple(sort_dict_recursively(item) for item in obj)
         return obj
 
+    def to_serializable(obj: Any) -> Any:
+        """Convert objects to JSON serializable types."""
+        if obj is None or isinstance(obj, (str, int, float, bool)):
+            return obj
+
+        if isinstance(obj, dict):
+            return {k: to_serializable(v) for k, v in obj.items()}
+
+        if isinstance(obj, (list, tuple)):
+            return [to_serializable(item) for item in obj]
+
+        # Some objects like Kubernetes ones or boto3 ones have to_dict 
defined, try using them if they exist
+        if hasattr(obj, "to_dict"):
+            return obj.to_dict()
+
+        return obj

Review Comment:
   I wonder if it’d be a good idea to use `default` instead. Something like…
   
   ```python
   def _fallback_serialization(obj):
       # Some objects like Kubernetes ones or boto3 ones have to_dict defined, 
try using them if they exist
       if hasattr(obj, "to_dict"):
           return obj.to_dict()
       raise TypeError(f"cannot serialize {obj}")
   
   try:
       serialized = json.dumps(template_field, default=_fallback_serialization)
   except (TypeError, ValueError):
       serialized = str(template_field)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to