0
0
LangchainHow-ToBeginner ยท 3 min read

How to Use Partial Prompt in Langchain for Flexible Inputs

In Langchain, use PartialPromptTemplate to create prompt templates with some fixed parts and some dynamic parts filled later. This lets you reuse prompt structures while customizing specific inputs when running the chain.
๐Ÿ“

Syntax

The PartialPromptTemplate in Langchain allows you to fix some variables in a prompt template while leaving others open to be filled later. It wraps a PromptTemplate and takes a dictionary of partial variables.

Key parts:

  • template: The full prompt string with placeholders like {variable}.
  • input_variables: List of all variables in the template.
  • partial_variables: Dictionary of variables you want to fix now.

When you call format on the partial prompt, you only provide the remaining variables.

python
from langchain.prompts import PromptTemplate, PartialPromptTemplate

# Define full prompt template with variables
full_template = "Write a {adjective} story about {topic}."

# Create PromptTemplate
prompt = PromptTemplate(template=full_template, input_variables=["adjective", "topic"])

# Create PartialPromptTemplate fixing 'adjective'
partial_prompt = PartialPromptTemplate(prompt=prompt, partial_variables={"adjective": "funny"})

# Later, provide only 'topic' when formatting
final_prompt = partial_prompt.format(topic="cats")
print(final_prompt)
Output
Write a funny story about cats.
๐Ÿ’ป

Example

This example shows how to create a partial prompt fixing the adjective, then use it to generate different prompts by providing only the topic each time.

python
from langchain.prompts import PromptTemplate, PartialPromptTemplate

# Full prompt with two variables
full_template = "Write a {adjective} story about {topic}."
prompt = PromptTemplate(template=full_template, input_variables=["adjective", "topic"])

# Fix adjective to 'exciting'
partial_prompt = PartialPromptTemplate(prompt=prompt, partial_variables={"adjective": "exciting"})

# Use partial prompt with different topics
print(partial_prompt.format(topic="space travel"))
print(partial_prompt.format(topic="robots"))
Output
Write an exciting story about space travel. Write an exciting story about robots.
โš ๏ธ

Common Pitfalls

Common mistakes when using PartialPromptTemplate include:

  • Not including all variables in input_variables of the original PromptTemplate.
  • Trying to format the partial prompt without providing the remaining variables.
  • Confusing partial_variables with input_variables.

Always ensure the variables you fix are in partial_variables and the rest are provided when calling format.

python
from langchain.prompts import PromptTemplate, PartialPromptTemplate

# Incorrect: missing 'topic' in input_variables
full_template = "Write a {adjective} story about {topic}."
prompt = PromptTemplate(template=full_template, input_variables=["adjective"])

# This will cause an error because 'topic' is missing
try:
    partial_prompt = PartialPromptTemplate(prompt=prompt, partial_variables={"adjective": "funny"})
    print(partial_prompt.format(topic="dogs"))
except Exception as e:
    print(f"Error: {e}")

# Correct way
prompt_correct = PromptTemplate(template=full_template, input_variables=["adjective", "topic"])
partial_prompt_correct = PartialPromptTemplate(prompt=prompt_correct, partial_variables={"adjective": "funny"})
print(partial_prompt_correct.format(topic="dogs"))
Output
Error: Missing input variables: topic Write a funny story about dogs.
๐Ÿ“Š

Quick Reference

Tips for using PartialPromptTemplate:

  • Define a full prompt with all variables.
  • Use partial_variables to fix some variables early.
  • Provide remaining variables when formatting the partial prompt.
  • Use partial prompts to reuse prompt structures with different inputs.
โœ…

Key Takeaways

Use PartialPromptTemplate to fix some variables in a prompt and fill others later.
Always list all variables in the original PromptTemplate's input_variables.
Provide only the remaining variables when formatting a partial prompt.
Partial prompts help reuse prompt templates with flexible inputs.
Check for missing variables to avoid runtime errors.