pip install openai pydantic
export OPENAI_API_KEY=sk-...
Create Pydantic models for Plan and Task that capture the goal decomposition.
1 from openai import OpenAI 2 from pydantic import BaseModel, Field 3 from typing import Literal 4 5 client = OpenAI() 6 7 class Task(BaseModel): 8 id: str = Field(description="Short unique identifier like 't1', 't2'") 9 description: str = Field(description="Specific, actionable task description") 10 depends_on: list[str] = Field(default_factory=list, description="IDs of tasks that must complete first") 11 status: Literal["pending", "in_progress", "done", "failed"] = "pending" 12 result: str | None = None 13 14 class Plan(BaseModel): 15 goal: str 16 reasoning: str = Field(description="Why these tasks achieve the goal") 17 tasks: list[Task] = Field(description="Ordered list of tasks, 3-6 tasks") 18 19 def create_plan(goal: str) -> Plan: 20 response = client.beta.chat.completions.parse( 21 model="gpt-4o", 22 messages=[ 23 {"role": "system", "content": "Decompose the goal into 3-5 concrete, independent, executable tasks."}, 24 {"role": "user", "content": f"Goal: {goal}"}, 25 ], 26 response_format=Plan, 27 ) 28 plan = response.choices[0].message.parsed 29 print(f"Plan for: {goal}") 30 for t in plan.tasks: 31 print(f" [{t.id}] {t.description}") 32 return plan 33 34 if __name__ == "__main__": 35 plan = create_plan("Research and compare the top 3 Python web frameworks for a REST API") 36
Plan object with 3 tasks generated from a research goal
Sign in to share your feedback and join the discussion.