Add trajectory condensation utility for OpenHands SFT data #157
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a utility script that applies OpenHands SDK context condensation to SFT trajectories, splitting them when condensation occurs.
Motivation
Long agent trajectories can exceed context limits during training. The OpenHands SDK includes a context condenser that summarizes and removes older conversation turns when a threshold is exceeded. This utility applies that condensation to stored trajectories, creating training data that reflects the condensed context that would be seen during inference.
Implementation
Main Components
condense_trajectories.py: Main utility script that:mock_condenser.py: A mock condenser for testing that:README_condense_trajectories.md: Comprehensive documentationKey Features
max_sizeandkeep_firstcontrol condensation behaviorInput/Output
sample_sft_openhands.jsonwith N trajectories{original_id}_seg{index}Testing
The script has been tested on multiple datasets:
agenttuning_alfworld: 5 trajectories → 23 segments (avg 4.6x split)swe-smith: 5 trajectories → 29 segments (avg 5.8x split)swe-gym: 5 trajectories → 21 segments (avg 4.2x split)Example verification with jq:
jq '[.[] | {id, conversations: (.conversations | length)}]' output.jsonUsage
With mock condenser (for testing):
With LLM condenser:
Dependencies
openhands-sdkto be installedFuture Work
Potential enhancements:
@neubig can click here to continue refining the PR