fix: add type checks to prevent AttributeError when LLM returns malformed JSON#210
Open
octo-patch wants to merge 1 commit intoVectifyAI:mainfrom
Open
fix: add type checks to prevent AttributeError when LLM returns malformed JSON#210octo-patch wants to merge 1 commit intoVectifyAI:mainfrom
octo-patch wants to merge 1 commit intoVectifyAI:mainfrom
Conversation
sicko7947
added a commit
to sicko7947/PageIndex
that referenced
this pull request
Apr 6, 2026
Combines fixes from PRs VectifyAI#217, VectifyAI#210, and additional guards for all remaining crash sites in toc_transformer and related functions. Fixes: - TypeError: int + NoneType when calculate_page_offset returns None (VectifyAI#153) - KeyError on dict access when extract_json returns {} (VectifyAI#163) - AttributeError: NoneType has no startswith (VectifyAI#199) - KeyError: 'table_of_contents' when LLM output is malformed - AttributeError: 'dict' has no extend / 'str' has no get Changes: - add_page_offset_to_toc_json: guard None offset, return data unchanged - process_none_page_numbers: fix prev/next defaults (start_index/end_index) - toc_detector_single_page: .get() with 'no' default - check_if_toc_extraction_is_complete: .get() with 'no' default - check_if_toc_transformation_is_complete: .get() with 'no' default - detect_page_index: .get() with 'no' default - toc_transformer: isinstance + .get() for table_of_contents access - toc_transformer: None/isinstance guard on new_complete.startswith - single_toc_item_index_fixer: .get() for physical_index - meta_processor: isinstance(item, dict) filter - process_no_toc: isinstance guard for generate_toc_init/continue results
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.
Fixes #199
Problem
When the LLM returns malformed JSON,
extract_json()inutils.pyreturns{}(empty dict) as a fallback. This causes two crashes downstream:Crash 1:
AttributeError: 'dict' object has no attribute 'extend'inprocess_no_tocgenerate_toc_init()can return{}on parse failure.extend()on a dict raises AttributeErrorCrash 2:
AttributeError: 'str' object has no attribute 'get'inmeta_processor.get()failsSolution
Added minimal type guards at the two crash sites:
process_no_toc: Check thatgenerate_toc_init()returns a list before using it. Skip non-list results fromgenerate_toc_continue()rather than crashing.meta_processor: Addedisinstance(item, dict)check in the list comprehension filter so non-dict items (strings, etc.) are safely discarded instead of raising AttributeError.These are the smallest possible fixes — they don't change the overall flow, just guard against unexpected return types from
extract_json().Testing
Reproducible with any local vLLM endpoint or small model (e.g. Qwen 7B) that frequently returns malformed JSON. With this fix, the pipeline gracefully handles parse failures instead of crashing with AttributeError.