griptape icon indicating copy to clipboard operation
griptape copied to clipboard

CodeExecutionTask doesn't handle inputs correctly when in a Workflow

Open shhlife opened this issue 11 months ago • 2 comments

Is your feature request related to a problem? Please describe. When using a pipeline, the CodeExecutionTask can access the parent task's output like this:

def my_fn(task: CodeExecutionTask) -> BaseArtifact:
  print(task.input)
  return TextArtifact(task.input)

However, Workflows don't work this way because there are potentially multiple parent tasks. You end up needing to do this instead: task.parents[0].output.value.

Describe the solution you'd like It would be better if we could use task.inputs[0] instead, that way it would be more consistent with pipelines.

shhlife avatar Mar 25 '24 08:03 shhlife

@shhlife does this only happen with CodeExecutionTask?

collindutter avatar Mar 25 '24 16:03 collindutter

I'm not sure.. for regular pipeline tasks we reference the task input value as parent_output, and for workflows it's parent_outputs.

But those haven't been defined as functions the way the CodeExecutionTask needs to be

shhlife avatar Mar 25 '24 16:03 shhlife

@shhlife I think everything is working as expected here.

In the example you provided:

def my_fn(task: CodeExecutionTask) -> BaseArtifact:
  print(task.input)
  return TextArtifact(task.input)

This code will work the same regardless of whether it is in a Pipeline or Workflow. Tasks always have a single input, single output.

If you're looking to get the output of the Task's parent(s), then the syntax will look something like this:

from griptape.artifacts import TextArtifact
from griptape.structures import Pipeline, Workflow
from griptape.tasks import CodeExecutionTask, PromptTask

pipeline = Pipeline(
    tasks=[
        PromptTask("Output an animal, just the name."),
        CodeExecutionTask(run_fn=lambda task: TextArtifact(task.parents_output_text.upper())),
    ]
)
print(pipeline.run().output.value)


workflow = Workflow(
    tasks=[
        PromptTask("Output an animal, just the name.", id="animal"),
        PromptTask("Output an color, just the name.", id="color"),
        CodeExecutionTask(
            run_fn=lambda task: TextArtifact(f"{task.parent_outputs['color']} {task.parent_outputs['animal']}".upper()),
            parent_ids=["animal", "color"],
        ),
    ]
)

print(workflow.run().output.value)

Note that in the Workflow example we could still use task.parents_output_text if desired.

collindutter avatar Oct 07 '24 16:10 collindutter

I think the task.parents_output_text was added after this ticket was initially created. that works well for my needs. :) we can close this!

thanks :)

shhlife avatar Oct 07 '24 16:10 shhlife