In this video, you gave a bad answer one more try, with the error in the request, and sent every note the intake script can’t book to a person on the front desk’s list. One function decides who goes on that list.
Key Points
- Nowhere to go. Before this lesson, the script held back a note whose answer failed the Zod schema and flagged a record the clinic can’t book, such as Noodle’s and Rolo’s. It printed them, and that’s all. Nobody at the clinic read them, and the one-word “help” note, which isn’t a booking, was printed with the others.
- One retry for a bad answer. An answer can still fail the schema even though the provider gets one. As you saw in the last lesson, the request gives the model the pet name’s 40-character limit only as a hint in a description, and only Zod enforces it. The same request can bring back a different answer, so a second try can pass, and saying what was wrong gives the model something specific to fix.
- Which answers get it. Only
"invalid", an answer that fails the schema, and"not JSON"get a retry. The request goes in a constant, so the retry can send it again. For those two failures, the script prints a retry line, builds a request with the error, and asks once more with{ ...request, ...retry }. - The retry replaces two keys. The keys of
retrycome later in the spread, so itsinstructionsandinputreplace the first ones, whileschemaandrolestay the same. The second result takes the place of the first, soresultis now alet, and its cost adds to the total. - One retry at most. There’s no loop around the retry, so a note can’t get a second one.
- The retry carries the failed answer. Each call to the model starts from nothing, so the retry has to carry the failed answer itself. In
model/index.js, the"not JSON"and"invalid"failures now return the answer’stexttoo. - What the retry sends.
withError(), in the new fileretry-prompt.js, adds three lines to the instructions saying the last answer failed. The input keeps the note, and below it go the failed answer and the errors, each in its own tags. Each Zod issue becomes one line, the key and then the message. - The retry for one record. No answer failed in the runs for this video. Here’s the end of what
withError()builds for the record you typed by hand in the last lesson, which no model sent. The errors name the pet’s name and the date:
<errors>
invalid
petName: Too big: expected string to have <=40 characters
preferredDate: Invalid ISO date
</errors>
Sent as a test, that retry came back valid all three times. In the first test, the model shortened the name to 'Pepper and Salt' and wrote the date as '2026-12-03'.