- Notifications
You must be signed in to change notification settings - Fork1k
Closed
Description
Description
Makerun_sql
only available once the plan has been presented to the user.
fromdataclassesimportdataclassfromdotenvimportload_dotenvfrompydantic_aiimportAgent,RunContextfrompydantic_ai.messagesimportModelMessagefrompydantic_ai.toolsimportTool,ToolDefinitionload_dotenv()@dataclassclassAgentDeps:plan_presented:bool=Falseasyncdefpresent_plan(ctx:RunContext[AgentDeps],plan:str)->str:""" Present the plan to the user. """ctx.deps.plan_presented=Truereturnplanasyncdefrun_sql(ctx:RunContext[AgentDeps],purpose:str,query:str)->str:""" Run an SQL query. """ ...asyncdefonly_if_plan_presented(ctx:RunContext[AgentDeps],tool_def:ToolDefinition)->ToolDefinition|None:ifctx.deps.plan_presented:returntool_defagent=Agent(model="google-gla:gemini-2.5-flash",deps_type=AgentDeps,output_type=[present_plan,Tool(run_sql,prepare=only_if_plan_presented)],)asyncdefrun_agent(user_prompt:str,agent_deps:AgentDeps,message_history:list[ModelMessage]|None=None)->str:whileTrue:res=awaitagent.run(user_prompt=user_prompt,deps=agent_deps,message_history=message_history)message_history=res.all_messages()user_prompt=input(f"{res.output} > ")ifuser_prompt.lower()in ["q","quit","exit"]:breakreturnres.output
References
Based on discussion with@DouweM here:https://pydanticlogfire.slack.com/archives/C083V7PMHHA/p1750354877664179