Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Fix the bug that call flow in flow function will fail #2267

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/promptflow/promptflow/_core/operation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class OperationContext(Dict):
_DEFAULT_TRACKING_KEYS = {"run_mode", "root_run_id", "flow_id", "batch_input_source"}
_TRACKING_KEYS = "_tracking_keys"

def copy(self):
ctx = OperationContext(self)
ctx[OperationContext._OTEL_ATTRIBUTES] = copy.copy(self._get_otel_attributes())
return ctx

def _add_otel_attributes(self, key, value):
attributes = self.get(OperationContext._OTEL_ATTRIBUTES, {})
attributes[key] = value
Expand Down Expand Up @@ -64,6 +69,10 @@ def get_instance(cls):
instance[cls._TRACKING_KEYS] = copy.copy(cls._DEFAULT_TRACKING_KEYS)
return instance

@classmethod
def set_instance(cls, instance):
cls._current_context.set(instance)

def __setattr__(self, name, value):
"""Set the attribute.

Expand Down
9 changes: 2 additions & 7 deletions src/promptflow/promptflow/executor/flow_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ async def exec_line_async(
@contextlib.contextmanager
def _update_operation_context(self, run_id: str, line_number: int):
operation_context = OperationContext.get_instance()
original_context = operation_context.copy()
original_mode = operation_context.get("run_mode", None)
values_for_context = {"flow_id": self._flow_id, "root_run_id": run_id}
if original_mode == RunMode.Batch.name:
Expand All @@ -791,13 +792,7 @@ def _update_operation_context(self, run_id: str, line_number: int):
operation_context._add_otel_attributes(k, v)
yield
finally:
for k in values_for_context:
operation_context.pop(k)
operation_context._remove_otel_attributes(values_for_otel.keys())
if original_mode is None:
operation_context.pop("run_mode")
else:
operation_context.run_mode = original_mode
OperationContext.set_instance(original_context)

def _add_line_results(self, line_results: List[LineResult], run_tracker: Optional[RunTracker] = None):
run_tracker = run_tracker or self._run_tracker
Expand Down
Loading