Try this first
In Lesson 1.6 you asked Rover about a file that does not exist, and the program crashed with
a traceback.
Look at that crash again. Your agent died because a file was missing. A human assistant would
have said “there is no such file” and carried on.
Errors go back to the model
An exception in a tool is not a program failure. It is information — and the model can act on
it, if you tell it.
def run_tool(name, tool_input):
try:
if name == "read_file":
return read_file(tool_input["path"]), False
return f"No tool named {name}.", True
except FileNotFoundError:
return f"There is no file called {tool_input['path']} in this folder.", True
except Exception as e:
return f"The tool failed: {e}", True
Then set the flag on the result:
output, failed = run_tool(call.name, call.arguments)
results.append({
"id": call.id,
"content": output,
"is_error": failed,
})
is_error: True marks the result as a failure. The model then knows this was not a normal
answer, and typically tries something else — listing the folder, or asking you which file you
meant.
Do not swallow the failure
There is a wrong version of this that looks reasonable:
except Exception:
return "", False # never do this
An empty string with no error flag tells the model the tool ran and found nothing. It will
happily report that the file is empty. You have converted a crash into a confident lie, which
is strictly worse.
Word the error for the reader
Compare these three, for the same missing file:
| Message | What the model can do with it |
|---|---|
Traceback (most recent call last): ... |
Very little. Noise |
FileNotFoundError |
Knows it failed, not why or what next |
No file called notes.txt in /home/priya/rover. Files here: agent.py, summary.txt |
Can pick the right file and retry immediately |
The third one costs you three lines and often saves a whole turn.
Two rules for writing them:
Say what was wrong and what exists. “Not found” plus the available options beats “not
found” alone almost every time.
Never put a raw traceback in a tool result. It is long, it is mostly your source, and it
gets resent on every later turn.
An error is a result, not a crash. Say what went wrong, and say what could work instead.
Try this before the next lesson
Add error handling to Rover, then ask it about a file that does not exist.
Watch what it does next. In most runs it will list the folder and find the file you actually
meant — without being told to. That recovery is not the model being clever. It is your error
message being useful.