AUTOMATIC1111/stable-diffusion-webui · error · RuntimeError
Prompt S/R did not find {xs[0]} in prompt or negative prompt
Error message
Prompt S/R did not find {xs[0]} in prompt or negative prompt. What it means
Thrown by the Prompt S/R (search/replace) axis handler in the XYZ Grid script of AUTOMATIC1111's stable-diffusion-webui. Before replacing text, it verifies that the first value of the comma-separated axis list (the search term) actually occurs in either the prompt or the negative prompt. If the search token is absent from both, replacement is impossible, so it aborts immediately with a RuntimeError.
Source
Thrown at scripts/xyz_grid.py:39
import re
from modules.ui_components import ToolButton
fill_values_symbol = "\U0001f4d2" # 📒
AxisInfo = namedtuple('AxisInfo', ['axis', 'values'])
def apply_field(field):
def fun(p, x, xs):
setattr(p, field, x)
return fun
def apply_prompt(p, x, xs):
if xs[0] not in p.prompt and xs[0] not in p.negative_prompt:
raise RuntimeError(f"Prompt S/R did not find {xs[0]} in prompt or negative prompt.")
p.prompt = p.prompt.replace(xs[0], x)
p.negative_prompt = p.negative_prompt.replace(xs[0], x)
def apply_order(p, x, xs):
token_order = []
# Initially grab the tokens from the prompt, so they can be replaced in order of earliest seen
for token in x:
token_order.append((p.prompt.find(token), token))
token_order.sort(key=lambda t: t[0])
prompt_parts = []
# Split the prompt up, taking out the tokens
for _, token in token_order:
View on GitHub (pinned to 82a973c043)
Solutions
- Make sure the first value of the Prompt S/R axis list appears exactly (case-sensitive substring) in the prompt or negative prompt, e.g. prompt contains 'a photo of a dog' and axis values are 'dog,cat'.
- Check for case and whitespace mismatches: 'Dog' will not match 'dog', and ' dog' will not match 'dog' after CSV splitting.
- If building requests programmatically, assert p.prompt contains xs[0] before launching the grid run.
- Use quotes around CSV fields containing commas so the search term is not split incorrectly.
Example fix
# before prompt = "a photo of a cat" axis_values = "dog,cat" # 'dog' not in prompt -> RuntimeError # after prompt = "a photo of a dog" axis_values = "dog,cat" # 'dog' found; replaced with 'cat' per cell
Defensive patterns
Strategy: validation
Validate before calling
# before running the grid, verify the S/R search term is present
def sr_axis_ok(prompt: str, negative_prompt: str, xs: list[str]) -> bool:
return xs[0] in prompt or xs[0] in negative_prompt
assert sr_axis_ok(p.prompt, p.negative_prompt, ["dog", "cat"]), \
f"Prompt S/R search term 'dog' missing from prompt and negative prompt" Type guard
def is_valid_sr_axis(prompt: str, negative_prompt: str, xs: list) -> bool:
"""True when the first Prompt S/R value occurs in prompt or negative_prompt."""
return isinstance(xs, list) and len(xs) > 0 and isinstance(xs[0], str) and \
(xs[0] in prompt or xs[0] in negative_prompt) Try / catch
try:
run_xyz_grid(p, axis_list)
except RuntimeError as e:
if "Prompt S/R did not find" in str(e):
log.warning("S/R token missing; fixing prompt and retrying")
p.prompt = p.prompt.replace(old_token, xs[0])
else:
raise Prevention
- Treat the first element of a Prompt S/R list as a required substring of the prompt; document this in team templates.
- Normalize whitespace and strip CSV fields before passing axis values.
- In automated pipelines, assert the search token is present before launching the run.
When it happens
Trigger: Adding an axis of type 'Prompt S/R' whose first list entry (e.g. "12345,67890" -> search term "12345") does not appear verbatim in the main prompt or negative prompt of the generation request. Common with batch scripts or API calls (/imgrun or --api with xyz_grid extension) where the prompt template was changed but the axis values were not.
Common situations: Typos or case differences between the search token and the prompt text; prompts built dynamically where the token is only inserted for some jobs; copying an axis list from another workflow whose prompt contained the token; leading/trailing whitespace mismatches after CSV parsing.
Related errors
- Unknown sampler: {x}
- Unknown checkpoint: {x}
- Sampler not found
- Invalid encoded image
- always on script {alwayson_script_name} not found
AI-assisted analysis of AUTOMATIC1111/stable-diffusion-webui@82a973c043 (2026-08-14).
Data as JSON: /api/errors/21e1311ec7c289ae.
Report an issue: GitHub.