Retries and errors
Model calls fail in different ways, and each needs its own answer. Foundation Models reports them as errors from the session, among them exceeding the context window and a guardrail violation, when the system's safety checks block a prompt or a reply.
- A reply that fails validation is worth another try, with the problem stated in the next prompt.
- A guardrail violation will usually happen again for the same prompt: stop and tell the user.
- An overflowing context needs a shorter transcript, not the same call again.
class GuardrailViolation(Exception):
pass
try:
raise GuardrailViolation("blocked")
except GuardrailViolation as error:
print("stop:", error)Catching every exception in one place and retrying turns a blocked prompt into three blocked prompts, and hides why the feature failed.
Your task
Write ask(model, prompt, validate, attempts=3). Call model(prompt) and return {"ok": True, "value": validate(reply), "calls": n}. When validate raises ValueError, call again with prompt + "\n\nYour last reply was invalid: " + str(error), at most attempts calls, then return {"ok": False, "error": "invalid", "calls": n}. Return "guardrail" or "too long" as the error at once for GuardrailViolation or ContextTooLong.
The explanation opens here once every check passes.