Fix the type hints of `step_one` and `step_two` in class `MultiStepWorkflow`

#88
by renwei2024 - opened

If "Bad thing happened", the workflow should go back to method step_one, so the type of ev can be either StartEvent or LoopEvent.

Without this fix, we get the error about MultiStepWorkflow:

ERROR:asyncio:Exception in callback Dispatcher.span.<locals>.wrapper.<locals>.handle_future_result(span_id='Workflow.run...-81c386686b78', bound_args=<BoundArguments ()>, instance=<__main__.Mul...x781e13ffc810>, context=<_contextvars...x781e1c891780>)(<WorkflowHand...ate_result'")>) at /usr/local/lib/python3.11/dist-packages/llama_index/core/instrumentation/dispatcher.py:276
handle: <Handle Dispatcher.span.<locals>.wrapper.<locals>.handle_future_result(span_id='Workflow.run...-81c386686b78', bound_args=<BoundArguments ()>, instance=<__main__.Mul...x781e13ffc810>, context=<_contextvars...x781e1c891780>)(<WorkflowHand...ate_result'")>) at /usr/local/lib/python3.11/dist-packages/llama_index/core/instrumentation/dispatcher.py:276>
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/events.py", line 88, in __getattr__
    return self._data[__name]
           ~~~~~~~~~~^^^^^^^^
KeyError: 'intermediate_result'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/context.py", line 618, in _step_worker
    new_ev = await instrumented_step(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/instrumentation/dispatcher.py", line 370, in async_wrapper
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<ipython-input-8-e1ea6b7344cb>", line 26, in step_two
    final_result = f"Finished processing: {ev.intermediate_result}"
                                           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/events.py", line 90, in __getattr__
    raise AttributeError(
AttributeError: 'LoopEvent' object has no attribute 'intermediate_result'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.11/asyncio/events.py", line 84, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/instrumentation/dispatcher.py", line 288, in handle_future_result
    raise exception
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/workflow.py", line 403, in _run_workflow
    raise exception_raised
  File "/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/context.py", line 627, in _step_worker
    raise WorkflowRuntimeError(
llama_index.core.workflow.errors.WorkflowRuntimeError: Error in step 'step_two': 'LoopEvent' object has no attribute 'intermediate_result'
Bad thing happened
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/events.py in __getattr__(self, _Event__name)
     87             try:
---> 88                 return self._data[__name]
     89             except KeyError:

KeyError: 'intermediate_result'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
6 frames
AttributeError: 'LoopEvent' object has no attribute 'intermediate_result'

The above exception was the direct cause of the following exception:

WorkflowRuntimeError                      Traceback (most recent call last)
/usr/local/lib/python3.11/dist-packages/llama_index/core/workflow/context.py in _step_worker(self, name, step, config, stepwise, verbose, checkpoint_callback, run_id, service_manager, dispatcher)
    625                     except Exception as e:
    626                         if config.retry_policy is None:
--> 627                             raise WorkflowRuntimeError(
    628                                 f"Error in step '{name}': {e!s}"
    629                             ) from e

WorkflowRuntimeError: Error in step 'step_two': 'LoopEvent' object has no attribute 'intermediate_result'
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment