Repository: deepset-ai/haystack

Issue:
PromptNode has implicit debug enabled
**Describe the bug**
No matter what you define in your pipeline, PromptNode will always output the _debug JSON block with a prompts_used value containing the complete prompt sent to the LLM.

This is defined in the code here: https://github.com/deepset-ai/haystack/blob/3272e2b9fec70ca769eb752b97f3ea284563fd7c/haystack/nodes/prompt/prompt_node.py#L677

It seems also that PromptNode has no way to define whether debug is enabled or not.

**Expected behavior**
When you enable/disable debug globally, it should apply to all nodes.

**FAQ Check**
- [X] Have you had a look at [our new FAQ page](https://docs.haystack.deepset.ai/docs/faq)?


Discussion:
Thank you @recrudesce for creating this issue. We should indeed add the _debug output if and only if the debug parameter is set to true, either globally or for this specific node via parameters.
The parameters that are passed in a pipeline's run method are passed on to the nodes in the following method: https://github.com/deepset-ai/haystack/blob/b6371c95a81c2fd723ae64a3c7ffd9ae5fe85870/haystack/nodes/base.py#L228

In the PromptNode class, we should check whether debug is set to True before adding `"_debug": {"prompts_used": prompt_collector}`to the output dictionary. Feel free to open a PR with that change if you would like to help us here and contribute. Would be much appreciated! 🙂 


Resolving patch:
diff --git a/haystack/nodes/prompt/prompt_node.py b/haystack/nodes/prompt/prompt_node.py
--- a/haystack/nodes/prompt/prompt_node.py
+++ b/haystack/nodes/prompt/prompt_node.py
@@ -420,6 +420,7 @@ def __init__(
         devices: Optional[List[Union[str, torch.device]]] = None,
         stop_words: Optional[List[str]] = None,
         top_k: int = 1,
+        debug: Optional[bool] = False,
         model_kwargs: Optional[Dict] = None,
     ):
         """
@@ -452,6 +453,8 @@ def __init__(
         self.prompt_model: PromptModel
         self.stop_words: Optional[List[str]] = stop_words
         self.top_k: int = top_k
+        self.debug = debug
+
         if isinstance(self.default_prompt_template, str) and not self.is_supported_template(
             self.default_prompt_template
         ):
@@ -681,11 +684,11 @@ def run(
         results = self(prompt_collector=prompt_collector, **invocation_context)
 
         invocation_context[self.output_variable] = results
-        final_result: Dict[str, Any] = {
-            self.output_variable: results,
-            "invocation_context": invocation_context,
-            "_debug": {"prompts_used": prompt_collector},
-        }
+        final_result: Dict[str, Any] = {self.output_variable: results, "invocation_context": invocation_context}
+
+        if self.debug:
+            final_result["_debug"] = {"prompts_used": prompt_collector}
+
         return final_result, "output_1"
 
     def run_batch(  # type: ignore


Test patch:
diff --git a/test/prompt/test_prompt_node.py b/test/prompt/test_prompt_node.py
--- a/test/prompt/test_prompt_node.py
+++ b/test/prompt/test_prompt_node.py
@@ -466,6 +466,31 @@ def test_simple_pipeline_with_topk(prompt_model):
     assert len(result["results"]) == 2
 
 
+@pytest.mark.integration
+@pytest.mark.parametrize("prompt_model", ["hf"], indirect=True)
+def test_prompt_node_no_debug(prompt_model):
+    """Pipeline with PromptNode should not generate debug info if debug is false."""
+
+    node = PromptNode(prompt_model, default_prompt_template="question-generation", top_k=2)
+    pipe = Pipeline()
+    pipe.add_node(component=node, name="prompt_node", inputs=["Query"])
+
+    # debug explicitely False
+    result = pipe.run(query="not relevant", documents=[Document("Berlin is the capital of Germany")], debug=False)
+    assert result.get("_debug", "No debug info") == "No debug info"
+
+    # debug None
+    result = pipe.run(query="not relevant", documents=[Document("Berlin is the capital of Germany")], debug=None)
+    assert result.get("_debug", "No debug info") == "No debug info"
+
+    # debug True
+    result = pipe.run(query="not relevant", documents=[Document("Berlin is the capital of Germany")], debug=True)
+    assert (
+        result["_debug"]["prompt_node"]["runtime"]["prompts_used"][0]
+        == "Given the context please generate a question. Context: Berlin is the capital of Germany; Question:"
+    )
+
+
 @pytest.mark.integration
 @pytest.mark.parametrize("prompt_model", ["hf", "openai", "azure"], indirect=True)
 def test_complex_pipeline_with_qa(prompt_model):