From ac9ceaf2eece5b41151a332f77fdc523c5dafb63 Mon Sep 17 00:00:00 2001 From: luojiyin Date: Mon, 19 Jan 2026 12:34:39 +0800 Subject: [PATCH] fix: prevent infinite loop in extract_toc_content The while loop exit condition used len(chat_history), but chat_history was rebuilt every iteration with exactly 2 elements, making the check len(chat_history) > 5 never true. Replace with explicit attempt counter and max_attempts limit. --- pageindex/page_index.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pageindex/page_index.py b/pageindex/page_index.py index 882fb5dea..2d2bebaf2 100644 --- a/pageindex/page_index.py +++ b/pageindex/page_index.py @@ -180,19 +180,22 @@ def extract_toc_content(content, model=None): response = response + new_response if_complete = check_if_toc_transformation_is_complete(content, response, model) + attempt = 0 + max_attempts = 5 + while not (if_complete == "yes" and finish_reason == "finished"): + attempt += 1 + if attempt > max_attempts: + raise Exception('Failed to complete table of contents after maximum retries') + chat_history = [ - {"role": "user", "content": prompt}, - {"role": "assistant", "content": response}, + {"role": "user", "content": prompt}, + {"role": "assistant", "content": response}, ] prompt = f"""please continue the generation of table of contents , directly output the remaining part of the structure""" new_response, finish_reason = ChatGPT_API_with_finish_reason(model=model, prompt=prompt, chat_history=chat_history) response = response + new_response if_complete = check_if_toc_transformation_is_complete(content, response, model) - - # Optional: Add a maximum retry limit to prevent infinite loops - if len(chat_history) > 5: # Arbitrary limit of 10 attempts - raise Exception('Failed to complete table of contents after maximum retries') return response