Step with function
This example demonstrates how to use named steps with custom function executors.
This example demonstrates Workflows using named Step objects with custom function executors. This pattern combines the benefits of named steps with the flexibility of custom functions, allowing for sophisticated data processing within structured workflow steps.
When to use: When you need named step organization but want custom logic that goes beyond what agents/teams provide. Ideal for complex data processing, multi-step operations, or when you need to orchestrate multiple agents within a single step.
1from kern.agent import Agent2from kern.db.sqlite import SqliteDb3from kern.models.openai import OpenAIResponses4from kern.team import Team5from kern.tools.hackernews import HackerNewsTools6from kern.tools.yfinance import YFinanceTools7from kern.workflow.step import Step, StepInput, StepOutput8from kern.workflow.workflow import Workflow910# Define agents11hackernews_agent = Agent(12 name="Hackernews Agent",13 model=OpenAIResponses(id="gpt-5.2"),14 tools=[HackerNewsTools()],15 instructions="Extract key insights and content from Hackernews posts",16)1718finance_agent = Agent(19 name="Finance Agent",20 model=OpenAIResponses(id="gpt-5.2"),21 tools=[YFinanceTools()],22 instructions="Get stock prices and financial data",23)2425# Define research team for complex analysis26research_team = Team(27 name="Research Team",28 members=[hackernews_agent, finance_agent],29 instructions="Analyze content and create comprehensive social media strategy",30)3132content_planner = Agent(33 name="Content Planner",34 model=OpenAIResponses(id="gpt-5.2"),35 instructions=[36 "Plan a content schedule over 4 weeks for the provided topic and research content",37 "Ensure that I have posts for 3 posts per week",38 ],39)404142def custom_content_planning_function(step_input: StepInput) -> StepOutput:43 """44 Custom function that does intelligent content planning with context awareness45 """46 message = step_input.input47 previous_step_content = step_input.previous_step_content4849 # Create intelligent planning prompt50 planning_prompt = f"""51 STRATEGIC CONTENT PLANNING REQUEST:5253 Core Topic: {message}5455 Research Results: {previous_step_content[:500] if previous_step_content else "No research results"}5657 Planning Requirements:58 1. Create a comprehensive content strategy based on the research59 2. Leverage the research findings effectively60 3. Identify content formats and channels61 4. Provide timeline and priority recommendations62 5. Include engagement and distribution strategies6364 Please create a detailed, actionable content plan.65 """6667 try:68 response = content_planner.run(planning_prompt)6970 enhanced_content = f"""71 ## Strategic Content Plan7273 **Planning Topic:** {message}7475 **Research Integration:** {"Research-based" if previous_step_content else "No research foundation"}7677 **Content Strategy:**78 {response.content}7980 **Custom Planning Enhancements:**81 - Research Integration: {"High" if previous_step_content else "Baseline"}82 - Strategic Alignment: Optimized for multi-channel distribution83 - Execution Ready: Detailed action items included84 """.strip()8586 return StepOutput(content=enhanced_content)8788 except Exception as e:89 return StepOutput(90 content=f"Custom content planning failed: {str(e)}",91 success=False,92 )939495# Define steps using different executor types9697research_step = Step(98 name="Research Step",99 team=research_team,100)101102content_planning_step = Step(103 name="Content Planning Step",104 executor=custom_content_planning_function,105)106107108# Define and use examples109if __name__ == "__main__":110 content_creation_workflow = Workflow(111 name="Content Creation Workflow",112 description="Automated content creation with custom execution options",113 db=SqliteDb(114 session_table="workflow_session",115 db_file="tmp/workflow.db",116 ),117 # Define the sequence of steps118 # First run the research_step, then the content_planning_step119 # You can mix and match agents, teams, and even regular python functions directly as steps120 steps=[research_step, content_planning_step],121 )122 content_creation_workflow.print_response(123 input="AI trends in 2024",124 markdown=True,125 )126127 print("\n" + "=" * 60 + "\n")