This example demonstrates how to use just a single function instead of steps in a workflow.
This example demonstrates Workflows 2.0 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.
function_instead_of_steps.py
Copy
Ask AI
from agno.agent import Agentfrom agno.models.openai import OpenAIChatfrom agno.storage.sqlite import SqliteStoragefrom agno.team import Teamfrom agno.tools.duckduckgo import DuckDuckGoToolsfrom agno.tools.hackernews import HackerNewsToolsfrom agno.utils.pprint import pprint_run_responsefrom agno.workflow.v2.types import WorkflowExecutionInputfrom agno.workflow.v2.workflow import Workflow# Define agentshackernews_agent = Agent( name="Hackernews Agent", model=OpenAIChat(id="gpt-4o-mini"), tools=[HackerNewsTools()], role="Extract key insights and content from Hackernews posts",)web_agent = Agent( name="Web Agent", model=OpenAIChat(id="gpt-4o-mini"), tools=[DuckDuckGoTools()], role="Search the web for the latest news and trends",)# Define research team for complex analysisresearch_team = Team( name="Research Team", mode="coordinate", members=[hackernews_agent, web_agent], instructions="Research tech topics from Hackernews and the web",)content_planner = Agent( name="Content Planner", model=OpenAIChat(id="gpt-4o"), instructions=[ "Plan a content schedule over 4 weeks for the provided topic and research content", "Ensure that I have posts for 3 posts per week", ],)def custom_execution_function( workflow: Workflow, execution_input: WorkflowExecutionInput): print(f"Executing workflow: {workflow.name}") # Run the research team run_response = research_team.run(execution_input.message) research_content = run_response.content # Create intelligent planning prompt planning_prompt = f""" STRATEGIC CONTENT PLANNING REQUEST: Core Topic: {execution_input.message} Research Results: {research_content[:500]} Planning Requirements: 1. Create a comprehensive content strategy based on the research 2. Leverage the research findings effectively 3. Identify content formats and channels 4. Provide timeline and priority recommendations 5. Include engagement and distribution strategies Please create a detailed, actionable content plan. """ content_plan = content_planner.run(planning_prompt) # Return the content plan return content_plan.content# Create and use workflowif __name__ == "__main__": content_creation_workflow = Workflow( name="Content Creation Workflow", description="Automated content creation from blog posts to social media", storage=SqliteStorage( table_name="workflow_v2", db_file="tmp/workflow_v2.db", mode="workflow_v2", ), steps=custom_execution_function, ) content_creation_workflow.print_response( message="AI trends in 2024", )
This was a synchronous non-streaming example of this pattern. To checkout async and streaming versions, see the cookbooks-