Lesson 03.05 · Phase 03 · The Agent Loop

Handling malformed actions

Repair what can't change the meaning. Reject the rest with an error that says exactly what to send instead.

Learning objectives

  • Classify the ways a model's tool call can be malformed: broken JSON, wrong shape, missing or extra arguments, wrong types, bad enum values, unknown tools, and calls written as text.
  • Implement a repair layer that fixes only lossless problems (code fences, trailing text, trailing commas, Python-style quotes, numeric strings) and never runs model output as code.
  • Validate arguments against the tool's JSON Schema, and turn every rejection into an error message the model can act on.
  • Measure what a real model actually sends, instead of guessing.

Lesson 1 treated every tool failure as an observation, which keeps the loop alive. But "error: invalid arguments" is a thin observation. Which argument? What should it look like? And some failures shouldn't be failures at all: {"topic": "K2 height"} wrapped in a Markdown code fence is a perfectly clear request.

Loading the full lesson…