ModelRepromptRepair

Stock self-healing repair: re-prompt the model with the tool's JSON schema + the failed args + the parse error, asking for corrected JSON. If the second response parses cleanly, return a ContentPart.ToolCall with the corrected input; otherwise return null (the agent loop surfaces StreamEvent.ToolError).

On-device targets like Gemma 4 E2B hallucinate args ~5% of the time; a single re-prompt typically recovers, and is cheap compared to a full user-visible failure. Wire via ToolLoopAgent.experimental_repairToolCall.

Generic over TContext — application code can use this directly regardless of its context type. The function does not look at the model's per-call options, only at the tool's published schema and the conversation history, so no context-specific behavior is needed.

Bounded recursion: this builder makes ONE repair attempt per failed call. The loop never re-enters repair on the corrected call.

Parameters

model

the LanguageModel to re-prompt. Typically the raw (un-middlewared) model so the repair turn doesn't show up in tool-call logs or other middleware side-channels. Pass a wrapped model if you want repair attempts instrumented the same way as main-flow calls.