Prompt Chaining

In this example, we’ll build a blog content generation Workflow that creates engaging, targeted content through a series of prompts. The Workflow takes an industry and target audience as inputs and produces a complete blog post through the following steps:

  1. TopicResearch: Generates and rates potential blog topics based on audience relevance and SEO potential
  2. OutlineCreation: Creates a detailed outline for the highest-rated topic
  3. ContentGeneration: Writes a complete blog post following the outline
  4. FinalOutput: Returns the final blog content
workflow.py
1## Graph Definition
2class Workflow(BaseWorkflow[Inputs, BaseState]):
3 graph = TopicResearch >> OutlineCreation >> ContentGeneration >> FinalOutput
4
5 class Outputs(BaseWorkflow.Outputs):
6 final_output = FinalOutput.Outputs.value
7
8## Running it
9workflow = Workflow()
10terminal_event = workflow.run(
11 inputs=Inputs(
12 audience="Working professionals targeting midsize businesses",
13 industry="Marketing Technology"
14 )
15)
16
17## Output:
18print(terminal_event.outputs.final_output)
19
20"""
21[Output will be a complete blog post tailored to the specified industry and audience]
22"""

Which corresponds to a Workflow graph like this:

Basic Prompt Chain

Setup

Install Vellum

$pip install vellum-ai

Create your Project

Structure your project like this:

1basic_prompt_chain/
2├── workflow.py
3├── inputs.py
4├── sandbox.py
5├── __init__.py
6└── nodes/
7 ├── __init__.py
8 ├── topic_research.py
9 ├── outline_creation.py
10 ├── content_generation.py
11 └── final_output.py

Define Workflow Inputs

inputs.py
1from vellum.workflows.inputs import BaseInputs
2
3class Inputs(BaseInputs):
4 audience: str
5 industry: str

The Workflow takes two inputs: the target audience and industry. These will be used to generate relevant, targeted content throughout the chain.

Build the Nodes

1

Topic Research

This node generates and rates potential blog topics based on the specified industry and audience.

nodes/topic_research.py
1class TopicResearch(InlinePromptNode):
2 ml_model = "gpt-4o-mini"
3 blocks = [
4 ChatMessagePromptBlock(
5 chat_role="SYSTEM",
6 blocks=[
7 JinjaPromptBlock(
8 template="""\
9You are a content strategist who identifies engaging blog topics for businesses to help boost their online presence and SEO performance.
10
11You will suggest 3 blog topics for:
12Industry: {{ industry }}
13Target Audience: {{ audience }}
14
15For each topic, provide:
161. A compelling title
172. A brief description of the angle
183. A rating from 1-10 with explanation
19
20No preamble/postamble\
21"""
22 )
23 ],
24 ),
25 ]
26 prompt_inputs = {
27 "audience": Inputs.audience,
28 "industry": Inputs.industry,
29 }
2

Outline Creation

Takes the rated topics and creates a detailed outline for the highest-rated one.

nodes/outline_creation.py
1class OutlineCreation(InlinePromptNode):
2 ml_model = "gpt-4o-mini"
3 blocks = [
4 ChatMessagePromptBlock(
5 chat_role="SYSTEM",
6 blocks=[
7 JinjaPromptBlock(
8 template="""\
9You are a content outline specialist.
10You will receive a list of <topics> with ratings to create a detailed outline targeted at {{ audience }}.
11
12You will:
131. Select the highest-rated topic(s)
142. Create a detailed outline for a 1000-word blog post
153. Include sections for examples and actionable tips
164. Add placeholders for statistics or case studies
17
18No preamble/postamble\
19"""
20 )
21 ],
22 ),
23 ]
24 prompt_inputs = {
25 "audience": Inputs.audience,
26 "topics": TopicResearch.Outputs.text,
27 }
3

Content Generation

Transforms the outline into a complete blog post.

nodes/content_generation.py
1class ContentGeneration(InlinePromptNode):
2 ml_model = "gpt-4o-mini"
3 blocks = [
4 ChatMessagePromptBlock(
5 chat_role="SYSTEM",
6 blocks=[
7 JinjaPromptBlock(
8 template="""\
9You are an expert content writer specializing in {{ industry }} for {{ audience }}.
10
11Guidelines:
121. Select the highest-rated topic
132. Write in a conversational tone
143. Follow the outline exactly
154. Include examples and statistics
165. Break up text with subheadings
176. Target length: 600 words
187. Include introduction and call-to-action
19
20No preamble/postamble\
21"""
22 )
23 ],
24 ),
25 ]
26 prompt_inputs = {
27 "outline": OutlineCreation.Outputs.text,
28 "industry": Inputs.industry,
29 "audience": Inputs.audience,
30 }
4

Final Output

Returns the generated content as the Workflow output.

nodes/final_output.py
1class FinalOutput(FinalOutputNode[BaseState, str]):
2 class Outputs(FinalOutputNode.Outputs):
3 value = ContentGeneration.Outputs.text

Instantiate the Graph and Invoke it

1

Define the Graph and its Outputs

workflow.py
1from vellum.workflows import BaseWorkflow
2from vellum.workflows.state import BaseState
3
4from .inputs import Inputs
5from .nodes.topic_research import TopicResearch
6from .nodes.outline_creation import OutlineCreation
7from .nodes.content_generation import ContentGeneration
8from .nodes.final_output import FinalOutput
9
10class Workflow(BaseWorkflow[Inputs, BaseState]):
11 graph = TopicResearch >> OutlineCreation >> ContentGeneration >> FinalOutput
12
13 class Outputs(BaseWorkflow.Outputs):
14 final_output = FinalOutput.Outputs.value

You can output directly from the ContentGeneration node. Here we’ve used the FinalOutput node for clarity when pushed to the UI.

2

Instantiate the Workflow

1## From any file / function from which you want to reference the Workflow
2
3# Required import (the file imported from depends on your folder structure)
4# from .workflow import Workflow
5
6workflow = Workflow()
3

Invoke the Workflow

1## From any file / function from which you want to run the Workflow
2
3# Required imports (the file imported from depends on your folder structure)
4# from .inputs import Inputs
5
6terminal_event = workflow.run(
7 inputs=Inputs(
8 audience="Working professionals targeting midsize businesses",
9 industry="Marketing Technology"
10 )
11)
12
13## Get the generated content:
14print(terminal_event.outputs.final_output)
15
16"""
17Example Output:
185 Essential MarTech Stack Optimization Strategies for 2024
19
20In today's rapidly evolving business landscape, having an efficient marketing technology stack is no longer optional—it's crucial for success. As marketing professionals targeting midsize businesses, you need to ensure your MarTech investments deliver maximum ROI while staying within budget constraints...
21
22[Rest of generated blog post content...]
23"""

Conclusion

We’ve built a prompt chain Workflow that generates blog content through a series of specialized prompts. Each node in the chain builds upon the previous one’s output, resulting in high-quality, targeted content. From here, you can:

  • Version control the Workflow with your codebase
  • Continue development in the Vellum UI
  • Deploy to Vellum
  • Add evaluation metrics to assess content quality
  • Expand the chain with additional steps like SEO optimization or image generation
Built with