{"record":{"id":"0becd3cf1b3c9e0d","repo":"sgl-project/sglang","slug":"for-classifier-free-guidance-either-negative-pro","errorCode":null,"errorMessage":"For classifier-free guidance, either `negative_prompt` or `negative_prompt_embeds` must be provided","messagePattern":"For classifier-free guidance, either `negative_prompt` or `negative_prompt_embeds` must be provided","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines_core/stages/input_validation.py","lineNumber":333,"sourceCode":"            batch.num_inference_steps = (\n                server_args.pipeline_config.shape_num_inference_steps\n            )\n\n        # Ensure prompt is properly formatted (I2M can be image-only)\n        if (\n            server_args.pipeline_config.task_type != ModelTaskType.I2M\n            and batch.prompt is None\n            and batch.prompt_embeds is None\n        ):\n            raise ValueError(\"Either `prompt` or `prompt_embeds` must be provided\")\n\n        # Ensure negative prompt is properly formatted if using classifier-free guidance\n        if (\n            batch.do_classifier_free_guidance\n            and batch.negative_prompt is None\n            and batch.negative_prompt_embeds is None\n        ):\n            raise ValueError(\n                \"For classifier-free guidance, either `negative_prompt` or \"\n                \"`negative_prompt_embeds` must be provided\"\n            )\n\n        # Validate number of inference steps\n        if batch.num_inference_steps <= 0:\n            raise ValueError(\n                f\"Number of inference steps must be positive, but got {batch.num_inference_steps}\"\n            )\n\n        # Validate guidance scale if using classifier-free guidance\n        if batch.do_classifier_free_guidance and batch.guidance_scale < 0:\n            raise ValueError(\n                f\"Guidance scale must be positive, but got {batch.guidance_scale}\"\n            )\n\n        # Reject requests that do not enable CFG on a server launched with\n        # --enable-cfg-parallel. CFG-parallel splits cond/uncond across ranks,","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines_core/stages/input_validation.py#L315-L351","documentation":"When classifier-free guidance is enabled (do_classifier_free_guidance=True), the pipeline needs either negative_prompt or negative_prompt_embeds to build the unconditional branch. If both are None, CFG cannot be computed and validation fails fast.","triggerScenarios":"Enabling CFG (guidance_scale > 1 / cfg flags) but submitting requests with no negative_prompt field and no negative_prompt_embeds, or a server default that enables CFG while the client omits negative prompts.","commonSituations":"Clients that never send negative_prompt against a CFG-enabled server; toggling --disable-cfg or cfg-parallel flags so do_classifier_free_guidance flips true unexpectedly; programmatic batch construction omitting the negative fields.","solutions":["Send a negative_prompt (an empty string \"\" usually suffices) in each request when using CFG","Or supply negative_prompt_embeds if you precompute embeddings","If you do not want CFG, disable it so do_classifier_free_guidance is False (guidance_scale <= 1 or the server-side CFG flag off)"],"exampleFix":"// before\nout = pipe(prompt=\"a cat\", guidance_scale=7.5)\n\n// after\nout = pipe(prompt=\"a cat\", negative_prompt=\"\", guidance_scale=7.5)","handlingStrategy":"validation","validationCode":"if batch.do_classifier_free_guidance:\n    assert batch.negative_prompt is not None or batch.negative_prompt_embeds is not None, \\\n        \"CFG requires negative_prompt (empty string is fine)\"","typeGuard":"def cfg_inputs_valid(batch) -> bool:\n    return not batch.do_classifier_free_guidance or batch.negative_prompt is not None or batch.negative_prompt_embeds is not None","tryCatchPattern":null,"preventionTips":["Always send negative_prompt=\"\" when guidance_scale > 1","Centralize request-building defaults for CFG fields"],"tags":["cfg","negative-prompt","input-validation"],"backgroundTag":"required-argument-missing","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}