My script published a half-written post because I never checked whether the model had finished
I wrote a small script that asks a language model for a blog post, takes what comes back, and publishes it. Recently it published a post that stopped partway through a word. The script said the run had succeeded, and that was true by every check I had written. The bug was the check I left out. This post covers what I got wrong, what the code does now, and a checklist I’ll use the next time I write or review code like this.
What the script does
The script sends one chat-completion request through an API gateway. The request offers the model one tool, and that tool’s arguments are the post itself: a title, a Markdown body and a one-line description. When the reply comes back, the script parses the tool call, writes the page, archives the input next to it and commits both.
What I checked, and what I didn’t
The reply handling made sure that:
- the HTTP status wasn’t an error,
- the body was valid JSON,
- the JSON didn’t contain an
errorobject, - there was at least one choice,
- the first choice held a call to the expected tool,
- the tool call’s arguments parsed into a JSON object, and
- the title and body weren’t empty.
If all of that held, the post went out.
None of it read the choice’s finish_reason. Without ever deciding to, I had treated “the request was served” plus “the reply looks like a tool call with a title and body” as proof that the model finished writing the post. Those are different things.
What happened
A run against a reasoning model finished with no errors. It wrote a post, archived the input and committed both. The post read fine for about two thirds of its length, then cut off at the start of a sentence, right after a paragraph that was clearly leading into the next one. Every earlier run had produced a complete post, so the commit was pushed before anyone read the file to the end. A later manual commit removed the page and its archived input.
The gateway’s log showed the finish reason for that request: content_filter, not tool_calls. The provider’s filter had stopped the output partway through the tool call’s arguments. The gateway still returned HTTP 200 with whatever had been produced, and it closed off the arguments’ JSON where the cut happened. So the JSON parsed, the title was there, and the body wasn’t empty. Every check passed. The one thing wrong with the reply was the one thing I never looked at.
The cost was a broken page live on the site, the manual cleanup, and paying for a request whose output we threw away. The same code would have done the same thing with a reply cut off at the output limit (length, which is the more common case) or one where the provider failed partway through (error). A longer input or a model with a smaller output limit would have hit this sooner or later.
What I should have written
An HTTP 200 tells you the request was served. A parsed body tells you the reply has the expected shape. Neither tells you that you got everything the model produced. Completion APIs report that in one place, the finish reason. Because gateways and SDKs often close off the JSON of a partial tool call so it still parses, a cut-off reply can look exactly like a short complete one. Shape checks are necessary, but they aren’t enough.
The fix is to read the finish reason before touching anything in the message, and to accept the reply only when that reason means the model finished:
FINISHED = {"tool_calls", "stop"} # "stop": some providers report it for a forced tool call
choice = reply["choices"][0]
reason = choice.get("finish_reason")
if reason not in FINISHED:
native = choice.get("native_finish_reason")
detail = f" (provider reported: {native})" if native and native != reason else ""
raise IncompleteReply(f"model did not finish: {reason or 'no finish reason'}{detail}")
# Only now look at the tool call, its arguments, the title and the body.
Three things about this matter:
- It’s an allowlist. The code lists the reasons that mean “finished” and rejects everything else. A reason it has never seen fails. My first instinct was to block
lengthandcontent_filter, and that would have let the next unfamiliar value through. - A missing reason fails. The API doesn’t document a reply with no finish reason, so I don’t trust one.
- A failure writes nothing. No page, no archive move, no commit. The error message includes the reason, plus the provider’s own wording when it differs, so whoever reads it can tell what to do:
content_filtermeans look at the input, andlengthmeans look at the output limit.
That change is now in place. It has tests for content_filter, length, error and a missing reason, plus one confirming that a tool call reported under stop is still accepted.
How I’ll do better
My mistake was treating “well-formed” as if it meant “complete.” Next time I write code that reads a model’s reply, the finish reason is the first thing it checks, before any validation of the content. Here’s the checklist I’ll use when I write or review this kind of code, whether it goes through a gateway, a vendor SDK or a raw HTTP call:
- Find where the finish reason is read. It might be called
finish_reason,stop_reason,native_finish_reasonor something SDK-specific. If the code never reads it, the code can’t tell a cut-off reply from a complete one. - Check that the accepted reasons are an allowlist of values that mean “the model finished” (
tool_calls,stop,end_turnand their vendor spellings), not a blocklist of failure values. - Check that a missing reason fails.
- Check that the finish reason is checked before the content or tool arguments are used, and that a failed check writes, sends and commits nothing.
- For streamed replies, check the finish reason on the final chunk. A stream that ended didn’t necessarily finish.
- Check that the error message names the reason, so someone reading it can tell a filter stop from a length limit.
A cut-off reply looks a lot like a complete one, and the finish reason is the only reliable way to tell them apart, so the code has to check it.