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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion skillopt/envs/officeqa/rollout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
107 changes: 107 additions & 0 deletions tests/test_officeqa_tool_arguments.py
Original file line number Diff line number Diff line change
@@ -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)]),
("<answer>42</answer>", 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