Skip to content
/

Pick a path

PathsChapter 5 of 154 min read

So far, every document goes through the same steps, in one line, so this chapter answers: how does each document take its own path?


One step reads what the document is, and that word picks the steps that come next:

Drawing diagram…
InputToolEach kind of document, its own path
  • Router: a step whose result picks which steps run next.
  • Label: the word a router returns, such as "contract".

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: every step checks for itself#

Each step runs for every document, and does its work only when the kind is its own:

Drawing diagram…
InputToolEvery step runs, and each one checks the kind

Let's save this as demo.py:

demo.py
# demo.py
from crewai.flow.flow import Flow, listen, 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

    @listen(read_document)
    def review_contract(self):
        if self.state.kind == "contract":
            return "reviewed the contract"

    @listen(review_contract)
    def check_invoice(self):
        if self.state.kind == "invoice":
            return "checked the invoice"


desk = Desk()
desk.kickoff(inputs={"kind": "invoice"})
print(desk.method_outputs)

kickoff(inputs={"kind": "invoice"}) puts "invoice" in the state's kind field before the first step runs.

Terminal
(env)$ python demo.py
Output
['read the invoice', None, 'checked the invoice']

The invoice went through the contract's review, which gave back None. Each new kind of document would add one more check to every step.

Way 2: a router picks the path#

@router(read_document) runs sort_document after read_document. The word it returns is a label, and only the steps with @listen on that label run. The label other is for everything else. Change demo.py to this:

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):
        return self.state.kind

    @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"


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

Each document ran one step after the router, and kickoff() gave back that step's result. Follow the invoice:

Drawing diagram…
InputToolHow the label "invoice" finds its step

No step is called contract, so @listen("contract") waits for a router to return that label.

A label nobody listens for#

An AI may answer Contract, with a capital letter. Change documents in demo.py:

Python
documents = ["contract", "Contract"]
Terminal
(env)$ python demo.py
Output
reviewed the contractContract

No step listens for Contract, so nothing ran after the router, and kickoff() gave back the label itself, with no error:

Drawing diagram…
InputToolResultA label with no step

Let the router send every word it doesn't know to the safest path, as the else in chapter 1 did. Change sort_document in demo.py:

Python
    @router(read_document)
    def sort_document(self):
        if self.state.kind in ["contract", "invoice"]:
            return self.state.kind
        return "other"
Terminal
(env)$ python demo.py
Output
reviewed the contracthanded it to a person

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

Terminal
(env)$ rm demo.py

The two ways side by side#

Compare what runs for one invoice:

Drawing diagram…
InputResultWhat runs for one invoice

Let one router choose the path, and let each step do only its own work.

Drawing diagram…
InputResultA router, or one line of steps?

LangGraph, the library named in chapter 3, calls a choice like this a conditional edge.

What comes next#

Each path now ends in its own step, but every document still needs a report at the end:

Drawing diagram…
ToolResultThree paths, one report

So how do the three paths meet again?