- [Instructor] Our agent is fully functional,
and that's fantastic.
But what happens if an external API is down,
or our network connection fails, or other issues like that?
Right now, there's a risk that our agent would crash.
So one of the things I'd like to do
is to look at how to make the agent more robust
by adding error handling.
And we can do this manually or we can use an AI assistant
like the Gemini CLI to do most of that heavy lifting for us.
The goal is to take all of the code
in our hiking_agent directory
and make it more resilient to errors.
If we can wrap the external calls
to the location, weather, and parks APIs,
and try...except blocks,
then this would allow our agent to fail gracefully
with a clear message instead of crashing.
Indeed, we saw some of the code for this
a little bit earlier on,
but I want to show what it would be like with the initial code
that many of us might build
that might be a little bit more naive.
For example, let's start with the parks.py.
And it's making these URL requests
that are common areas of failure.
So for example, something like this one,
the common pattern is to define the URL,
to call requests to get the response,
and then to return the response.json.
Now, there are a number of areas where this could go wrong,
and naively, we could say, to fix it,
we could manually add a try...except block,
but there may be corner cases that we don't see.
So what if we ask an AI assistant to do this work for us?
So I'm going to do that by starting Gemini.
You can use any AI assistant you like of course,
but I like using the Gemini CLI
because I'm in the same directory as the code
and I can work directly within the code itself.
So I can specifically say something like,
in my parks.py file,
please update the get_parks function
to handle potential network errors.
It should use a try...except block.
Check for bad HTTP responses,
network failures,
or any other issues you can think of
and return None
when an error occurs.
This is one of the ways that your skills as a developer
are actually more useful, I think,
in the age of AI because you can be much more specific
with your prompt here by the kind of things
that you're experienced, like HTTP errors,
bad H responses, network failures,
all of those kind of things.
And I just did get something like this
and have it kind of generate the code for me.
It's the first time I'm running it,
so sometimes it's going to ask me for permission
to be able to write and stuff like that.
And we can see the code
that Gemini came up with here for me.
It's using this raise_for_status,
which can check for HTTP errors
and do other goodies like this as well.
So it actually also spotted this one where I could have had,
if I look at it here briefly before I continue,
that there are corner cases
where if there's a null in the data, that it would crash.
And the if data in parks_data would actually crash
rather than failing
and gracefully allowing the agent to continue,
there'll be an actual crash here.
So we're saying like, you know, it's going to update that.
And the agent being the agent, the Gemini one,
is it's going to actually run it
and just check if it's working
and do other things and fixes for us like that.
I find that's really, really useful,
and I think it's one of those things
that would be very important for you
to be able to check errors in your files like this.
In this case, actually, I didn't run it
in a virtual environment,
so it found that that package was missing.
And with that package missing, it wants to install that,
and then retest the file with the package there.
And we can see it actually ran parks.py
and was able to find some trails for us,
so we know that it's working now.
The changes that it did to add error handling
have not changed anything.
And I'm just allowing it to remove any files that it added.
While it was working through this,
sometimes it writes temporary files and stuff like that.
I'm allowing it to remove them.
So this is a good example of being able to use
a AI agent or an AI model.
I love to do it as a command-line interface, personally,
so it save a lot of cutting and pasting when I'm working
and stuff like this to update and improve your code.
And we can see the updated and improved code here.
So as you can see,
the idea here is you can write the code yourself,
you can create code for building an agent like this one
using your own understanding,
but there are always corner cases that you may not see,
and I find that using an AI assistant
can dramatically speed up the process
of that polishing your code and hardening your code
and finding those corner cases.
I gave a quick example of a clear semantic prompt,
and I would encourage you to think about that,
think about the code that you're doing,
and provide those prompts that will allow you
to delegate the important and repetitive tasks
of doing things like adding error handling.
I've provided in the download finished versions of the code
that I worked on that I find is more robust
than I built it in that way.
And in the next video, I'm going to use the same technique
to add documentation to the code.