DEV Community

RobertJJJ
RobertJJJ

Posted on AI-assisted

Fix the generator or repair the output: when a downstream repair is even well-defined

A public bug report about streaming tool calls made me re-examine a repair layer I maintain, and the useful part was not the fix - it was deciding which fix is even the right one.

The report: a third-party gateway streamed two parallel bash calls with the same tool_call index (distinct ids). The adapter keys tool blocks by index first, so the second call's argument text was appended to the first, producing two complete JSON objects back to back. The merged string fails to parse; the turn dies with a malformed-response error. Their measurement: roughly a third of requests collapse at max reasoning effort, and retrying the identical body recovered the handful they tried.

Two fixes were proposed: prefer the call id over the index when keying blocks, and make the malformed-response error retryable by default. Both are reasonable, and they do not do the same thing. The first removes the merge at its source. The second lowers how often a turn dies but cannot reach zero if the collapse is intermittent - which their own retry numbers suggest.

I reproduced the downstream half only. I did not drive their gateway. What I could build was the artifact the adapter hands over: one fused argument string of 181 characters, which does not parse whole, but splits at boundary roots into two calls whose arguments each parse and whose ids are distinct. That is a shape reproduction, not a reproduction of their failure rate, and not on their platform.

This is where I think the second-order lesson is, and it is the part I would push back on anyone copying from the thread: a downstream repair is only well-defined if the fuse is a concatenation. If the upstream ever interleaves fragments between the two calls, no boundary split can be correct - the right answer is refusal, not a guess. So "repair downstream" is not a weaker version of "fix upstream"; it is a different claim that only holds under a condition nobody in the thread has measured yet.

The practical rule I took away: before choosing between fixing the generator and repairing the output, measure whether the corruption preserves structure. Concatenation preserves it; interleaving does not. If you cannot tell which one you have, the downstream tool must refuse rather than silently "fix" a guess.

I would rather report that limit than demonstrate a repair that only works in the easy case.

Disclosure: AI tools helped draft and structure this post. I reviewed the wording, the numbers, and the boundaries before publishing.

Top comments (3)

Collapse
 
userxnom profile image
Abdullah Naseer •

This is the sharpest framing of the repair-layer problem I have read in a while. Most people ask "can I patch this?" and stop there; you ask whether the repair is even well-defined, and that changes the whole decision. Your rule — if the malformed output could have arrived by more than one path, you cannot repair it, only reject it — is the part I will keep.

The index-versus-id detail is a good example of why: keying by index first looks harmless until a gateway streams two parallel calls with the same index and distinct ids, and then the second call silently overwrites the first. Preferring the call id when it is present is the right fix, and adding an explicit malformed-input rule instead of silently discarding it makes the failure visible instead of hidden.

What would you do when the gateway does not send a usable id at all — is that a case for rejecting the whole batch, or for falling back to index keying with a warning attached?

Collapse
 
irck_ysia_d79705b3a189af9 profile image
Irck Ysia •

Really insightful #productivity post on "Fix the generator or repair the output: when a downstream repair is ev"! Love seeing practical workflows that cut friction—I've been automating static page builds and IndexNow pings in vanilla Node.js this weekend myself. Dropping a heart!

Collapse
 
jjjjjjjjnnjnn profile image
RobertJJJ •

Depends on which chunk is missing the id.

Continuation deltas without an id are normal—only the first chunk carries it, so keying by index and appending to the active block there works fine.

What breaks is when an opening chunk has no id, and another opening chunk lands on the same index also without one. If you fall back to index there, you end up silently concatenating two separate calls. In that case I'd just fail the turn with a collision error and preserve the raw text. Merging with a warning is worse—you still get mangled tool arguments, just with a log line attached, whereas a hard error can at least be retried.

The other option is buffering chunks until the final chunk in case the id arrives late, though that obviously defeats part of the point of streaming.

Full disclosure: I haven't verified whether any real gateways actually omit opening IDs or only send them at the end. My repro had IDs on both calls, so I'm going off the spec here rather than something I've hit locally.