Skip to content

fix: handle JSON array responses in Inference.ts#800

Closed
rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
rikitikitavi2012-debug:fix/inference-json-parsing
Closed

fix: handle JSON array responses in Inference.ts#800
rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
rikitikitavi2012-debug:fix/inference-json-parsing

Conversation

@rikitikitavi2012-debug
Copy link

Summary

  • Fix JSON parsing in Inference.ts --json mode to handle both object and array responses
  • The existing greedy regex /\{[\s\S]*\}/ fails when the LLM returns a JSON array ([...]), which silently breaks 4+ downstream tools

Problem

When expectJson: true is set, Inference.ts uses a single regex to extract JSON:

const jsonMatch = output.match(/\{[\s\S]*\}/);

This fails in two ways:

  1. JSON arrays are missed entirely[{...}, {...}] doesn't match the object regex
  2. Greedy capture produces invalid JSON — when LLM wraps output in markdown with braces, the regex captures too much

Affected tools that use --json with 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.ts returns { 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:

const objectMatch = output.match(/\{[\s\S]*\}/);
const arrayMatch = output.match(/\[[\s\S]*\]/);

for (const candidate of [objectMatch?.[0], arrayMatch?.[0]]) {
  if (!candidate) continue;
  try {
    const parsed = JSON.parse(candidate);
    // success — use this candidate
  } catch { /* try next candidate */ }
}

Object match is tried first (most common case), then array match as fallback.

Test plan

  • Verified with prompts returning {...} objects — still works
  • Verified with prompts returning [{...}, {...}] arrays — now works
  • Verified with markdown-wrapped JSON — greedy capture validated by parse

🤖 Generated with Claude Code

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.
@danielmiessler
Copy link
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

danielmiessler added a commit that referenced this pull request Mar 2, 2026
- #800: Inference.ts JSON array parsing (objects + arrays)
- #836: CONTEXT_ROUTING.md dead reference cleanup (29 refs removed)
- #817: WorldThreatModelHarness $PAI_DIR portability
- #846: User context migration for v2.5/v3.0 upgraders

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants