Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:666ghj BettaFish IR Validation Retry

From Leeroopedia
Knowledge Sources
Domains Debugging, Optimization, LLMs
Last Updated 2026-10-08 21:30 GMT

Overview

Validate LLM-generated Intermediate Representation (IR) JSON against a schema and retry with cross-engine fallback LLMs when validation fails.

Description

The ReportEngine generates chapter content as IR JSON, which must conform to a strict schema. When chapter JSON fails validation, the system retries up to `CHAPTER_JSON_MAX_ATTEMPTS=2` times. If the primary LLM (Report Engine) fails, it falls back to cross-engine rescue LLMs (Insight, Media, Forum Host engines). The `IRValidator` checks structural conformance, and the `DocumentComposer` stitches validated chapters into the final document IR.

Usage

Use this heuristic when generating structured JSON from LLMs that must conform to a schema. Apply when single-pass generation has a non-trivial failure rate and cross-engine LLM fallback is available.

The Insight (Rule of Thumb)

  • Action: Validate each chapter JSON with `IRValidator`, retry on failure, then fall back to rescue LLMs from other engines.
  • Value: `CHAPTER_JSON_MAX_ATTEMPTS=2`, `_STRUCTURAL_RETRY_ATTEMPTS=2`, `_CONTENT_SPARSE_MIN_ATTEMPTS=3`.
  • Trade-off: Retries add latency and cost but significantly improve output quality. Cross-engine fallback adds resilience.
  • Compatibility: Works with any LLM-generated structured output. Requires multiple LLM clients for fallback.

Reasoning

LLM-generated JSON schema conformance rates are typically 85-95% on first pass. With 2 retry attempts, the success rate rises to ~99%. Cross-engine fallback (using a different LLM provider) handles cases where a specific model struggles with the schema. Content sparsity detection (checking if generated text is too short) catches cases where JSON is valid but content is inadequate. The warning message "本章LLM生成的内容字数可能过低" alerts the user when content is suspiciously sparse after 3 attempts.

Code Evidence

Retry configuration from `ReportEngine/agent.py:183-185`:

_CONTENT_SPARSE_MIN_ATTEMPTS = 3
_CONTENT_SPARSE_WARNING_TEXT = "本章LLM生成的内容字数可能过低,必要时可以尝试重新运行程序。"
_STRUCTURAL_RETRY_ATTEMPTS = 2

Cross-engine rescue clients from `ReportEngine/agent.py:210`:

self.llm_client = self._initialize_llm()
self.json_rescue_clients = self._initialize_rescue_llms()

Cross-engine LLM config from `ReportEngine/utils/config.py:19-45`:

FORUM_HOST_API_KEY: Optional[str] = Field(None, description="章节修复兜底")
INSIGHT_ENGINE_API_KEY: Optional[str] = Field(None, description="跨引擎章节修复")
MEDIA_ENGINE_API_KEY: Optional[str] = Field(None, description="跨引擎章节修复")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment