From e38c494e324b6c7dca36eaf9d7a3e01b63ba92e8 Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Wed, 2 Dec 2026 21:25:45 +0000 Subject: [PATCH] fix(officeqa): keep the tool loop going on malformed tool arguments process_one parsed each tool call's arguments with json.loads and had no local error handling. Malformed JSON raised JSONDecodeError. Valid JSON that is not an object reached run_tool, where .get raised AttributeError. Either exception left the loop and was recorded as the attempt's error, so the model's next reply, which could contain the right answer, was never read. Use an empty dict for unparsable arguments and for any parsed value that is not an object. Object arguments are passed to the tool unchanged. --- CHANGELOG.md | 5 ++ skillopt/envs/officeqa/rollout.py | 9 ++- tests/test_officeqa_tool_arguments.py | 107 ++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 tests/test_officeqa_tool_arguments.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7542320d2..5ab5ab5ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,11 @@ All notable changes to SkillOpt are documented here. This project adheres to value is rejected with a clear error. ### Fixed +- The OfficeQA tool loop keeps going when a tool call's arguments are malformed + JSON or valid JSON that is not an object. Such arguments ended the attempt with + a JSON or AttributeError, so the model's next reply, which could hold the right + answer, was never read. They now become an empty dict, as in the ReAct + SpreadsheetBench loop (thanks @Shizoqua). - `scripts/train.py` accepts `--slow_update_gate_with_selection`. The config key `optimizer.slow_update_gate_with_selection` exists, but the flag was never added, so `--slow_update_gate_with_selection true` failed with an diff --git a/skillopt/envs/officeqa/rollout.py b/skillopt/envs/officeqa/rollout.py index c3aac5511..2975c4d19 100644 --- a/skillopt/envs/officeqa/rollout.py +++ b/skillopt/envs/officeqa/rollout.py @@ -717,7 +717,14 @@ def process_one( if getattr(message, "tool_calls", None): for tool_call in message.tool_calls: tool_name = tool_call.function.name - arguments = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {} + try: + arguments = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {} + except json.JSONDecodeError: + arguments = {} + # Valid JSON that is not an object has no fields to read; + # treat it like unparsable arguments so the loop continues. + if not isinstance(arguments, dict): + arguments = {} cmd, obs = run_tool(tool_name, arguments, allowed_roots=docs_roots, allowed_files=allowed_files) conversation.append({"type": "tool_call", "cmd": cmd, "obs": obs}) messages.append({ diff --git a/tests/test_officeqa_tool_arguments.py b/tests/test_officeqa_tool_arguments.py new file mode 100644 index 000000000..49161cd84 --- /dev/null +++ b/tests/test_officeqa_tool_arguments.py @@ -0,0 +1,107 @@ +"""A malformed or non-object tool argument does not end an OfficeQA attempt. + +``process_one`` parsed each tool call's arguments with ``json.loads`` and had no +local error handling. Malformed JSON raised JSONDecodeError, and valid JSON that +is not an object (an array, a string or ``null``) reached ``run_tool``, where +``.get`` raised AttributeError. Either exception left the tool loop and was +recorded as the attempt's error, so the model's next reply was never read. +Such arguments now become an empty dict, and the loop continues. +""" + +from __future__ import annotations + +from types import SimpleNamespace + +import pytest + +from skillopt.envs.officeqa import rollout as ro + +ITEM = {"id": "q1", "question": "What is revenue?", "ground_truth": "42", "source_files": []} + + +def _tool_call(arguments): + return SimpleNamespace( + id="c1", + function=SimpleNamespace(name="glob", arguments=arguments), + model_dump=lambda mode="json": { + "id": "c1", + "type": "function", + "function": {"name": "glob", "arguments": arguments}, + }, + ) + + +def _scripted_chat(replies): + remaining = iter(replies) + + def fake_chat(**kwargs): + content, tool_calls = next(remaining) + return SimpleNamespace(content=content, tool_calls=tool_calls), None + + return fake_chat + + +@pytest.fixture +def run(monkeypatch, tmp_path): + """Run one OfficeQA item with the model and the document tools replaced.""" + docs = tmp_path / "docs" + docs.mkdir() + monkeypatch.setattr(ro, "resolve_docs_roots", lambda data_dirs: [str(docs)]) + monkeypatch.setattr(ro, "resolve_candidate_files", lambda source_files, roots: []) + monkeypatch.setattr(ro, "build_oracle_parsed_pages_context", lambda *a, **k: "") + + tool_runs: list = [] + + def fake_run_tool(name, arguments, **kwargs): + tool_runs.append((name, arguments)) + return name, "[no matches]" + + monkeypatch.setattr(ro, "run_tool", fake_run_tool) + + def run_item(first_arguments): + monkeypatch.setattr( + ro, + "chat_target_messages", + _scripted_chat( + [ + ("", [_tool_call(first_arguments)]), + ("42", None), + ] + ), + ) + out = ro.process_one( + dict(ITEM), + str(tmp_path / "out"), + "", + max_tool_turns=3, + use_local_tools=True, + data_dirs=[str(docs)], + ) + return out, tool_runs + + return run_item + + +def test_malformed_json_arguments_do_not_end_the_attempt(run): + result, tool_runs = run('{"pattern": "*.txt"') + + assert result["fail_reason"] == "" + assert tool_runs == [("glob", {})] + assert result["hard"] == 1 + + +@pytest.mark.parametrize("arguments", ['["*.txt"]', '"*.txt"', "null", "42"]) +def test_non_object_arguments_do_not_end_the_attempt(run, arguments): + result, tool_runs = run(arguments) + + assert result["fail_reason"] == "" + assert tool_runs == [("glob", {})] + assert result["hard"] == 1 + + +def test_object_arguments_are_passed_to_the_tool_unchanged(run): + result, tool_runs = run('{"pattern": "*.txt"}') + + assert result["fail_reason"] == "" + assert tool_runs == [("glob", {"pattern": "*.txt"})] + assert result["hard"] == 1