A contract goes through three steps, and the last one can fail, so this chapter answers: how do we keep what one step made when the next one fails?
Follow the arrows: each step hands its result to the next one.
Drawing diagram…
Tool ResultThree steps, and what each one hands on
The steps here are stand-ins that call no AI, so every run is free. On the real desk, the review is the step we pay for.
Set up#
If you closed the terminal, go back into the folder and switch the environment on, then install CrewAI:
1 $ cd document-desk
2 $ source env/bin/activate
3 ( env ) $ pip install crewai == 1.15 .45
On Windows, the second line is env\Scripts\activate.
Way 1: call each step yourself#
Your code calls the steps one after another, so an error in the last step ends the whole program.
Drawing diagram…
Tool ResultPlain calls: one error ends everything
Let's save this as demo.py:
1 # demo.py
2 def read_document ( ) :
3 return "# Software subscription agreement"
4
5
6 def review_contract ( text ) :
7 print ( "review finished" )
8 return "risk summary of: " + text
9
10
11 def check_summary ( summary ) :
12 raise ConnectionError ( "no internet" )
13
14
15 text = read_document ( )
16 summary = review_contract ( text )
17 checked = check_summary ( summary )
review finished Traceback (most recent call last): ... ConnectionError: no internet
The review finished, but the error ended the program, and the summary went with it.
Way 2: let a flow call each step#
A CrewAI flow calls every step for you, and keeps each result as it goes. Each step gets a note : a line starting with @, just above it.
@start() : marks the first step.
@listen(step) : runs this step with the result of step.
kickoff() : starts the flow.
method_outputs : the list of results the flow has kept so far.
Drawing diagram…
Input Tool ResultA flow: the library calls each step and keeps its result
Change demo.py to this:
1 # demo.py
2 from crewai . flow . flow import Flow , listen , start
3
4
5 class Desk ( Flow ) :
6 suppress_flow_events = True
7
8 @start ( )
9 def read_document ( self ) :
10 return "# Software subscription agreement"
11
12 @listen ( read_document )
13 def review_contract ( self , text ) :
14 return "risk summary of: " + text
15
16 @listen ( review_contract )
17 def check_summary ( self , summary ) :
18 raise ConnectionError ( "no internet" )
19
20
21 desk = Desk ( )
22 try :
23 desk . kickoff ( )
24 except ConnectionError as error :
25 print ( "stopped:" , error )
26 print ( desk . method_outputs )
A class groups the steps, and (Flow) gives ours everything a CrewAI flow has. Each step takes self, the desk itself, which Python passes in for us. suppress_flow_events = True hides CrewAI's progress boxes.
Error executing listener check_summary: no internet stopped: no internet ['# Software subscription agreement', 'risk summary of: # Software subscription agreement']
The first line is CrewAI reporting the error, and the second is our except. The check still failed, but the flow had already kept the text and the summary.
Note
method_outputs lives only while the program runs. A later chapter saves it, so a new run can skip what already finished.
The notes decide the order#
Each step's note names the step it waits for, so the order in the file doesn't matter.
Drawing diagram…
Input ToolThe notes, not the file, set the order
Let's write the steps from last to first, and let the check work. Change demo.py to this:
1 # demo.py
2 from crewai . flow . flow import Flow , listen , start
3
4
5 class Desk ( Flow ) :
6 suppress_flow_events = True
7
8 @listen ( "review_contract" )
9 def check_summary ( self , summary ) :
10 return "checked " + summary
11
12 @listen ( "read_document" )
13 def review_contract ( self , text ) :
14 return "risk summary of: " + text
15
16 @start ( )
17 def read_document ( self ) :
18 return "# Software subscription agreement"
19
20
21 print ( Desk ( ) . kickoff ( ) )
The names are in quotes because Python hasn't read those steps yet.
checked risk summary of: # Software subscription agreement
read_document ran first though it's written last, and kickoff() gave back the last step's result.
Calling the next step yourself#
It can look natural for the review to call the check. Change review_contract in demo.py :
1 @listen ( "read_document" )
2 def review_contract ( self , text ) :
3 return self . check_summary ( "risk summary of: " + text )
checked checked risk summary of: # Software subscription agreement
The check ran twice. Follow the two arrows into it:
Drawing diagram…
Input ToolTwo calls reach the check
Tip
Let each step only return its result, and let the flow call the next one.
Let's remove the program. CrewAI stays installed for the next chapter.
The two ways side by side#
Compare what is left when the check fails:
Drawing diagram…
Input Tool ResultWhat is left after the check fails
In a flow, each step says what it waits for, and the flow makes every call.
To choose, answer the questions from the top:
Drawing diagram…
Input ResultPlain calls or a flow?
What comes next#
The real check must compare the summary with the contract, but only the summary reaches it:
Drawing diagram…
ToolThe check needs the text, but gets only the summary
So where can a later step find what an earlier step made?