fix: handle JSON array responses in Inference.ts#800
Closed
rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
Closed
fix: handle JSON array responses in Inference.ts#800rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
Conversation
The greedy object regex /\{[\s\S]*\}/ fails to parse valid JSON arrays
returned by the LLM. When the model returns a JSON array (e.g. from
WisdomExtractor, TELOSTracker), the object regex either captures an
invalid substring or misses the array entirely.
Fix: try both object and array regex matches, validate each with
JSON.parse, and use whichever parses successfully. This handles both
`{...}` and `[...]` responses reliably.
Affected tools: WisdomExtractor, TELOSTracker, FailureCapture,
IntegrityMaintenance — all use `--json` with array-returning prompts.
Owner
|
Thank you @rikitikitavi2012-debug! This fix is merged into PAI v4.0.3. The JSON array parsing improvement is solid — trying both object and array candidates with validation is the right approach. Fix included in: Releases/v4.0.3 |
HyggeHacker
pushed a commit
to HyggeHacker/Personal_AI_Infrastructure
that referenced
this pull request
Mar 2, 2026
…nielmiessler#836, danielmiessler#817, danielmiessler#846) - PR danielmiessler#800: Inference.ts JSON array parsing (dual-candidate strategy) - PR danielmiessler#836: CONTEXT_ROUTING.md dead reference cleanup (75→31 lines) - PR danielmiessler#817: WorldThreatModelHarness $PAI_DIR portability (10 instances) - PR danielmiessler#846: Installer user context migration (copyMissing + migrateUserContext) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5 tasks
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
Inference.ts--jsonmode to handle both object and array responses/\{[\s\S]*\}/fails when the LLM returns a JSON array ([...]), which silently breaks 4+ downstream toolsProblem
When
expectJson: trueis set,Inference.tsuses a single regex to extract JSON:This fails in two ways:
[{...}, {...}]doesn't match the object regexAffected tools that use
--jsonwith array-returning prompts:WisdomExtractor.ts— expects[{domain, observation, type}]TELOSTracker.ts— expects[{goal, status, evidence}]FailureCapture.ts— expects[{pattern, category}]IntegrityMaintenance.ts— expects[{file, issue}]All four tools silently fail because
Inference.tsreturns{ success: false, error: 'No JSON found' }and the callers proceed without data.Fix
Try both object and array regex matches, validate each candidate with
JSON.parse, use whichever parses successfully:Object match is tried first (most common case), then array match as fallback.
Test plan
{...}objects — still works[{...}, {...}]arrays — now works🤖 Generated with Claude Code