Dynamic Prompts
Dynamic Prompts is a Python library designed for creating flexible and expressive prompt templates for text-to-image generators like Stable Diffusion, MidJourney, or Dall-e 2. It enables users to generate a multitude of unique prompts from a single template using features such as variants, wildcards, and combinatorial generation. The library is actively maintained, with its current version being 0.31.0, and receives regular updates.
Common errors
-
ModuleNotFoundError: No module named 'dynamicprompts.samplers'
cause This error typically occurs when attempting to import or use an optional feature (like specific samplers, Magic Prompt, or Attention Grabber) that was not included during the initial `pip install dynamicprompts` command.fixInstall `dynamicprompts` with its optional dependencies using `pip install "dynamicprompts[magicprompt, attentiongrabber]"` or specifically for the feature you need (e.g., `pip install "dynamicprompts[magicprompt]"`). -
Prompting eventually crashes / Prompts stop working after some iterations / Error while generating prompt
cause Often caused by malformed or syntactically incorrect prompt templates. For example, a missing closing brace `}` or a typo in a wildcard name can lead to the parser failing after some initial successful attempts, especially if the problematic section is not hit immediately.fixInspect your prompt templates for any syntax errors, such as mismatched braces (`{}`), incorrect wildcard delimiters (`__wildcard__`), or invalid variant syntax (`{opt1|opt2}`). Test with a very simple template first to isolate the issue. Check GitHub issues for similar parsing problems.
Warnings
- gotcha The `num_prompts` argument in generator methods (e.g., `generate`) acts as an upper bound, not an exact count. If the number of unique combinations possible in your template is less than `num_prompts`, the generator will yield fewer prompts than requested. This is particularly relevant for combinatorial generation where unique combinations can be limited.
- gotcha Using optional features like 'Magic Prompt' can incur a significant initial overhead. The first time a Magic Prompt model is used, it will download a large (approx. 500MB) model file, which can consume time and require substantial VRAM, potentially leading to CUDA errors on systems with limited GPU memory.
- gotcha Subtle syntax errors in prompt templates (e.g., using `]` instead of `}` for variants, or incorrect wildcard formatting) can lead to parsing failures or unexpected prompt generation behavior. These errors might not be immediately obvious and can manifest as seemingly random crashes or incorrect output after some successful generations.
Install
-
pip install dynamicprompts -
pip install "dynamicprompts[magicprompt, attentiongrabber]"
Imports
- RandomPromptGenerator
from dynamicprompts.generators import RandomPromptGenerator
- WildcardManager
from dynamicprompts.wildcards import WildcardManager
Quickstart
from pathlib import Path
from dynamicprompts.wildcards import WildcardManager
from dynamicprompts.generators import RandomPromptGenerator
# Define a directory for wildcards (optional, create if needed)
WILDCARD_DIR = Path("./wildcards")
WILDCARD_DIR.mkdir(exist_ok=True)
# Create a dummy wildcard file for demonstration
(WILDCARD_DIR / "colours.txt").write_text("red\ngreen\nblue")
# Initialize WildcardManager and RandomPromptGenerator
wm = WildcardManager(WILDCARD_DIR)
generator = RandomPromptGenerator(wildcard_manager=wm)
# Generate 5 random prompts using a template with a variant and a wildcard
template = "A {flower|tree} in __colours__ hue."
num_prompts = 5
print(f"Generating {num_prompts} prompts from template: '{template}'")
for i, prompt in enumerate(generator.generate(template, num_prompts=num_prompts)):
print(f"Prompt {i+1}: {prompt}")