diff --git a/src/promptflow/promptflow/_sdk/_utils.py b/src/promptflow/promptflow/_sdk/_utils.py index e2f8f7bf558..631211550ae 100644 --- a/src/promptflow/promptflow/_sdk/_utils.py +++ b/src/promptflow/promptflow/_sdk/_utils.py @@ -369,17 +369,9 @@ def safe_parse_object_list(obj_list, parser, message_generator): return results -def _normalize_identifier_name(name): - normalized_name = name.lower() - normalized_name = re.sub(r"[\W_]", " ", normalized_name) # No non-word characters - normalized_name = re.sub(" +", " ", normalized_name).strip() # No double spaces, leading or trailing spaces - if re.match(r"\d", normalized_name): - normalized_name = "n" + normalized_name # No leading digits - return normalized_name - - def _sanitize_python_variable_name(name: str): - return _normalize_identifier_name(name).replace(" ", "_") + from promptflow._utils.utils import _sanitize_python_variable_name + return _sanitize_python_variable_name(name) def _get_additional_includes(yaml_path): diff --git a/src/promptflow/promptflow/_utils/utils.py b/src/promptflow/promptflow/_utils/utils.py index bbaf46399f7..bb789f30034 100644 --- a/src/promptflow/promptflow/_utils/utils.py +++ b/src/promptflow/promptflow/_utils/utils.py @@ -296,3 +296,16 @@ def prompt_y_n(msg, default=None): def prompt_input(msg): return input("\n===> " + msg) + + +def _normalize_identifier_name(name): + normalized_name = name.lower() + normalized_name = re.sub(r"[\W_]", " ", normalized_name) # No non-word characters + normalized_name = re.sub(" +", " ", normalized_name).strip() # No double spaces, leading or trailing spaces + if re.match(r"\d", normalized_name): + normalized_name = "n" + normalized_name # No leading digits + return normalized_name + + +def _sanitize_python_variable_name(name: str): + return _normalize_identifier_name(name).replace(" ", "_") diff --git a/src/promptflow/promptflow/contracts/flow.py b/src/promptflow/promptflow/contracts/flow.py index 1ef62efbb8d..6859902068f 100644 --- a/src/promptflow/promptflow/contracts/flow.py +++ b/src/promptflow/promptflow/contracts/flow.py @@ -17,7 +17,7 @@ from .._constants import LANGUAGE_KEY, FlowLanguage from .._sdk._constants import DEFAULT_ENCODING from .._utils.dataclass_serializer import serialize -from .._utils.utils import try_import +from .._utils.utils import try_import, _sanitize_python_variable_name from ._errors import FailedToImportModule from .tool import ConnectionType, Tool, ToolType, ValueType @@ -601,7 +601,7 @@ def deserialize(data: dict) -> "Flow": outputs = data.get("outputs") or {} return Flow( # TODO: Remove this fallback. - data.get("id", data.get("name", "default_flow_id")), + data.get("id", "default_flow_id"), data.get("name", "default_flow"), nodes, {name: FlowInputDefinition.deserialize(i) for name, i in inputs.items()}, @@ -655,6 +655,7 @@ def from_yaml(cls, flow_file: Path, working_dir=None) -> "Flow": working_dir = cls._parse_working_dir(flow_file, working_dir) with open(working_dir / flow_file, "r", encoding=DEFAULT_ENCODING) as fin: flow_dag = load_yaml(fin) + flow_dag["name"] = flow_dag.get("name", _sanitize_python_variable_name(working_dir.stem)) return Flow._from_dict(flow_dag=flow_dag, working_dir=working_dir) @classmethod diff --git a/src/promptflow/promptflow/executor/flow_executor.py b/src/promptflow/promptflow/executor/flow_executor.py index c37b5472200..49c67bbb9ae 100644 --- a/src/promptflow/promptflow/executor/flow_executor.py +++ b/src/promptflow/promptflow/executor/flow_executor.py @@ -782,7 +782,7 @@ def _exec_with_trace( Returns: LineResult: Line run result """ - with open_telemetry_tracer.start_as_current_span("promptflow.flow") as span: + with open_telemetry_tracer.start_as_current_span(self._flow.name) as span: # initialize span span.set_attributes( {