Skip to content

Commit

Permalink
update: handle more OrderDict to dict scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotzh committed Apr 25, 2024
1 parent 2be62b2 commit 50f16be
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@
"dcid",
"piezo",
"Piezo",
"cmpop"
"cmpop",
"omap"
],
"flagWords": [
"Prompt Flow"
],
"allowCompoundWords": true
}
}
8 changes: 3 additions & 5 deletions src/promptflow-core/promptflow/_utils/flow_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json
import os
import re
from collections import OrderedDict
from os import PathLike
from pathlib import Path
from typing import Optional, Tuple, Union
Expand All @@ -22,7 +21,7 @@
)
from promptflow._core._errors import MetaFileNotFound, MetaFileReadError
from promptflow._utils.logger_utils import LoggerFactory
from promptflow._utils.utils import strip_quotation
from promptflow._utils.utils import convert_ordered_dict_to_dict, strip_quotation
from promptflow._utils.yaml_utils import dump_yaml, load_yaml
from promptflow.contracts.flow import Flow as ExecutableFlow
from promptflow.exceptions import ErrorTarget, UserErrorException, ValidationException
Expand Down Expand Up @@ -157,10 +156,9 @@ def dump_flow_dag(flow_dag: dict, flow_path: Path):
"""Dump flow dag to given flow path."""
flow_dir, flow_filename = resolve_flow_path(flow_path, check_flow_exist=False)
flow_path = flow_dir / flow_filename
if isinstance(flow_dag, OrderedDict):
flow_dag = dict(flow_dag)
with open(flow_path, "w", encoding=DEFAULT_ENCODING) as f:
dump_yaml(flow_dag, f)
# directly dumping ordered dict will bring !!omap tag in yaml
dump_yaml(convert_ordered_dict_to_dict(flow_dag), f)
return flow_path


Expand Down
45 changes: 45 additions & 0 deletions src/promptflow-core/promptflow/_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,48 @@ def strip_quotation(value):
return value[1:-1]
else:
return value


def is_empty_target(obj: Optional[Dict]) -> bool:
"""Determines if it's empty target
:param obj: The object to check
:type obj: Optional[Dict]
:return: True if obj is None or an empty Dict
:rtype: bool
"""
return (
obj is None
# some objs have overloaded "==" and will cause error. e.g CommandComponent obj
or (isinstance(obj, dict) and len(obj) == 0)
)


def convert_ordered_dict_to_dict(target_object: Union[Dict, List], remove_empty: bool = True) -> Union[Dict, List]:
"""Convert ordered dict to dict. Remove keys with None value.
This is a workaround for rest request must be in dict instead of
ordered dict.
:param target_object: The object to convert
:type target_object: Union[Dict, List]
:param remove_empty: Whether to omit values that are None or empty dictionaries. Defaults to True.
:type remove_empty: bool
:return: Converted ordered dict with removed None values
:rtype: Union[Dict, List]
"""
# OrderedDict can appear nested in a list
if isinstance(target_object, list):
new_list = []
for item in target_object:
item = convert_ordered_dict_to_dict(item)
if not is_empty_target(item) or not remove_empty:
new_list.append(item)
return new_list
if isinstance(target_object, dict):
new_dict = {}
for key, value in target_object.items():
value = convert_ordered_dict_to_dict(value)
if not is_empty_target(value) or not remove_empty:
new_dict[key] = value
return new_dict
return target_object

0 comments on commit 50f16be

Please sign in to comment.