From e95622ca7b2943271d207d2061cc2864af7c849f Mon Sep 17 00:00:00 2001 From: yangdx Date: Mon, 1 Sep 2025 07:17:30 +0800 Subject: [PATCH] fix(utils): enhance remove_think_tags to handle orphaned closing tags The function now properly handles cases where text contains closing tags without corresponding opening tags, which can occur due to content truncation or processing errors. --- lightrag/utils.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lightrag/utils.py b/lightrag/utils.py index 7c92d99c..c215d52b 100644 --- a/lightrag/utils.py +++ b/lightrag/utils.py @@ -1594,8 +1594,11 @@ async def update_chunk_cache_list( def remove_think_tags(text: str) -> str: - """Remove tags from the text""" - return re.sub(r"^(.*?|)", "", text, flags=re.DOTALL).strip() + """Remove ... tags from the text + Remove orphon ... tags from the text also""" + return re.sub( + r"^(.*?|.*)", "", text, flags=re.DOTALL + ).strip() async def use_llm_func_with_cache( @@ -1678,13 +1681,7 @@ async def use_llm_func_with_cache( if max_tokens is not None: kwargs["max_tokens"] = max_tokens - try: - res: str = await use_llm_func(safe_input_text, **kwargs) - except Exception as e: - # Add [LLM func] prefix to error message - error_msg = f"[LLM func] {str(e)}" - # Re-raise with the same exception type but modified message - raise type(e)(error_msg) from e + res: str = await use_llm_func(safe_input_text, **kwargs) res = remove_think_tags(res)