Nested Workflow

Basic nested workflow: inner research pipeline feeds into an outer writing step.

1from kern.agent import Agent
2from kern.models.openai import OpenAIChat
3from kern.workflow.step import Step
4from kern.workflow.types import StepInput, StepOutput
5from kern.workflow.workflow import Workflow
6
7
8def create_summary(step_input: StepInput) -> StepOutput:
9 """A simple function step that summarizes the previous step's output"""
10 previous_content = step_input.get_last_step_content()
11 summary = (
12 f"Summary of research:\n{previous_content[:500]}..."
13 if previous_content
14 else "No content to summarize"
15 )
16 return StepOutput(content=summary)
17
18
19# Create a simple inner workflow that does research
20research_agent = Agent(
21 name="Research Agent",
22 model=OpenAIChat(id="gpt-4o-mini"),
23 instructions="You are a research assistant. Provide concise, factual information.",
24)
25
26inner_workflow = Workflow(
27 name="Research Workflow",
28 description="A simple workflow that researches a topic",
29 steps=[
30 Step(name="research", agent=research_agent),
31 Step(name="summary", executor=create_summary),
32 ],
33)
34
35# Create the outer workflow that uses the inner workflow as a step
36writer_agent = Agent(
37 name="Writer Agent",
38 model=OpenAIChat(id="gpt-4o-mini"),
39 instructions="You are a professional writer. Take the research provided and write a polished article.",
40)
41
42outer_workflow = Workflow(
43 name="Research and Write Workflow",
44 description="A workflow that researches a topic and then writes about it",
45 steps=[
46 Step(name="research_phase", workflow=inner_workflow),
47 Step(name="writing_phase", agent=writer_agent),
48 ],
49)
50
51
52if __name__ == "__main__":
53 result = outer_workflow.print_response(
54 input="Tell me about the history of artificial intelligence",
55 stream=True,
56 stream_events=True,
57 )

Run the Example

1git clone https://github.com/kern-ai/kern.git
2cd kern/cookbook/04_workflows/06_advanced_concepts/workflow_as_a_step
3
4pip install kern-ai openai
5
6python nested_workflow.py