Access Multiple Previous Steps Output

This example demonstrates **Workflows 2.0** advanced data flow capabilities

This example demonstrates Workflows 2.0 shows how to:

  1. Access outputs from specific named steps (get_step_content())
  2. Aggregate all previous outputs (get_all_previous_content())
  3. Create comprehensive reports by combining multiple research sources

Key Features:

  • Step Output Access: Retrieve data from any previous step by name or collectively.
  • Custom Reporting: Combine and analyze outputs from parallel or sequential steps.
  • Streaming Support: Real-time updates during execution.
1from kern.agent.agent import Agent
2from kern.tools.hackernews import HackerNewsTools
3from kern.tools.yfinance import YFinanceTools
4from kern.workflow.step import Step
5from kern.workflow.types import StepInput, StepOutput
6from kern.workflow.workflow import Workflow
7
8# Define the research agents
9hackernews_agent = Agent(
10 name="HackerNews Researcher",
11 instructions="You are a researcher specializing in finding the latest tech news and discussions from Hacker News. Focus on startup trends, programming topics, and tech industry insights.",
12 tools=[HackerNewsTools()],
13)
14
15finance_agent = Agent(
16 name="Finance Researcher",
17 instructions="You are a finance researcher. Search for stock data, market trends, and financial information to gather detailed insights.",
18 tools=[YFinanceTools()],
19)
20
21reasoning_agent = Agent(
22 name="Reasoning Agent",
23 instructions="You are an expert analyst who creates comprehensive reports by analyzing and synthesizing information from multiple sources. Create well-structured, insightful reports.",
24)
25
26# Create the research steps
27research_hackernews = Step(
28 name="research_hackernews",
29 agent=hackernews_agent,
30 description="Research latest tech trends from Hacker News",
31)
32
33research_finance = Step(
34 name="research_finance",
35 agent=finance_agent,
36 description="Research financial data and market trends",
37)
38
39# Custom function step that has access to ALL previous step outputs
40
41
42def create_comprehensive_report(step_input: StepInput) -> StepOutput:
43 """
44 Custom function that creates a report using data from multiple previous steps.
45 This function has access to ALL previous step outputs and the original workflow message.
46 """
47
48 # Access original workflow input
49 original_topic = step_input.input or ""
50
51 # Access specific step outputs by name
52 hackernews_data = step_input.get_step_content("research_hackernews") or ""
53 finance_data = step_input.get_step_content("research_finance") or ""
54
55 # Or access ALL previous content
56 _ = step_input.get_all_previous_content()
57
58 # Create a comprehensive report combining all sources
59 report = f"""
60 # Comprehensive Research Report: {original_topic}
61
62 ## Executive Summary
63 Based on research from HackerNews and web sources, here's a comprehensive analysis of {original_topic}.
64
65 ## HackerNews Insights
66 {hackernews_data[:500]}...
67
68 ## Finance Research Findings
69 {finance_data[:500]}...
70 """
71
72 return StepOutput(
73 step_name="comprehensive_report", content=report.strip(), success=True
74 )
75
76
77comprehensive_report_step = Step(
78 name="comprehensive_report",
79 executor=create_comprehensive_report,
80 description="Create comprehensive report from all research sources",
81)
82
83# Final reasoning step using reasoning agent
84reasoning_step = Step(
85 name="final_reasoning",
86 agent=reasoning_agent,
87 description="Apply reasoning to create final insights and recommendations",
88)
89
90workflow = Workflow(
91 name="Enhanced Research Workflow",
92 description="Multi-source research with custom data flow and reasoning",
93 steps=[
94 research_hackernews,
95 research_finance,
96 comprehensive_report_step, # Has access to both previous steps
97 reasoning_step, # Gets the last step output (comprehensive report)
98 ],
99)
100
101if __name__ == "__main__":
102 workflow.print_response(
103 "Latest developments in artificial intelligence and machine learning",
104 stream=True,
105 )