fix: return actionable error for malformed page ranges in local tool

The local get_page_content agent tool only converted DocumentNotFoundError
into a JSON error; a malformed page spec ("all", "5-") let parse_pages'
ValueError surface as the agent SDK's generic non-fatal tool-failure text
("An error occurred... invalid literal for int()"), which the model can't
act on. Catch (ValueError, AttributeError) and return the same actionable
"Invalid pages format: ... Use '5-7', '3,8', or '12'" message the legacy
retrieval tool already gives, so the model can retry with a valid range.
This commit is contained in:
mountain 2026-07-09 19:22:04 +08:00
parent 91e016d2e4
commit 56fc3bf7bb
2 changed files with 20 additions and 0 deletions

View file

@ -127,6 +127,18 @@ def test_scoped_mode_allows_in_scope_doc_id(populated_backend):
assert out.get("doc_name") == "alpha.pdf"
@pytest.mark.parametrize("bad_pages", ["all", "5-", "abc", "3-1"])
def test_get_page_content_returns_actionable_error_for_bad_page_spec(populated_backend, bad_pages):
# A malformed page spec must come back as a correctable JSON error (like the
# legacy retrieval tool), not the agent SDK's generic tool-failure fallback,
# so the model can retry with a valid range instead of giving up.
tools = populated_backend.get_agent_tools("papers", doc_ids=["d1"])
by_name = {t.name: t for t in tools.function_tools}
out = json.loads(_invoke_tool(by_name["get_page_content"], {"doc_id": "d1", "pages": bad_pages}))
assert "error" in out
assert "Invalid pages format" in out["error"]
def test_wrap_with_doc_context_single(populated_backend):
from pageindex.agent import wrap_with_doc_context
docs = populated_backend._scoped_docs("papers", ["d1"])