From 4d1173595235a7b4c16bda528da66829c6f70bc2 Mon Sep 17 00:00:00 2001 From: tsumon <47944351+tsumon@users.noreply.github.com> Date: Mon, 30 Nov 2026 05:04:11 -0800 Subject: [PATCH] fix: fall back when a node summary hits the output limit --- pageindex/utils.py | 16 ++++++++-- tests/test_summary_finish_reason.py | 46 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/test_summary_finish_reason.py diff --git a/pageindex/utils.py b/pageindex/utils.py index 9aa6cfdfd..723f28296 100644 --- a/pageindex/utils.py +++ b/pageindex/utils.py @@ -52,6 +52,11 @@ _llm_priority: contextvars.ContextVar = contextvars.ContextVar( "pageindex_llm_priority", default=0) +# Node summaries have a deterministic fallback; other async callers keep the +# original reply, and the Claude Code path handles its own completion errors. +_summary_complete_only: contextvars.ContextVar = contextvars.ContextVar( + "pageindex_summary_complete_only", default=False) + @contextmanager def llm_priority(prio): @@ -647,7 +652,10 @@ async def _acompletion(model, messages, sent): **sent, })) _add_usage(response) - return response.choices[0].message.content + choice = response.choices[0] + if _summary_complete_only.get() and getattr(choice, "finish_reason", None) == "length": + return "" + return choice.message.content except Exception as e: if getattr(e, "status_code", None) in _NO_RETRY_STATUS: raise @@ -1689,7 +1697,11 @@ async def _ask(self, prompt, prio): self._asked = True async with self._gate.slot(prio): with llm_priority(prio): - reply = await self._complete(prompt) + token = _summary_complete_only.set(True) + try: + reply = await self._complete(prompt) + finally: + _summary_complete_only.reset(token) if parse_summary(reply): self._answered = True return reply diff --git a/tests/test_summary_finish_reason.py b/tests/test_summary_finish_reason.py new file mode 100644 index 000000000..329188905 --- /dev/null +++ b/tests/test_summary_finish_reason.py @@ -0,0 +1,46 @@ +"""Node summaries must not store a provider's truncated JSON response.""" + +import asyncio +from types import SimpleNamespace + +import litellm +import pytest + +import pageindex.utils as utils + + +@pytest.mark.parametrize("finish_reason, reply, expected", [ + ("length", '{"summary": "truncated', "A; B"), + ("stop", '{"summary": "complete"}', "complete"), + ("stop", "plain summary", "plain summary"), +]) +def test_parent_summary_uses_fallback_only_for_length_stop(monkeypatch, finish_reason, reply, expected): + async def complete(**kwargs): + prompt = kwargs["messages"][0]["content"] + parent = "Section Title" in prompt + return SimpleNamespace(choices=[SimpleNamespace( + finish_reason=finish_reason if parent else "stop", + message=SimpleNamespace(content=reply if parent else '{"summary": "leaf"}'), + )]) + + monkeypatch.setattr(litellm, "acompletion", complete) + pages = [("alpha " * 300, 0), ("beta " * 300, 0)] + nodes = [{"title": "R", "start_index": 1, "end_index": 2, "nodes": [ + {"title": "A", "start_index": 1, "end_index": 1}, + {"title": "B", "start_index": 2, "end_index": 2}, + ]}] + + out = asyncio.run(utils.summarize_tree(nodes, pages, model="gpt-4o-mini", small_node_tokens=0)) + + assert [node["summary"] for node in utils._subtree(out)] == [expected, "leaf", "leaf"] + + +def test_async_completion_outside_summary_returns_partial_content(monkeypatch): + async def complete(**kwargs): + return SimpleNamespace(choices=[SimpleNamespace( + finish_reason="length", message=SimpleNamespace(content="partial"), + )]) + + monkeypatch.setattr(litellm, "acompletion", complete) + + assert asyncio.run(utils.llm_acompletion("gpt-4o-mini", "prompt")) == "partial"