mirror of
https://github.com/VectifyAI/PageIndex.git
synced 2026-07-18 21:21:05 +02:00
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:
parent
91e016d2e4
commit
56fc3bf7bb
2 changed files with 20 additions and 0 deletions
|
|
@ -306,6 +306,14 @@ class LocalBackend:
|
||||||
result = backend.get_page_content(col_name, doc_id, pages)
|
result = backend.get_page_content(col_name, doc_id, pages)
|
||||||
except DocumentNotFoundError:
|
except DocumentNotFoundError:
|
||||||
return json.dumps({"error": f"doc_id '{doc_id}' not found."})
|
return json.dumps({"error": f"doc_id '{doc_id}' not found."})
|
||||||
|
except (ValueError, AttributeError) as e:
|
||||||
|
# A malformed page spec ("all", "5-") is a recoverable bad tool
|
||||||
|
# argument: hand the model an actionable error it can correct
|
||||||
|
# (mirroring the legacy retrieval tool) rather than letting the
|
||||||
|
# ValueError surface as the agent SDK's generic tool-failure text.
|
||||||
|
return json.dumps({
|
||||||
|
"error": f"Invalid pages format: {pages!r}. Use '5-7', '3,8', or '12'. Error: {e}"
|
||||||
|
})
|
||||||
return json.dumps(result, ensure_ascii=False)
|
return json.dumps(result, ensure_ascii=False)
|
||||||
|
|
||||||
tools = [get_document, get_document_structure, get_page_content]
|
tools = [get_document, get_document_structure, get_page_content]
|
||||||
|
|
|
||||||
|
|
@ -127,6 +127,18 @@ def test_scoped_mode_allows_in_scope_doc_id(populated_backend):
|
||||||
assert out.get("doc_name") == "alpha.pdf"
|
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):
|
def test_wrap_with_doc_context_single(populated_backend):
|
||||||
from pageindex.agent import wrap_with_doc_context
|
from pageindex.agent import wrap_with_doc_context
|
||||||
docs = populated_backend._scoped_docs("papers", ["d1"])
|
docs = populated_backend._scoped_docs("papers", ["d1"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue