Conditional Branching Workflow
This example demonstrates **Workflows 2.0** router pattern for intelligent, content-based workflow routing.
This example demonstrates Workflows 2.0 to dynamically select the best execution path based on input analysis, enabling adaptive workflows that choose optimal strategies per topic.
When to use: When you need mutually exclusive execution paths based on business logic. Ideal for topic-specific workflows, expertise routing, or when different subjects require completely different processing strategies. Unlike Conditions which can trigger multiple parallel paths, Router selects exactly one path.
1from typing import List23from kern.agent.agent import Agent4from kern.tools.hackernews import HackerNewsTools5from kern.tools.yfinance import YFinanceTools6from kern.workflow.router import Router7from kern.workflow.step import Step8from kern.workflow.types import StepInput9from kern.workflow.workflow import Workflow1011# Define the research agents12hackernews_agent = Agent(13 name="HackerNews Researcher",14 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.",15 tools=[HackerNewsTools()],16)1718finance_agent = Agent(19 name="Finance Researcher",20 instructions="You are a finance researcher. Search for stock data, market trends, and financial information to gather detailed insights.",21 tools=[YFinanceTools()],22)2324content_agent = Agent(25 name="Content Publisher",26 instructions="You are a content creator who takes research data and creates engaging, well-structured articles. Format the content with proper headings, bullet points, and clear conclusions.",27)2829# Create the research steps30research_hackernews = Step(31 name="research_hackernews",32 agent=hackernews_agent,33 description="Research latest tech trends from Hacker News",34)3536research_finance = Step(37 name="research_finance",38 agent=finance_agent,39 description="Research financial data and market trends",40)4142publish_content = Step(43 name="publish_content",44 agent=content_agent,45 description="Create and format final content for publication",46)474849# Now returns Step(s) to execute50def research_router(step_input: StepInput) -> List[Step]:51 """52 Decide which research method to use based on the input topic.53 Returns a list containing the step(s) to execute.54 """55 # Use the original workflow message if this is the first step56 topic = step_input.previous_step_content or step_input.input or ""57 topic = topic.lower()5859 # Check if the topic is tech/startup related - use HackerNews60 tech_keywords = [61 "startup",62 "programming",63 "ai",64 "machine learning",65 "software",66 "developer",67 "coding",68 "tech",69 "silicon valley",70 "venture capital",71 "cryptocurrency",72 "blockchain",73 "open source",74 "github",75 ]7677 if any(keyword in topic for keyword in tech_keywords):78 print(f"Tech topic detected: Using HackerNews research for '{topic}'")79 return [research_hackernews]80 else:81 print(f"Finance topic detected: Using finance research for '{topic}'")82 return [research_finance]838485workflow = Workflow(86 name="Intelligent Research Workflow",87 description="Automatically selects the best research method based on topic, then publishes content",88 steps=[89 Router(90 name="research_strategy_router",91 selector=research_router,92 choices=[research_hackernews, research_finance],93 description="Intelligently selects research method based on topic",94 ),95 publish_content,96 ],97)9899if __name__ == "__main__":100 workflow.print_response(101 "Latest developments in artificial intelligence and machine learning"102 )