Repository navigation
feat(tools): parse fenced and embedded JSON in filter_tools - #115
Conversation
filter_tools only stripped an exact ```json ... ``` fence before json.loads(). LLM tool-reflection output commonly wraps the payload in a plain ``` fence (no language tag) or embeds it between prose, which made the call raise ValueError and abort tool filtering. Add a best-effort _extract_json_payload() that pulls the payload out of a fenced block (with or without the json tag), or from the first JSON object, before falling back to the raw text. Truly invalid input still raises ValueError, preserving documented behavior. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
The contribution is retained in follow-up PR #120 with the original commits and attribution. The follow-up preserves valid JSON containing embedded fences, replaces greedy object extraction with structural decoding, rejects conflicting tool selections, and adds the missing regression coverage. Full local regression: 387 passed, 1 explicitly opt-in backend test skipped. #120 is submitted for review only; neither PR has been merged. |
|
The completed fixes have now been appended directly to this original contributor branch at 0cb40e0, preserving the existing commits and attribution. The newly triggered CI run passed all 4 checks: https://github.com/wanxingai/LightAgent/actions/runs/40616538629 . Supplemental PR #120 has been closed as redundant. This original PR remains open and unmerged for your review; main has not been modified. |
What
filter_tools()only stripped an exact``json ... ``` fence beforejson.loads(). LLM tool-reflection output commonly wraps the payload in a plain`` fence (no language tag) or embeds it between prose, which made the call raiseValueErrorand abort tool filtering.Why
Add a best-effort
_extract_json_payload()that pulls the payload out of a fenced block (with or without thejsontag), or from the first JSON object, before falling back to the raw text. Truly invalid input still raisesValueError, preserving documented behavior.How I verified
tests/test_tool_schema_validation.pycovering plain JSON, json-fenced, plain-fenced, prose-surrounded, prose-embedded, and two failure shapes.