Skip to content

LAB 17 / Tools & environments

ToolRLQuick start

Download the rlla_4k data, run ToolRL GRPO training on Qwen3.5-4B, evaluate on the held-out test split and BFCL Non-Live, and optionally run the reward and <think> prefill ablations.

Read the method → View code ↗

Run all commands from the repository root. The project requires Python >=3.13.

bash
uv sync
trio login
swanlab login

1. Download the data ​

bash
uv run python experiments/toolrl/prepare_data.py

The script downloads dataset/rlla_4k_raw/rlla_rl.json (about 15 MB, 4,000 samples) from the official repository qiancheng0/ToolRL, shuffles it with a fixed seed, holds out 2% as the test split, and writes:

text
experiments/toolrl/datasets/train.jsonl   3920 条
experiments/toolrl/datasets/test.jsonl      80 条

No tool is executed during training, so you do not need a sandbox, a search backend or any extra API key.

2. Evaluate the base model ​

bash
uv run python experiments/toolrl/eval.py \
    --output experiments/toolrl/eval-results/base.jsonl

Evaluation uses greedy decoding by default (--temperature 0), one sample per question. Per-sample results go to base.jsonl and the summary metrics go to base.summary.json.

The summary reports two sets of metrics: all covers all 80 samples, and clean drops the 8 samples with wrong ground truth listed in test_exclude.json (for example, integers written as "765.0" or truncated nested JSON). After editing the exclusion list, you can re-summarize without sampling again:

bash
uv run python experiments/toolrl/eval.py --summarize-only \
    --output experiments/toolrl/eval-results/base.jsonl

3. Start training ​

First run a 2-step smoke test to confirm that the prompt, the reward and remote training all work:

bash
uv run python experiments/toolrl/train.py \
    --max-steps 2 \
    --questions-per-batch 4 \
    --group-size 4 \
    --save-every 0 \
    --swanlab-mode disabled

Step 0 prints the end of the prompt and the first sample. The prompt should end with <|im_start|>assistant\n, and the sample should start with a <think> written by the model itself.

20-step validation run:

bash
uv run python experiments/toolrl/train.py \
    --max-steps 20 \
    --questions-per-batch 32 \
    --group-size 4 \
    --save-every 10 \
    --run-name toolrl-qwen35-4b-step20 \
    --save-rollouts \
    --swanlab-mode online

--save-rollouts writes every sample of every step (text, reward components and advantage) to experiments/toolrl/rollouts/<run-name>.jsonl, so you can inspect how the model's behavior changes sample by sample.

A checkpoint is saved every --save-every steps; if the last step does not fall on the save interval, an extra -final checkpoint is saved at the end. The terminal prints the trio:// path of the sampler weights, which you need for evaluation.

Default hyperparameters: Qwen3.5-4B, LoRA rank 32, lr 4e-5, 4 samples per question, a 2048-token prompt limit, a 1024-token generation limit, PyTRIO's built-in ppo loss (clip ε=0.2), and no KL.

The prompt matches the official setup: it ends with <|im_start|>assistant\n and the model writes <think> itself (when thinking is disabled, the Qwen3.5 template inserts an empty thinking block, which the script removes). Add --think-prefill to prefill <think>\n at the end of the prompt instead; see Section 6.

Before the format check, the double newline after </think> is normalized to a single newline by default, to accommodate Qwen3.5's output habit. Add --strict-think-newline to turn off the normalization and match the official format check exactly.

4. Evaluate the trained weights ​

bash
uv run python experiments/toolrl/eval.py \
    --model-path 'trio://RUN_ID/sampler_weights/toolrl-qwen35-4b-step20-step-20-weights' \
    --output experiments/toolrl/eval-results/step20.jsonl

The base model and the checkpoints must use the same evaluation arguments (--temperature, --n, --max-tokens, --strict-think-newline, --think-prefill).

5. BFCL evaluation ​

Out-of-distribution generalization is measured on the Non-Live subset of BFCL v4, 1,240 questions in total:

CategoryQuestionsWhat it tests
simple_python400One function, called once
multiple200Pick the right function from several candidates
parallel200The same function called several times
parallel_multiple200Several functions, each called once or more
irrelevance240No suitable function; no tool should be called

First download the data (about 1.5 MB, pinned to gorilla commit f7cf7359b7, written to experiments/toolrl/datasets/bfcl/):

bash
uv run python experiments/toolrl/bfcl_eval.py --prepare

Evaluate the base model:

bash
uv run python experiments/toolrl/bfcl_eval.py \
    --output experiments/toolrl/eval-results/bfcl-base.jsonl

Evaluate the trained weights:

bash
uv run python experiments/toolrl/bfcl_eval.py \
    --model-path 'trio://RUN_ID/sampler_weights/toolrl-qwen35-4b-step20-step-20-weights' \
    --output experiments/toolrl/eval-results/bfcl-step20.jsonl

For a quick check, add --limit 5 to run only 5 questions per category, or --categories irrelevance (and so on) to run only the listed categories. The summary is written to a .summary.json file with the same name and includes per-category accuracy and the average over the four AST categories.

Notes:

  • The prompt follows the template of the official ToolRL BFCL handler (benchmarks/BFCL/rlla_qwen.py), and parsing likewise keeps only the last <tool_call> block, so a model that splits several calls into several blocks is marked wrong on the parallel categories.
  • Scoring uses bfcl_checker.py, a trimmed Python port of the official BFCL AST checker. The results do not come from the official leaderboard pipeline and cannot be compared directly with the numbers in the paper or on the leaderboard.

6. Ablations (optional) ​

The reward granularity and scale ablations correspond to the experiments in the paper; keep the other arguments the same as in Step 3:

bash
# 奖励粒度:fine(默认)/ intermediate / coarse / strict
--granularity coarse

# 正确性奖励缩放到 [-1, 1](论文 Equal Max)
--correct-max 1.0

# advantage 只减组内均值,不除标准差
--no-std-norm

Use a different --run-name for each ablation so the runs are easy to compare in SwanLab.

<think> prefill ablation ​

By default the prompt ends with <|im_start|>assistant\n and the model writes <think> itself, as in the official setup. With --think-prefill, thinking rendering is turned on instead: the prompt ends with <think>\n and the model continues straight into its reasoning, so the "write <think>" part of the format reward no longer applies.

Because the prompt changes, training and every evaluation (including the base model) must use the same flag:

bash
uv run python experiments/toolrl/train.py \
    --max-steps 20 \
    --questions-per-batch 32 \
    --group-size 4 \
    --save-every 10 \
    --run-name toolrl-qwen35-4b-step20-prefill \
    --think-prefill \
    --swanlab-mode online

uv run python experiments/toolrl/eval.py --think-prefill \
    --output experiments/toolrl/eval-results/prefill-base.jsonl

uv run python experiments/toolrl/bfcl_eval.py --think-prefill \
    --output experiments/toolrl/eval-results/bfcl-prefill-base.jsonl

Also add --think-prefill when evaluating checkpoints, and pass the weights with --model-path. The think_prefill field in the summary file records which prompt was used for the evaluation.

Open code. Real experiments. Honest limitations.