- RejectionSamplingStrategy: Simple retry with the same prompt. Best for non-deterministic failures where the same instruction might succeed on retry.
- RepairTemplateStrategy: Single-turn repair by modifying the instruction with validation feedback. Adds failure reasons to the instruction and retries. Best for simple tasks where feedback can be incorporated into the instruction.
- MultiTurnStrategy: Multi-turn conversational repair (requires ChatContext). Adds validation failure reasons as new user messages in the conversation, allowing iterative improvement through dialogue. Best for complex tasks and agentic workflows.
Classes
CLASS BaseSamplingStrategy
Base class for multiple strategies that reject samples based on given instructions.
Args:
loop_budget: Maximum number of generate/validate cycles per concurrent subsample. Must be greater than 0. Defaults to1.concurrency_budget: Number of concurrent subsamples. Sampling generates at mostloop_budget * concurrency_budgetrequests and stops at the first valid result. Must be greater than 0. Defaults to1(no concurrency). When allowing concurrency, number of generations may substantially increase (potentially causing rate-limiting issues depending on the provider) and the returned order of results will no longer be deterministic.requirements: Global requirements evaluated on every sample. When set, overrides per-call requirements.
loop_budget=1: no repair strategies are used.loop_budget=3, concurrency_budget=1: generate -> repair -> generate -> repair -> final generate.loop_budget=2, concurrency_budget=2: two concurrent subsamples, each with one repair.
ValueError: Ifloop_budget < 1orconcurrency_budget < 1.
FUNC repair
old_ctx: The context WITHOUT the last action + output.new_ctx: The context including the last action + output.past_actions: List of actions that have been executed (without success).past_results: List of (unsuccessful) generation results for these actions.past_val: List of validation results for the results.
- The next action component and context to be used for the next generation attempt.
FUNC select_from_failure
.value iff the loop budget is exhausted and no success.
Args:
sampled_actions: List of actions that have been executed (without success).sampled_results: List of (unsuccessful) generation results for these actions.sampled_val: List of validation results for the results.
- The index of the result that should be selected as
.value.
FUNC sample
action: The action object to be sampled.context: The context to be passed to the sampling strategy.backend: The backend used for generating samples.requirements: List of requirements to test against (merged with global requirements).validation_ctx: Optional context to use for validation. If None, validation_ctx = ctx.format: output format for structured outputs.model_options: model options to pass to the backend during generation / validation.tool_calls: True if tool calls should be used during this sampling strategy.show_progress: if true, a tqdm progress bar is used. Otherwise, messages will still be sent to flog.
- SamplingResult[S]: A result object indicating the success or failure of the sampling process.
AssertionError: Asserts that all required components (repair, select_from_failure, validate, and generate) are provided before proceeding with the sampling.ValueError: If aSAMPLING_LOOP_STARThook returns a non-positiveloop_budget.
CLASS RejectionSamplingStrategy
Simple rejection sampling strategy that just repeats the same call on failure.
Methods:
FUNC select_from_failure
sampled_actions: List of actions that have been executed (without success).sampled_results: List of (unsuccessful) generation results for these actions.sampled_val: List of validation results for the results.
- The index of the result that should be selected as
.value.
FUNC repair
old_ctx: The context WITHOUT the last action + output.new_ctx: The context including the last action + output.past_actions: List of actions that have been executed (without success).past_results: List of (unsuccessful) generation results for these actions.past_val: List of validation results for the results.
- The next action component and context to be used for the next generation attempt.
CLASS RepairTemplateStrategy
A sampling strategy that adds a repair string to the instruction object.
Methods:
FUNC select_from_failure
sampled_actions: List of actions that have been executed (without success).sampled_results: List of (unsuccessful) generation results for these actions.sampled_val: List of validation results for the results.
- The index of the result that should be selected as
.value.
FUNC repair
old_ctx: The context WITHOUT the last action + output.new_ctx: The context including the last action + output.past_actions: List of actions that have been executed (without success).past_results: List of (unsuccessful) generation results for these actions.past_val: List of validation results for the results.
- The next action component and context to be used for the next generation attempt.
CLASS MultiTurnStrategy
Rejection sampling strategy with (agentic) multi-turn repair.
Methods:
FUNC select_from_failure
sampled_actions: List of actions that have been executed (without success).sampled_results: List of (unsuccessful) generation results for these actions.sampled_val: List of validation results for the results.
- The index of the result that should be selected as
.value.
FUNC repair
old_ctx: The context WITHOUT the last action + output.new_ctx: The context including the last action + output.past_actions: List of actions that have been executed (without success).past_results: List of (unsuccessful) generation results for these actions.past_val: List of validation results for the results.
- The next action component and context to be used for the next generation attempt.