3.4 KiB
LLM JSON Retry Fix Design
Context
Production video #213 failed while generating the first of nine chapter articles. The LLM response could not be parsed as JSON and produced Expecting ',' delimiter at line 11, column 17.
The worker already has a three-attempt quality rewrite loop and a prompt that explicitly asks for corrected JSON after a parse failure. However, generate_article_payload() calls the LLM and parses its response before entering the loop's try block. The resulting RuntimeError therefore bypasses the existing retry handler and aborts processing after the first malformed response.
Scope
- Move
generate_article_payload()into the existingtryblock ingenerate_articles_with_quality_retries(). - Preserve the current limit of three quality rewrite attempts.
- Preserve the existing JSON repair function and retry feedback prompt.
- Add regression coverage proving that a malformed first response is retried and a valid second response succeeds.
- Deploy through the existing Forgejo
mainworkflow. - Re-run production video
#213and verify that draft articles are created or, if all three LLM responses remain invalid, that the progress log records retries2/3and3/3before the final error.
Non-goals
- Do not store raw LLM responses.
- Do not add undocumented
response_formatparameters to the Timeweb request. - Do not refactor the article-generation pipeline or JSON repair heuristics.
- Do not change the existing frame lifecycle during reprocessing; record the frame count before and after the production retry so this pre-existing behavior remains visible.
- Do not modify unrelated passkey/authentication or admin changes already present in the worktree.
Control Flow
For each quality attempt, the worker will call and parse the LLM response inside the existing exception boundary. A JSON parse RuntimeError matching the existing quality markers will set feedback and continue to the next attempt. The next prompt will include the current instruction to return one valid JSON object. A valid payload continues through the existing article validation path.
Network retries inside call_llm() remain separate from quality retries: transport and server failures follow the existing network retry policy, while syntactically invalid model content follows the three-attempt quality rewrite policy.
Testing
A focused stdlib unittest test will replace generate_article_payload() with a deterministic fake and patch append_progress() to avoid accessing the real database:
- First call raises
RuntimeErrorcontaining the existing invalid-JSON marker. - Second call returns a valid payload.
articles_from_payload()is patched to return a sentinel successful result so the test remains focused on retry control flow.- The test asserts that two payload-generation calls occurred, the second call received the first error as
quality_feedback, and article generation returned the successful result.
Existing syntax/tests will be run without relying on the real LLM service.
Production Verification
After pushing only the task files, wait for the Forgejo deployment, verify /health, confirm the running worker contains the deployed fix, record the current frame count for video #213, and requeue it. Completion requires checking the video's production status, progress log, resulting article records, and final frame count; deployment health alone is insufficient.