Skip to content

fix(llm): adaptive split-and-retry for label_communities on parse failure (#1278) - #1380

Closed
CJdev232 wants to merge 1 commit into
Graphify-Labs:v8from
CJdev232:fix/label-batch-adaptive-retry
Closed

CJdev232 wants to merge 1 commit into
Graphify-Labs:v8from
CJdev232:fix/label-batch-adaptive-retry

Conversation

@CJdev232

Copy link
Copy Markdown
Contributor

What

Fixes #1278.

label_communities previously logged-and-skipped any batch where the LLM returned malformed JSON or a non-object payload. On large graphs (the reporter's case: 121k nodes / 135 batches), a single failed batch silently lost ~100 community names with no recovery and no warning beyond the per-batch stderr line.

This mirrors the recovery shape _extract_with_adaptive_retry already uses for the extract path: when a batch fails to parse, split it at the midpoint and retry each half recursively. Most transient LLM JSON glitches go away at the first or second split (smaller prompt → smaller output → less likely to truncate or mangle).

Fix

  • New helper _label_batch_with_retry() in graphify/llm.py. Catches (json.JSONDecodeError, ValueError) from _parse_label_response, splits batch_cids and batch_lines together at mid = len(batch_cids) // 2, recurses on each half with depth + 1. Recursion capped at max_depth=3 (same as extract).
  • Non-parse exceptions (network errors, missing API keys, programming bugs) propagate unchanged via bare raise. The retry net is intentionally narrow.
  • label_communities is now a thin wrapper: builds slices, calls the helper, tracks first_error and written exactly as before. The outer graceful-degradation contract is preserved — when retries exhaust at the base case, the original parse error re-raises so generate_community_labels still falls back to placeholders rather than silently returning empty.

Test

New tests/test_label_retry.py adds a unit test with a stateful mock that returns malformed JSON on the first call and clean JSON on subsequent (smaller) calls. Confirms:

  • All 4 input communities end up labeled despite the initial failure
  • _call_llm was called more than once (proving the retry actually happened)

All 15 existing tests in tests/test_labeling.py continue to pass — including test_label_communities_malformed_raises (the old contract is preserved) and test_generate_community_labels_degrades_on_error (graceful fallback still works when retries exhaust).

Design choices worth flagging

  • Exception types caught — only (json.JSONDecodeError, ValueError) (the two things _parse_label_response raises). Network errors / config errors are not retried because they're not the recoverable case the issue describes.
  • max_depth=3 for consistency with _extract_with_adaptive_retry. With batch_size=100, this allows up to 8 leaf batches of ~12-13 communities each before giving up. If real-world cases need deeper recursion, that's a follow-up tuning.
  • Re-raise at base case (not silent return) so the existing first_error tracking in label_communities still fires. A partial-success edge case (left half succeeds, right half exhausts) loses the left half's labels — but in exchange, the existing graceful-degradation contract is preserved. The alternative (return empty dict at base case) breaks test_label_communities_malformed_raises semantics.

Notes

  • Reproducible context: the original issue was on a 121k-node corpus with batch_size=100 and Gemini backend. I was unable to reproduce the LLM-side malformed-JSON nondeterminism directly; the test uses a stateful mock to simulate the documented failure mode.
  • The fix touches only llm.py — no API surface changes.
  • Ran the full test suite. One pre-existing failure unrelated to this change: tests/test_cli_export.py::test_export_wiki_accepts_edges_only_graph_json fails on unmodified v8 with KeyError: 'links'. Looks like the same edges-vs-links key pattern already fixed in affected.py (fix(affected): handle "edges"-keyed graph.json in load_graph (KeyError: 'links') #1280); the wiki export path appears to have been missed. Happy to follow up in a separate PR if useful.

…lure (Graphify-Labs#1278)

Closes Graphify-Labs#1278.

label_communities now mirrors the extract path's adaptive recovery shape.
When the LLM returns malformed JSON or a non-object payload for a batch,
the batch is split at the midpoint and each half is retried recursively
(capped at depth=3). Non-parse exceptions still propagate unchanged.

Preserves the existing graceful-degradation contract: when retries
exhaust at the base case, the original parse error re-raises so
generate_community_labels still falls back to placeholders rather than
silently returning empty.

Added tests/test_label_retry.py - a stateful mock that simulates one
malformed batch followed by clean sub-batches confirms all communities
end up labeled. All 15 existing tests in tests/test_labeling.py pass
unchanged.
@safishamsi

Copy link
Copy Markdown
Member

Merged to v8 in ab1e0ec — thanks @CJdev232. The split-and-retry logic is correct and mirrors _extract_with_adaptive_retry cleanly; verified termination (single-element base case re-raises, caller skips that batch), split alignment, and that non-parse errors aren't retried. On merge I removed the leftover # YOUR TURN — scaffolding comment block and corrected the helper docstring (it said the base case returns empty, but it re-raises — which is what the caller's per-batch skip + all-fail-reraise contract expects). Full suite green. The partial-success label-loss on a fully-exhausted sub-branch is a known trade-off (it preserves the malformed-raises contract) — fine to revisit separately.

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.

cluster-only skips labeling batch on JSON parse error without retry or chunk split (inconsistent with extract)

2 participants