Files
sales-trainer/docs/engineering-log/2026-10-02-enable-thinking-inline-root-cause.md
Macky 0f34af71d1 fix(llm): always send enable_thinking=true — hidden reasoning was written inline into content
The llama.cpp endpoint writes the model's hidden reasoning INLINE into
content unless enable_thinking=true is sent (which relocates it to the
separate reasoning_content field). The app sent the field absent, so:
- complete_json got corrupted JSON -> live 500 'Unterminated string char 143'
- persona JSON generation failed (same root cause, earlier incident)
- persona replies were bloated with the reasoning chain

Send the flag on all three call paths (complete/complete_json/
complete_conversation). thinking param retained for API compat, now a
no-op on this provider (always separated — the only correct mode here).

- tests/test_llm_enable_thinking.py pins the flag on all three paths
- full suite: 543 passed; independent reviewer PASS (22 focused passed,
  single OpenAI client in the app, both fixed sites confirmed, no stale
  tests)
- cleanup stale comments that encoded the backwards understanding
  (endpoint 'ignores enable_thinking' — it honors it; that misconception
  is what caused this incident)
2026-10-02 20:09:31 +07:00

2.1 KiB
Raw Permalink Blame History

2026-10-02 — 500 "invalid JSON: Unterminated string": hidden reasoning inline in content

Symptom

After the budget fix (c5553eb): live POST /api/chat/.../send → 500 LLM returned invalid JSON: Unterminated string starting at: line 5 column 10 (char 143). The budget-raise was whack-a-mole: bigger budget just moves the failure from content='' to truncated JSON.

Diagnosis (live probes vs pchome.moreminimore.com/v1, model mia-qwen38-3.0bpw)

The llama.cpp endpoint has a hidden-thinking field, and the app must tell it to use it:

enable_thinking sent content reasoning location
absent (what app sent: extra_body=None) 17×24=...408 {"result":408} inline in content (+ reasoning_content field)
false inline in content inline
true {"result":408} — clean reasoning_content field

Consequences of the inline form (what the app had been living with):

  • complete_json last-ditch regex grabs a corrupted span → Unterminated string → 500 (this incident)
  • persona_generator (persona JSON) → persona generation fails (the "สร้าง persona ไม่ได้" incident, same root cause)
  • persona reply (chat) → reply text bloated with reasoning chain (the 21562-byte 200)

Fix (this commit)

llm.py: complete(), complete_json() (via complete), and complete_conversation() always send extra_body={"enable_thinking": True} — relocates hidden reasoning into reasoning_content, content stays clean. The thinking param is retained for API compatibility but is a no-op on this provider (always separated). tests/test_llm_enable_thinking.py pins the flag on all three call paths.

Why not just a bigger budget

Budget raises only delay the corruption (empty → truncated → eventually clean); the inline reasoning format is the actual bug. Separation fixes the judge 500, persona JSON, and persona reply text at once.

Server-side (speed, optional — owner)

--reasoning-format none (or no-think template) still removes the reasoning compute entirely → latency goes back to ~5s. Not required for correctness.