{"id":8967,"library":"dynamicprompts","title":"Dynamic Prompts","description":"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.","status":"active","version":"0.31.0","language":"en","source_language":"en","source_url":"https://github.com/adieyal/dynamicprompts","tags":["AI","Stable Diffusion","Prompt Engineering","NLP","Text-to-Image","Templating"],"install":[{"cmd":"pip install dynamicprompts","lang":"bash","label":"Basic Installation"},{"cmd":"pip install \"dynamicprompts[magicprompt, attentiongrabber]\"","lang":"bash","label":"With Optional Features (Magic Prompt, Attention Grabber)"}],"dependencies":[{"reason":"Provides enhanced prompt generation with AI-driven modifiers, typically requiring large model downloads on first use.","package":"magicprompt","optional":true},{"reason":"Emphasizes random phrases in prompts for systems supporting attention syntax, often leveraging libraries like spaCy for NLP.","package":"attentiongrabber","optional":true}],"imports":[{"symbol":"RandomPromptGenerator","correct":"from dynamicprompts.generators import RandomPromptGenerator"},{"symbol":"WildcardManager","correct":"from dynamicprompts.wildcards import WildcardManager"}],"quickstart":{"code":"from pathlib import Path\nfrom dynamicprompts.wildcards import WildcardManager\nfrom dynamicprompts.generators import RandomPromptGenerator\n\n# Define a directory for wildcards (optional, create if needed)\nWILDCARD_DIR = Path(\"./wildcards\")\nWILDCARD_DIR.mkdir(exist_ok=True)\n\n# Create a dummy wildcard file for demonstration\n(WILDCARD_DIR / \"colours.txt\").write_text(\"red\\ngreen\\nblue\")\n\n# Initialize WildcardManager and RandomPromptGenerator\nwm = WildcardManager(WILDCARD_DIR)\ngenerator = RandomPromptGenerator(wildcard_manager=wm)\n\n# Generate 5 random prompts using a template with a variant and a wildcard\ntemplate = \"A {flower|tree} in __colours__ hue.\"\nnum_prompts = 5\n\nprint(f\"Generating {num_prompts} prompts from template: '{template}'\")\nfor i, prompt in enumerate(generator.generate(template, num_prompts=num_prompts)):\n    print(f\"Prompt {i+1}: {prompt}\")","lang":"python","description":"This quickstart demonstrates how to set up `RandomPromptGenerator` and `WildcardManager` to create dynamic prompts. It uses both inline variants (`{option1|option2}`) and a simple wildcard file (`__colours__`) to show how diverse prompts can be generated from a single template. The `WILDCARD_DIR` is created locally, and a sample wildcard file is generated for immediate execution."},"warnings":[{"fix":"Be aware that the actual number of generated prompts might be less than `num_prompts` if the template's possible variations are exhausted. For precise control over combinatorial generation, consider using `CombinatorialPromptGenerator` and inspecting its `get_template_variants()` method or explicitly setting `max_prompts`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you have sufficient disk space and a stable internet connection for the initial download. If encountering CUDA errors, verify your system meets the VRAM requirements or consider running on a CPU if performance is not critical.","message":"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.","severity":"gotcha","affected_versions":"All versions with magicprompt dependency"},{"fix":"Carefully review template syntax. The library expects specific delimiters for variants (`{item1|item2}`) and wildcards (`__wildcard__`). Test your templates thoroughly with smaller `num_prompts` counts to catch issues early. Refer to the official syntax guide for details.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `dynamicprompts` with its optional dependencies using `pip install \"dynamicprompts[magicprompt, attentiongrabber]\"` or specifically for the feature you need (e.g., `pip install \"dynamicprompts[magicprompt]\"`).","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.","error":"ModuleNotFoundError: No module named 'dynamicprompts.samplers'"},{"fix":"Inspect 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.","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.","error":"Prompting eventually crashes / Prompts stop working after some iterations / Error while generating prompt"}]}