FrozenLake RL Trajectories → LLM SFT Converter
This utility converts numeric RL testing trajectories collected from the FrozenLake wrapper into language trajectories suitable for supervised fine-tuning (SFT) of LLM agents.
It reconstructs the text grid from the one-hot observations saved during RL testing and formats per-turn prompts and responses to mirror RAGEN’s ContextManager chat format.
Path Layout
- Input:
runs/<experiment>/trajectories/step_XXXXXX/trajectories.jsonl - Output:
runs/<experiment>/sft/step_XXXXXX_sft.jsonlpython scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake__1__1762841111 --step step_993280 --include_failed
Each output line is a JSON object:
messages: chat array with rolessystem,user,assistant,user (reward)meta:{ episode_return, episode_success, global_step }
Requirements
- Python 3.8+
- Optional:
pyyamlif you want the script to readconfig/envs.yamlandconfig/base.yamlforenv_instruction,action_sep, andenable_think. If not installed, built-in defaults are used.
Usage
Convert the latest step under the run directory:
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake__1__1762841111
Convert a specific step directory:
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake__1__1762841111 --step step_993280
Include failed episodes (by default only successful episodes are kept):
python scripts/convert_rl_to_sft_frozenlake.py runs/FrozenLake__ppo_frozenlake_nochangeenv__1__1763561679 --step step_1986560
What the Converter Does
- Reconstructs the 4×4 grid text from the FrozenLake numeric state:
- One-hot over 4 cell types plus 2 normalized coordinates for player position
- Displays
Pfor player on frozen,Xfor player in hole, and√for player on goal
- Builds a chat-style prompt per episode:
system: "You're a helpful assistant."user:env_instructionfollowed by per-turn blocks withState, remaining actions, and format constraintsassistant: Tagged action outputs:<think></think><answer>Action</answer>(or<answer>Action</answer>if think disabled)user:Rewardafter each assistant response
- Maps RL actions (0..3) to RAGEN’s
{Left, Down, Right, Up} - Reads
global_stepfrom the step’smetrics.jsonif present
Configuration Hooks
config/envs.yaml→FrozenLake.env_instruction,FrozenLake.max_tokensconfig/base.yaml→agent_proxy.action_sep(default||),agent_proxy.enable_think(defaultTrue)
If these files are not present or pyyaml is not installed, the converter uses safe defaults.
Output Example (truncated)
{
"messages": [
{"role": "system", "content": "You're a helpful assistant. "},
{"role": "user", "content": "You are solving the FrozenLake puzzle...\nTurn 1:\nState:\n__PO\n____\nG___\n____\nYou have 4 actions left..."},
{"role": "assistant", "content": "<think></think><answer>Left</answer>"},
{"role": "user", "content": "Reward:\n0.0\n"},
...
],
"meta": {"episode_return": 1.0, "episode_success": true, "global_step": 993280}
}
Notes
- The converter currently targets FrozenLake; extending to Bandit and Sokoban is straightforward by adapting the state decoder.
- The per-turn structure follows the training-time prompt generator so SFT will be consistent with RL rollouts.