How to beginner · 3 min read

Negative prompts in Stable Diffusion

Quick answer
In Stable Diffusion, negative prompts specify undesired features to avoid in generated images, improving output quality by guiding the model away from unwanted content. You pass negative prompts as a separate parameter in the generation API or pipeline to explicitly suppress certain attributes or artifacts.

PREREQUISITES

  • Python 3.8+
  • pip install diffusers transformers torch
  • Access to a Stable Diffusion model checkpoint or API

Setup

Install the diffusers library and dependencies to run Stable Diffusion locally or in a cloud environment.

Use the following command to install:

bash
pip install diffusers transformers torch

Step by step

This example shows how to generate an image with a negative prompt using the diffusers pipeline. The negative prompt helps suppress unwanted elements like "blurry" or "low quality".

python
from diffusers import StableDiffusionPipeline
import torch
import os

# Load the Stable Diffusion pipeline
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16
).to("cuda")

# Define positive and negative prompts
prompt = "a beautiful landscape with mountains and a river"
negative_prompt = "blurry, low quality, watermark, text"

# Generate image with negative prompt
image = pipe(prompt=prompt, negative_prompt=negative_prompt, guidance_scale=7.5).images[0]

# Save the output image
image.save("output.png")
print("Image saved as output.png")
output
Image saved as output.png

Common variations

You can use negative prompts with different Stable Diffusion models or APIs. For example, when using a hosted API, pass the negative prompt as a separate parameter in the request payload.

Async generation or streaming is less common for image generation but can be implemented with custom wrappers.

python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="stable-diffusion-2-base",
    messages=[{"role": "user", "content": "Generate an image of a futuristic city"}],
    tools=[{
        "type": "image_generation",
        "parameters": {
            "prompt": "futuristic city skyline at sunset",
            "negative_prompt": "fog, blurry, low resolution"
        }
    }]
)

print(response.choices[0].message.content)

Troubleshooting

  • If the generated image still contains unwanted artifacts, try expanding your negative prompt with more descriptive terms.
  • Ensure your environment has a compatible GPU and the correct torch version for float16 support.
  • If you get errors loading the model, verify your internet connection and model checkpoint availability.

Key Takeaways

  • Use negative_prompt to explicitly exclude unwanted features in Stable Diffusion outputs.
  • Negative prompts improve image quality by guiding the model away from artifacts like blur or watermarks.
  • The diffusers library supports negative prompts natively in the pipeline call.
  • Expand negative prompts with specific terms if undesired elements persist.
  • Negative prompts can be passed similarly in hosted API calls as a separate parameter.
Verified 2026-04 · runwayml/stable-diffusion-v1-5, stable-diffusion-2-base
Verify ↗