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.
Python code
17 linesimport 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
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
- Use `random.SystemRandom` for cryptographic randomness if security matters.
- 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
More from AI & LLM integration patterns
- Cache LLM Completions by Hashing the Prompt in Python easy
- Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo easy
- Circuit Breaker Pattern in Python for LLM API Calls medium
- Cosine Similarity to Retrieve Top K Chunks in Python easy
- Demonstrate Prompt Injection Bypass in Python easy
- How to Accumulate Streamed Tokens into a Final String in Python easy
Keep learning
Related tutorials and quizzes for this topic.