Skip to slide
Chapter 13 · Reliability: Retries, Idempotency, and Failure Handling
121 / 191

CHAPTER 13 · Reliability: Retries, Idempotency, and Failure Handling · 2 / 12

Defensive parsing of model output

The model is a probabilistic component; its output will sometimes be malformed. Guard every place you consume it:

  • Tool arguments: parse defensively; if the JSON is malformed, default to empty arguments rather than throwing, and let the tool report a useful error.
  • Unknown tool names: if the model calls a tool you don't have, return a "that tool isn't available" result so the model can recover, rather than erroring out.
  • Emitted protocols: if the citation/structured block is missing or malformed, proceed without it; don't make a missing trailer fatal.
  • Missing tool results: guarantee that every tool call the model made gets some result back (a real one or a synthesized error). Providers require this pairing; a missing result breaks the next call (Chapter 2).

The principle: the model's mistakes should become recoverable signals, not crashes. Often the best recovery is to feed the error back to the model as a tool result; it will frequently adjust and try again.

← → arrow keys work too