Simple Self-Critique Wrapper

Easy
Agents

Implement a self-critique wrapper that:

  1. Calls generate_func() to get initial output
  2. Calls critique_func(output) to evaluate
  3. If critique returns passed: False, regenerate with feedback
  4. Repeat up to max_iterations
  5. Return final result with history of all attempts

History Format: [{'output': ..., 'critique': {'passed': ..., 'feedback': ...}}, ...]

Constraints:

  • Always return after max_iterations even if not passed
  • passed=True stops early
  • Include all attempts in history

Examples

Example 1:
Input: with_self_critique(lambda: 'draft', lambda x: {'passed': True, 'feedback': 'good'}, 2)
Output: {'final_output': 'draft', 'iterations': 1, 'passed': True, 'history': [{'output': 'draft', 'critique': {'passed': True, 'feedback': 'good'}}]}
Explanation: Passes on first try, stops early
Example 2:
Input: with_self_critique(lambda: 'x', lambda x: {'passed': False, 'feedback': 'bad'}, 1)['passed']
Output: False
Explanation: Fails critique, hits max iterations, returns passed=False

Starter Code

def with_self_critique(generate_func, critique_func, max_iterations=2):
    """
    Wrapper that adds self-critique to any generation function.
    
    Args:
        generate_func: Function() -> output
        critique_func: Function(output) -> {'passed': bool, 'feedback': str}
        max_iterations: Maximum generation-critique cycles
    
    Returns:
        dict with 'final_output', 'iterations', 'passed', 'history'
    """
    # Your implementation here
    pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews