Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pageindex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tests/test_summary_finish_reason.py
Original file line number Diff line number Diff line change
@@ -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"