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 Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIResponses
4from kern.team import Team
5from kern.tools.hackernews import HackerNewsTools
6from kern.tools.yfinance import YFinanceTools
7from kern.workflow.types import WorkflowExecutionInput
8from kern.workflow.workflow import Workflow
9
10# Define agents
11hackernews_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)
23
24# Define research team for complex analysis
25research_team = Team(
26 name="Research Team",
27 members=[hackernews_agent, finance_agent],
28 instructions="Research tech topics from Hackernews and the web",
29)
30
31content_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)
39
40
41def custom_execution_function(
42 workflow: Workflow, execution_input: WorkflowExecutionInput
43):
44 print(f"Executing workflow: {workflow.name}")
45
46 # Run the research team
47 run_response = research_team.run(execution_input.input)
48 research_content = run_response.content
49
50 # Create intelligent planning prompt
51 planning_prompt = f"""
52 STRATEGIC CONTENT PLANNING REQUEST:
53
54 Core Topic: {execution_input.input}
55
56 Research Results: {research_content[:500]}
57
58 Planning Requirements:
59 1. Create a comprehensive content strategy based on the research
60 2. Leverage the research findings effectively
61 3. Identify content formats and channels
62 4. Provide timeline and priority recommendations
63 5. Include engagement and distribution strategies
64
65 Please create a detailed, actionable content plan.
66 """
67 content_plan = content_planner.run(planning_prompt)
68
69 # Return the content plan
70 return content_plan.content
71
72
73# Create and use workflow
74if __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 )