Function instead of steps
This example demonstrates how to use just a single function instead of steps in a workflow.
This example demonstrates Workflows using a single custom execution function instead of discrete steps. This pattern gives you complete control over the orchestration logic while still benefiting from workflow features like storage, streaming, and session management.
When to use: When you need maximum flexibility and control over the execution flow, similar to Workflows 1.0 approach but with a better structured approach.
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.types import WorkflowExecutionInput8from kern.workflow.workflow import Workflow910# Define agents11hackernews_agent = Agent(12 name="Hackernews Agent",13 model=OpenAIResponses(id="gpt-5.2"),14 tools=[HackerNewsTools()],15 role="Extract key insights and content from Hackernews posts",16)17finance_agent = Agent(18 name="Finance Agent",19 model=OpenAIResponses(id="gpt-5.2"),20 tools=[YFinanceTools()],21 role="Get stock prices and financial data",22)2324# Define research team for complex analysis25research_team = Team(26 name="Research Team",27 members=[hackernews_agent, finance_agent],28 instructions="Research tech topics from Hackernews and the web",29)3031content_planner = Agent(32 name="Content Planner",33 model=OpenAIResponses(id="gpt-5.2"),34 instructions=[35 "Plan a content schedule over 4 weeks for the provided topic and research content",36 "Ensure that I have posts for 3 posts per week",37 ],38)394041def custom_execution_function(42 workflow: Workflow, execution_input: WorkflowExecutionInput43):44 print(f"Executing workflow: {workflow.name}")4546 # Run the research team47 run_response = research_team.run(execution_input.input)48 research_content = run_response.content4950 # Create intelligent planning prompt51 planning_prompt = f"""52 STRATEGIC CONTENT PLANNING REQUEST:5354 Core Topic: {execution_input.input}5556 Research Results: {research_content[:500]}5758 Planning Requirements:59 1. Create a comprehensive content strategy based on the research60 2. Leverage the research findings effectively61 3. Identify content formats and channels62 4. Provide timeline and priority recommendations63 5. Include engagement and distribution strategies6465 Please create a detailed, actionable content plan.66 """67 content_plan = content_planner.run(planning_prompt)6869 # Return the content plan70 return content_plan.content717273# Create and use workflow74if __name__ == "__main__":75 content_creation_workflow = Workflow(76 name="Content Creation Workflow",77 description="Automated content creation from blog posts to social media",78 db=SqliteDb(79 session_table="workflow_session",80 db_file="tmp/workflow.db",81 ),82 steps=custom_execution_function,83 )84 content_creation_workflow.print_response(85 input="AI trends in 2024",86 )