@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
1# demo.py2from crewai.flow.flow import Flow, listen, router, start
3from pydantic import BaseModel, ConfigDict
456classDeskState(BaseModel):7 model_config = ConfigDict(validate_assignment=True)89 kind:str=""101112classDesk(Flow[DeskState]):13 suppress_flow_events =True1415@start()16defread_document(self):17return"read the "+ self.state.kind
1819@router(read_document)20defsort_document(self):21return self.state.kind
2223@listen("contract")24defreview_contract(self):25return"reviewed the contract"2627@listen("invoice")28defcheck_invoice(self):29return"checked the invoice"3031@listen("other")32defhand_to_person(self):33return"handed it to a person"343536documents =["contract","invoice","other"]37for kind in documents:38 desk = Desk()39print(desk.kickoff(inputs={"kind": kind}))
Terminal
1(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.