Request repair
Every provider stream passes through it: streaming.stream._metered calls request_repair.prepared before the provider and reads its events through request_repair.repairing.
| Hook | When | Answers |
|---|---|---|
before_sending(model, context) | Before every request | The context with earlier lessons applied |
after_refusal(model, context, refusal) | An attempt ended in an error before saying anything | The mended context, or None if unknown |
REPAIRS: prefill, then body_cap. The first mended request goes again, at most MAX_RESENDS (3) times.
| Stream rule | Detail |
|---|---|
| Events | Never held back; the consumer sees the reply begin once |
| Resend | Its first start is dropped; the refusal is not passed on |
After anything beyond start | Events pass through, nothing is resent |
| Cancel | RepairingStream.cancel_producer closes the current provider stream, with the same bounded wait |
| Refusal no repair knows | Goes on to retry and failover |
The two repairs
| Body cap | Prefill | |
|---|---|---|
| Refusal | Status 413, or body, request, payload or entity too large; not a context overflow (utils/failures/overflow.py) | "does not support assistant message prefill", "Requests ending with a model turn are not supported", "last message cannot be from the assistant" |
| Seen on | Anthropic's 32 MB request_too_large, relays | Anthropic models behind some gateways, Volcengine Agent Plan, Gemini on Vertex AI |
| Learns | The smallest size named below what was sent, else ¾ of what was sent | That this model refuses |
| Remembered per | Provider and base URL | Provider, API and model |
| Mends | Fits to ¾ of the cap: oldest history images replaced one by one with a note | Adds a user turn Continue. |
| Leaves alone | The latest user message's images, all text, a tool result's words | A conversation ending with a tool call |
| Gives up | Every earlier image out and still too big: refusal goes on | – |
The note: [An earlier image was left out: the request was over the provider's size limit.] The ¾ margin lets a conversation grow a few turns before more images go, so its prompt cache is not broken every turn.
Files
| File | Holds |
|---|---|
models/providers/utils/failures/request_repair.py | prepared, repairing, order |
models/providers/utils/failures/body_cap.py | Size refusals, caps, fitting |
models/providers/utils/failures/prefill.py | Prefill refusals, per-model memory |
models/providers/streaming/stream.py | _metered |
Both memories last for the process; tests/conftest.py clears them after each test. The repairs follow magpie's body cap and prefill memory (yetone/magpie, MIT).
Last updated · 2026-11-25