Nested Workflow
Basic nested workflow: inner research pipeline feeds into an outer writing step.
1from kern.agent import Agent2from kern.models.openai import OpenAIChat3from kern.workflow.step import Step4from kern.workflow.types import StepInput, StepOutput5from kern.workflow.workflow import Workflow678def 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_content14 else "No content to summarize"15 )16 return StepOutput(content=summary)171819# Create a simple inner workflow that does research20research_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)2526inner_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)3435# Create the outer workflow that uses the inner workflow as a step36writer_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)4142outer_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)505152if __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.git2cd kern/cookbook/04_workflows/06_advanced_concepts/workflow_as_a_step34pip install kern-ai openai56python nested_workflow.py