@@ -163,9 +163,10 @@ A template can be a list of `ChatMessage` objects, or a special string, as shown
163163
164164It constructs prompts using static or dynamic templates, which you can update for each pipeline run.
165165
166- Template variables in the template are optional unless specified otherwise.
167- If an optional variable isn't provided, it defaults to an empty string. Use ` variable ` and ` required_variables `
168- to define input types and required variables.
166+ Template variables in the template are required by default. To make any subset of variables optional,
167+ set ` required_variables ` to an explicit list of the variables that should remain required; any variable
168+ not listed becomes optional and defaults to an empty string when missing.
169+ Set ` required_variables ` to ` None ` to mark every variable as optional.
169170
170171### Usage examples
171172
@@ -266,7 +267,7 @@ builder.run(user_name="John", images=images)
266267``` python
267268__init__ (
268269 template: list[ChatMessage] | str | None = None ,
269- required_variables: list[str ] | Literal[" *" ] | None = None ,
270+ required_variables: list[str ] | Literal[" *" ] | None = " * " ,
270271 variables: list[str ] | None = None ,
271272) -> None
272273```
@@ -279,8 +280,10 @@ Constructs a ChatPromptBuilder component.
279280 renders the prompt with the provided variables. Provide the template in either
280281 the ` init ` method` or the ` run\` method.
281282- ** required_variables** (<code >list\[ str\] | Literal[ '\* '] | None</code >) – List variables that must be provided as input to ChatPromptBuilder.
282- If a variable listed as required is not provided, an exception is raised.
283- If set to ` "*" ` , all variables found in the prompt are required. Optional.
283+ Defaults to ` "*" ` , which marks every variable found in the prompt as required.
284+ Pass an explicit list to only require a subset of the variables; any variable not listed becomes
285+ optional and is replaced with an empty string in the rendered prompt when missing.
286+ Set to ` None ` to mark every variable as optional.
284287- ** variables** (<code >list\[ str\] | None</code >) – List input variables to use in prompt templates instead of the ones inferred from the
285288 ` template ` parameter. For example, to use more variables during prompt engineering than the ones present
286289 in the default template, you can provide them here.
@@ -353,8 +356,9 @@ Deserialize this component from a dictionary.
353356Renders a prompt filling in any variables so that it can send it to a Generator.
354357
355358The prompt uses Jinja2 template syntax.
356- The variables in the default template are used as PromptBuilder's input and are all optional.
357- If they're not provided, they're replaced with an empty string in the rendered prompt.
359+ The variables in the default template are used as PromptBuilder's input and are all required by default.
360+ To make any subset of variables optional, set ` required_variables ` to an explicit list of the variables that
361+ should remain required. Optional variables are replaced with an empty string in the rendered prompt.
358362To try out different prompts, you can replace the prompt template at runtime by
359363providing a template for each pipeline run invocation.
360364
@@ -377,12 +381,12 @@ builder.run(target_language="spanish", snippet="I can't speak spanish.")
377381#### In a Pipeline
378382
379383This is an example of a RAG pipeline where PromptBuilder renders a custom prompt template and fills it
380- with the contents of the retrieved documents and a query. The rendered prompt is then sent to a Generator .
384+ with the contents of the retrieved documents and a query. The rendered prompt is then sent to a ChatGenerator .
381385
382386``` python
383387from haystack import Pipeline, Document
384388from haystack.utils import Secret
385- from haystack.components.generators import OpenAIGenerator
389+ from haystack.components.generators.chat import OpenAIChatGenerator
386390from haystack.components.builders.prompt_builder import PromptBuilder
387391
388392# in a real world use case documents could come from a retriever, web, or any other source
@@ -399,7 +403,7 @@ prompt_template = """
399403 """
400404p = Pipeline()
401405p.add_component(instance = PromptBuilder(template = prompt_template), name = " prompt_builder" )
402- p.add_component(instance = OpenAIGenerator (api_key = Secret.from_env_var(" OPENAI_API_KEY" )), name = " llm" )
406+ p.add_component(instance = OpenAIChatGenerator (api_key = Secret.from_env_var(" OPENAI_API_KEY" )), name = " llm" )
403407p.connect(" prompt_builder" , " llm" )
404408
405409question = " Where does Joe live?"
@@ -480,7 +484,7 @@ Use `template_variables` to overwrite pipeline variables (such as documents) as
480484``` python
481485__init__ (
482486 template: str ,
483- required_variables: list[str ] | Literal[" *" ] | None = None ,
487+ required_variables: list[str ] | Literal[" *" ] | None = " * " ,
484488 variables: list[str ] | None = None ,
485489) -> None
486490```
@@ -492,12 +496,12 @@ Constructs a PromptBuilder component.
492496- ** template** (<code >str</code >) – A prompt template that uses Jinja2 syntax to add variables. For example:
493497 ` "Summarize this document: {{ documents[0].content }}\nSummary:" `
494498 It's used to render the prompt.
495- The variables in the default template are input for PromptBuilder and are all optional,
496- unless explicitly specified.
497- If an optional variable is not provided, it's replaced with an empty string in the rendered prompt.
499+ The variables in the default template are input for PromptBuilder and are all required by default.
498500- ** required_variables** (<code >list\[ str\] | Literal[ '\* '] | None</code >) – List variables that must be provided as input to PromptBuilder.
499- If a variable listed as required is not provided, an exception is raised.
500- If set to ` "*" ` , all variables found in the prompt are required. Optional.
501+ Defaults to ` "*" ` , which marks every variable found in the prompt as required.
502+ Pass an explicit list to only require a subset of the variables; any variable not listed becomes
503+ optional and is replaced with an empty string in the rendered prompt when missing.
504+ Set to ` None ` to mark every variable as optional.
501505- ** variables** (<code >list\[ str\] | None</code >) – List input variables to use in prompt templates instead of the ones inferred from the
502506 ` template ` parameter. For example, to use more variables during prompt engineering than the ones present
503507 in the default template, you can provide them here.
0 commit comments