Skip to content

Rollout

Chain Generation

Base class for chain-based generation:

ChainRollout()

Basic class for chain-based rollout. It starts multiple chains and runs them asynchronously.

Functions

get_trajectories() -> List[Trajectory]

Build :class:Trajectory objects for every active chain.

Maps chain.info keys into the typed Trajectory fields:

  • reward (a RewardResult produced by calculate_reward) → Trajectory.reward (float) + Trajectory.metrics (extras)
  • finish_reason, rollout_time_sec, chain_id, group_id, chain_idx, group_idx → first-class fields
  • everything else (dataset-row passthrough fields like answer, task_id, etc.) → Trajectory.metadata

run_async(messages: List[Dict], max_turns: int, num_chains: int, generation_config: Optional[Dict[str, Any]] = None, enable_streaming: bool = False, max_concurrent_chains: Optional[int] = None, context_config: Optional[ContextConfig] = None) async

Run the chain-based rollout with optional streaming support.

Parameters:

  • max_steps –

    Maximum number of steps for each chain.

  • start_messages –

    List of messages to start the chains.

  • num_chains (int) –

    Number of chains to run for each message.

  • max_concurrent_chains (Optional[int], default: None ) –

    Maximum number of chains to execute concurrently across the whole rollout. If None, all chains are scheduled at once.

  • generation_config (Optional[Dict[str, Any]], default: None ) –

    Generation configuration dictionary.

  • enable_streaming (bool, default: False ) –

    Whether to enable streaming mode.

  • streaming_callback –

    Optional callback for streaming events.

  • context_config (Optional[ContextConfig], default: None ) –

    Optional :class:~agentfly.core.context_config.ContextConfig for tool resources (backend).

apply_context_folding(chain: Chain, messages: Messages, tool_name: str, observation: Any, tool_call_id: str, tool_result_name: str, new_content: List[Dict[str, Any]], context: Context) -> Tuple[Messages, bool]

Apply context folding based on the tool that was just executed.

Parameters:

  • chain (Chain) –

    The current Chain object (used to record full histories).

  • messages (Messages) –

    The current full Messages object before applying the tool.

  • tool_name (str) –

    Name of the tool that was executed.

  • observation (Any) –

    Tool observation returned by the tool.

  • tool_call_id (str) –

    ID of the tool call.

  • tool_result_name (str) –

    Name of the tool result (tool implementation name).

  • new_content (List[Dict[str, Any]]) –

    Content payload for the tool message.

Returns:

  • Messages –

    (updated Messages, did_fold) where did_fold is True when history was

  • bool –

    replaced by a summarize fold (caller should refresh cumulative token counts).