Skip to content
/

Paths that meet

PathsChapter 6 of 154 min read

The router sends each document down its own path, and every document still needs a report, so this chapter answers: how do the three paths meet again?


Each path hands its result to one step at the end:

Drawing diagram…
ToolResultThree paths, one report
  • Join: the step where paths meet again.
  • or_(): runs a step once, when the first of the steps it lists has finished.

Set up#

If you closed the terminal, go back into the folder and switch the environment on:

Terminal
$ cd document-desk
$ source env/bin/activate

Way 1: the report listens to one path#

write_report takes the result of the step it listens to, as review_contract took the text in chapter 2. Let's save this as demo.py:

demo.py
# demo.py
from crewai.flow.flow import Flow, listen, router, start
from pydantic import BaseModel, ConfigDict


class DeskState(BaseModel):
    model_config = ConfigDict(validate_assignment=True)

    kind: str = ""


class Desk(Flow[DeskState]):
    suppress_flow_events = True

    @start()
    def read_document(self):
        return "read the " + self.state.kind

    @router(read_document)
    def sort_document(self):
        if self.state.kind in ["contract", "invoice"]:
            return self.state.kind
        return "other"

    @listen("contract")
    def review_contract(self):
        return "reviewed the contract"

    @listen("invoice")
    def check_invoice(self):
        return "checked the invoice"

    @listen("other")
    def hand_to_person(self):
        return "handed it to a person"

    @listen(review_contract)
    def write_report(self, result):
        return "report: " + result


documents = ["contract", "invoice", "other"]
for kind in documents:
    desk = Desk()
    print(desk.kickoff(inputs={"kind": kind}))
Terminal
(env)$ python demo.py
Output
report: reviewed the contractchecked the invoicehanded it to a person

Only the contract got a report. The other two lines are what their paths returned, the last step's result:

Drawing diagram…
ToolResultTwo paths end without a report

A copy of write_report for each path would work, but then a change to the report means three changes.

Way 2: wait for any one path#

or_() lists the three paths, and write_report runs when one of them finishes. Add or_ to the import in demo.py:

Python
from crewai.flow.flow import Flow, listen, or_, router, start

Change write_report in demo.py:

Python
    @listen(or_(review_contract, check_invoice, hand_to_person))
    def write_report(self, result):
        return "report: " + result
Terminal
(env)$ python demo.py
Output
report: reviewed the contractreport: checked the invoicereport: handed it to a person

Every document got a report from the same step, and result held whatever its own path returned. The router lets only one path run, so exactly one of the three ever finishes:

Drawing diagram…
InputToolThe invoice reaches the report

When two of them finish#

or_() doesn't wait for a second step. Let's make the report wait for the start and for the review, which both finish for a contract. It prints, so we see each time it runs. Change write_report in demo.py:

Python
    @listen(or_(read_document, review_contract))
    def write_report(self, result):
        print("report:", result)

Run it for the contract only. Change documents in demo.py:

Python
documents = ["contract"]
Terminal
(env)$ python demo.py
Output
report: read the contractreviewed the contract

write_report ran once, for read_document, the first to finish. When review_contract finished, no second report came, and kickoff() gave back the review, the last step's result:

Drawing diagram…
InputToolor_() runs once, for the first step to finish

Let's remove the program. The folder stays for the next chapter.

Terminal
(env)$ rm demo.py

The two ways side by side#

Compare which documents reach the report:

Drawing diagram…
InputResultWhich documents get a report

Join the paths of a router with or_(), so one step finishes every document.

Drawing diagram…
InputResultIs or_() the right join?

What comes next#

Some steps need two results, not the first of two:

Drawing diagram…
ToolResultA step that needs two results

So how does a step wait for both?