# Guided JSON in vLLM: Fixing Invalid LLM JSON on DGX Spark (Nemotron Nano Omni Rerun)

> A DGX Spark rerun by Greg Alton: guided JSON in vLLM took invalid model replies from about 12% to 0% on a 1.6M-line log, plus the one regex the model suggested.

Level: Advanced
Tool: DGX Spark
Category: Building
Time: About 20 minutes
Checked: 2026-11-06
Tools: [vLLM structured outputs](https://docs.vllm.ai/en/latest/features/structured_outputs.html), [vLLM](https://docs.vllm.ai), [Nemotron 3 Nano Omni (NVFP4) on Hugging Face](https://huggingface.co/nvidia/Nemotron-3-Nano-Omni-30B-A3B-Reasoning-NVFP4), [NVIDIA DGX Spark playbooks](https://build.nvidia.com/spark)
HTML page: https://easyaihowtos.com/guides/guided-json-vllm-fix-invalid-llm-json-dgx-spark

AI engineering is just process engineering. *— [Greg Alton](https://www.linkedin.com/in/greg-alton-ios)*

My [last DGX Spark recipe](/guides/hybrid-log-watching-nemotron-nano-omni-dgx-spark) ended with three fixes and a promise to rerun the experiment. The model was fast, but 12% of its replies were broken JSON, and it mostly repeated the counter numbers back to me.

So I replayed a saved production-style SIP server log, 1.6 million lines, through two setups on my DGX Spark: the old prompt, and guided JSON with a better prompt. Same model, same 42 windows. I bought the Spark to learn by doing, and this run taught me something I didn't expect: fixing the format was easy, but the new prompt changed what the model thought its job was.

Prefer to watch? Catch the [quick video on YouTube](https://youtube.com/shorts/QdJtkAI1QCc).

## What you need

- A DGX Spark (or another GPU box) serving a model through vLLM's OpenAI-compatible API. I used vLLM 0.20.
- A saved log file you're allowed to read. Replaying a saved file means both setups see exactly the same lines.
- Python 3 with the requests package
- The counters and the three fixes from the earlier recipe, Hybrid log watching on a DGX Spark

## Steps

### 1. What I promised, and how I tested it

In the [hybrid log-watching recipe](/guides/hybrid-log-watching-nemotron-nano-omni-dgx-spark), the counters-plus-model pattern worked, but the model had two bad habits: **12% of its replies were invalid JSON**, and it mostly **repeated the counter numbers** back to me. I ended with three fixes and a promise to rerun it. This is the rerun.

- **The log:** a saved production-style SIP server log, **1,645,716 lines**. I replayed the saved file instead of streaming live, so both setups saw identical input. Raw lines never left the Spark.
- **The model:** Nemotron 3 Nano Omni (NVFP4), served by vLLM 0.20 on the Spark, with thinking off, temperature 0, and a 300-token cap.
- **The counters:** Fix 1, the level-aware regex counters, read every line.
- **Setup A:** the original prompt, with no structured output. It asks for `issues` and `health`.
- **Setup B:** guided JSON (Fix 2) plus the judgment prompt (Fix 3).
- **The windows:** both setups got the **same 42 windows** from the file, one check every 120 seconds of log time.

The October run is the baseline for context only. It was a live 15-minute stream with screenshots and a word-based error counter, so it isn't a perfect twin. The fair comparison is **A vs B**.

### 2. Fix 2 in practice: the guided JSON request

This is the request setup B sent. The `response_format` block is the whole trick: vLLM constrains decoding to the schema, so the model can't produce broken JSON.
```python
import requests

SCHEMA = {
    "type": "object",
    "properties": {
        "verdict": {"type": "string", "enum": ["ignore", "watch", "page"]},
        "likely_cause": {"type": "string"},
        "new_pattern": {"type": "string"},
        "next_check": {"type": "string"},
    },
    "required": ["verdict", "likely_cause", "new_pattern", "next_check"],
    "additionalProperties": False,
}

r = requests.post("http://localhost:8000/v1/chat/completions", json={
    "model": "your-model",
    "messages": [{"role": "user", "content": prompt}],
    "response_format": {
        "type": "json_schema",
        "json_schema": {"name": "verdict", "schema": SCHEMA, "strict": True},
    },
    "chat_template_kwargs": {"enable_thinking": False},
    "temperature": 0, "max_tokens": 300,
}, timeout=60)
```
I had a fallback ready in case the server rejected `json_schema`. It never needed it: all 42 setup B calls went through in schema mode.

### 3. Fix 3 in practice: the judgment prompt

The counter totals go in as context, followed by one plain instruction. Here's the prompt shape with the log sample removed:
```text
You are a secondary judge for a SIP proxy log watcher. Cheap regex counters
already counted every line. Here are their totals (context only):
{counter totals as JSON}

Do not repeat these numbers.
Trigger: periodic.
Using the log sample, answer with judgment the counters cannot give:
1) Is this new relative to normal traffic?
2) What is the likely cause (one client retrying vs many failing, etc.)?
3) What should a human check next?
4) Is there a pattern worth a new counter? If yes, put a concrete
   Python/PCRE-style regex in new_pattern; otherwise put an empty string.
Return ONLY JSON matching the schema.

--- LOG SAMPLE ---
{recent lines}
```
To score the echo problem, I counted a reply as an echo when most of the numbers in it (70% or more) were just the counter numbers repeated back.

### 4. The results

| Measure | October baseline | Setup A (old prompt) | Setup B (guided JSON + Fix 3) |
|---|---|---|---|
| Lines | about 292k (live) | 1,645,716 | 1,645,716 |
| Model calls | 85 | 42 | 42 |
| Invalid JSON | about 12% | 0% | 0% |
| Replies that echo the counters | frequent | 0% | 0% |
| Mean latency | 1.12 s | 0.57 s | 1.41 s |
| Latency range | 0.63–5.7 s | 0.51–1.43 s | 1.15–1.94 s |
| Regexes suggested | none | 0 | 1 |

**Invalid JSON went from about 12% to 0%,** and the echo went away. Setup B is about 0.8 seconds slower per call, because the replies are longer and the decoder follows the schema. That still sits inside the old 0.75–2.35 s trip-to-verdict range, so it's a fair price for never dropping an alert to broken JSON.

Same window, side by side:
```json
// Setup A
{"issues": [], "health": "ok"}

// Setup B
{"verdict": "watch",
 "likely_cause": "single client retrying after loop detection",
 "new_pattern": "",
 "next_check": "Check for repeated 482 Loop Detected responses from the same client and verify NAT/routing configuration causing circular path"}
```
Setup B is answering the question I actually asked: what's the likely cause, and what should a human check next?

### 5. The one regex it suggested: keep, with a caveat

I wanted the model to teach the cheap layer new tricks. In 42 calls, setup B suggested exactly one pattern, in one reply:
```text
.*Loop Detected.*
```
I scored it against the whole file:

| Check | Result |
|---|---|
| Lines matched | 47,139 of 1,645,716 (2.9%) |
| Sampled matches that were right | 50 of 50 |
| Scan time | 3.75 s for the whole file (about 2.3 µs a line) |

**Verdict: keep, with a caveat.** It found the right lines, but the `.*` on both ends adds nothing except work, because `re.search` already looks anywhere in the line. I'd promote the tighter form:
```python
LOOP = re.compile(r"482 Loop Detected")
```
At 2.9% of all lines, it's a good **counter** and a noisy **alert**. Count it, and alert on a jump, not on every hit.

Real logs don't come with an answer key, so "50 of 50" comes from labelling a sample of matches, not from ground truth. Treat it as an estimate.

### 6. What went wrong (and the fixes)

I promised honest numbers, so here are the parts that didn't go to plan.

- **No real alert ever fired.** The saved capture turned out to be almost all debug-level lines. The level-aware counters found 0 errors and 0 warnings in 1.65 million lines, so all 42 windows were routine periodic checks. **Fix:** pick a log that contains the failure you're trying to catch. I'll find a capture with a real error burst for the next run.
- **Every quiet window got "watch".** On the same calm windows, setup A said `ok` every time, and setup B said `watch` every time. The schema makes B pick a verdict, and it never chose `ignore`, even though that was allowed. A loop-detection retry looks interesting, so it kept flagging it. **Fix:** rename `ignore` to `ok` so it matches the model's own word, add "If nothing is new, answer ok" to the prompt, and add a quiet-window rule in code: if no counter tripped, log the verdict but don't escalate a `watch`.
- **The model copied client IPs into `next_check`.** It sometimes repeated addresses from the sample lines. **Fix:** scrub model output (IPs, phone numbers, user names) before you log it, share it, or send it to chat. It's a reply from your own model, but it's built from your raw data.
- **My level regex missed lines at first.** On the real lines, the text runs straight into the level tag with no space, so a pattern that expects a space before the level finds nothing. **Fix:** allow zero or more spaces:
  ```python
  import re

  LEVELS = r"DEBUG|INFO|NOTICE|WARN(?:ING)?|ERROR|CRIT(?:ICAL)?|FATAL"
  # \s* (not \s+) accepts the tag with or without a space in front of it.
  LEVEL = re.compile(rf"^\S+\s+\S+?\s*\[?({LEVELS})\]?(?=[\s:\]\-]|$)", re.I)
  ```
  Test it on a handful of your own lines before you trust it.
- **The echo test was easy this time.** With quiet windows and zero counts, there wasn't much to echo. I'm calling the echo fixed on this log, but not proven on a busy one.
- **It's slower.** About 0.8 s a call slower, as noted above. That's fine for a periodic judge, but don't put it in a hot loop.

### 7. The takeaway

**Guided JSON is the easy win.** One `response_format` block took invalid replies from about 12% to 0%, with no retries and no fallback needed. If you parse model output in code, turn it on.

**The prompt changes the job, not just the format.** The same model on the same windows said "ok" with the old prompt and "watch, here's why, check this next" with the new one. That's more useful, but it needs a way to say "nothing new" or it will cry wolf. The model also suggested one regex, and it was right. That's a small start on the goal of the model teaching the counters.

Next, I'll run it on a log with a real error burst and the `ok` verdict added. Follow the [DGX Spark section](/dgx-spark) for that one.

**Want to try this without buying a DGX Spark?** [GPUwerk.com](https://gpuwerk.com) rents dedicated DGX Spark time by the minute, and it looks like an inexpensive way to try before you buy. (GPUwerk is not a sponsor. Samuel from GPUwerk asked the question that started this rerun.)

## Tips

- Replay a saved log when comparing setups. Live traffic changes under you; a saved file gives every setup identical windows.
- Set `strict: true` and `additionalProperties: false` in the schema, and still keep a non-model fallback alert in case the model server is down.
- Drop the `.*` at the ends of a suggested regex. `re.search` already matches anywhere, so the wrappers only cost time.
- Scrub model output before it leaves the box. The model will happily quote addresses from the sample you gave it.
- Turn thinking off for a short JSON judge. You want a fast verdict, not a long reasoning trace.

## FAQ

### Does guided JSON make the model smarter?

No. It guarantees the shape of the reply, not the quality of the judgment. In this run, the shape was perfect every time, and the judgment needed a prompt fix: it said "watch" on every quiet window.

### Why did setup A also get 0% invalid JSON?

Setup A gave short, simple replies on quiet windows (`{"issues": [], "health": "ok"}`), which are hard to break. October's 12% came from a busier live stream with 12 counter trips and screenshot calls. Guided JSON is what makes 0% a guarantee instead of luck.

### Is 0.8 seconds extra a problem?

Not for this job. The counters raise the alert instantly; the model adds context a second or two later. Setup B's slowest call, 1.94 s, was still inside the original trip-to-verdict range.

### Should I ship the model's regex as-is?

No. Score it on your own log first: count the hits, check a sample by hand, and time the scan. Then tighten it. Here, `.*Loop Detected.*` became `482 Loop Detected`.

---

This guide is from Easy AI How-To (https://easyaihowtos.com). It is independent of the tools named above. Screens and free-plan limits change, so check the date before you rely on a button name.
