fix(compress-report-section): tolerate trailing characters from small-model structured output#727
Merged
Merged
Conversation
…-model structured output Adds LenientJsonModel mixin that catches Pydantic's 'json_invalid' trailing-characters failure and extracts the first balanced JSON object via json.JSONDecoder().raw_decode before validating. Small structured-output LLMs (notably the default openrouter-gemini-2.5-flash-lite-preview-09-2025) occasionally emit a valid JSON object followed by extra tokens; the strict validator was rejecting the whole payload despite a correct prefix, causing compress_premortem to fail repeatedly for plans with multi-clause failure-mode tables. Recovery here is local: the extracted prefix is validated against the same schema; if it still fails, the original error is re-raised so genuine schema problems are not masked. Includes 4 unit tests covering the trailing-object, trailing-prose, well-formed, and schema-error paths. Also includes the failure-analysis doc captured against 20251110_4DWW_India. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compress_premortem.mdwas consistently failing for plans whose premortem section is dense with multi-clause failure-mode rows (most recently20251110_4DWW_India). The root cause is at the validator, not the model: the default LLM (openrouter-gemini-2.5-flash-lite-preview-09-2025) emits a syntactically valid JSON object then continues with extra tokens, and Pydantic'smodel_validate_jsonstrict-rejects the whole payload withjson_invalid("trailing characters at line N column M"). The retry loop wastes its budget on a response whose prefix is already correct.This PR adds a
LenientJsonModelmixin incompress_report_section.pythat catches that specific failure, extracts the first balanced JSON value viajson.JSONDecoder().raw_decode, and validates that against the same schema. If the extracted value still fails (genuine schema problem), the originalValidationErroris re-raised, so the recovery never masks structural issues.All six per-bucket schemas (
SectionSummaryOnly,NumericValuesOnly,LoadBearingAssumptionsOnly,GatesAndThresholdsOnly,RisksAndShocksOnly,MissingDataOnly) inherit the mixin.Failure analysis
Captured against the 4DWW India case in
experiments/napkin_math/docs/20260517_compress_premortem_failure.md— symptoms, failure pattern across 3 outer × 3 inner retries, root-cause hypothesis, and three escalation tiers (the validator-trim fix here is tier 1).Verification
prepare_extract_input.pyagainst20251110_4DWW_Indiawith the default LLM unchanged.compress_premortem.mdandcompress_premortem_raw.jsonwere both produced on the first outer attempt, with no[premortem] Attempt N failedlines. Premortem section is now in the bundled digest.Test plan
pytest worker_plan_internal/parameter_extraction/tests/test_compress_report_section.py— 17/17 green# Premortemsection heading🤖 Generated with Claude Code