How to randomly assign a prompt variant to each key in Python

Randomly pick one variant from a list for each prompt key, useful for A/B testing message variations.

Easy Python 3.9+ Aug 9, 2026 AI & LLM integration patterns 14 views 0 copies

Python code

17 lines
Python 3.9+
import random

def assign_prompt_variant(prompts: dict[str, list[str]]) -> dict[str, str]:
    """Assign a random prompt variant to each prompt key."""
    return {key: random.choice(variants) for key, variants in prompts.items()}

if __name__ == "__main__":
    prompt_bank = {
        "greeting": ["Hello!", "Hi there!", "Hey!"],
        "farewell": ["Goodbye!", "See you later!", "Take care!"],
        "thanks": ["Thanks!", "Thank you!", "Much appreciated!"]
    }

    random.seed(42)  # For reproducible output
    assigned = assign_prompt_variant(prompt_bank)
    for key, variant in assigned.items():
        print(f"{key}: {variant}")

Output

stdout
greeting: Hi there!
farewell: Take care!
thanks: Thank you!

How it works

The function assign_prompt_variant takes a dictionary where each key maps to a list of possible variants. It uses random.choice to select one variant randomly for each key, returning a new dictionary with the same keys but single chosen strings. Seeding the random generator with random.seed(42) ensures the same output across runs, which is helpful for testing or reproducible experiments. This pattern is common in AI workflows where you want to vary system prompts for A/B testing without changing the core logic.

Common mistakes

  • Forgetting to seed random when reproducibility is needed for debugging or A/B test consistency.
  • Assuming the order of dictionary items matters; random choice is per key, not per list position.
  • Not handling empty variant lists, which would raise an IndexError.

Variations

  1. Use `random.SystemRandom` for cryptographic randomness if security matters.
  2. Assign a fixed variant based on a hash of a user ID for consistent assignment across sessions.

Real-world use cases

  • Serving multiple versions of a chatbot greeting to users to test engagement metrics.
  • Rotating error message phrasing in a customer support tool to reduce repetition.
  • Varying system prompt instructions in an LLM pipeline to evaluate output quality.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from AI & LLM integration patterns

Related tutorials and quizzes for this topic.