feat(tools): implement extended tool definitions with native Pydantic…#24
feat(tools): implement extended tool definitions with native Pydantic…#24Leoyzen wants to merge 5 commits intophil65:mainfrom
Conversation
…AI integration - Introduce support for the 'prepare' protocol and explicit 'function_schema' overrides in tool definitions - Utilize native pydantic_ai.Tool.from_schema to enable full validation capabilities, including validate_json - Implement an automated schema generation fallback using schemez to handle complex context types (AgentContext, RunContext) - Enhance Tool.to_pydantic_ai to handle custom JSON schemas and context injection natively - Ensure seamless integration with agent-level tool wrapping for confirmation and execution hooks - Provide comprehensive test coverage for dynamic schema generation and validation paths
…ydanticAI integration
…ydanticAI integration
048333c to
767333b
Compare
…ydanticAI integration
|
Could you explain some concrete use cases here? |
|
@phil65 Here are the concrete use cases for 1. Dynamic Context Injection (Hidden from LLM)
2. Dynamic Schema Modification
Why Like #23 |
|
...and why not just injecting AgentContext/RunContext via tool signature and doing the injection in the tool? |
|
@phil65 Good question. The context injection already works in main (via function signature) — that part is fine. What's missing is dynamic schema definition based on runtime context. The schema is generated at tool registration time, before we have access to # Current limitation:
async def delegate_to(agent_names: list[str], message: str):
# LLM sees: "agent_names: list of agent names to delegate to"
# But we want: "Available agents: [writer, reviewer, coder]" <- dynamic!
...With
So |
PR Description
Summary
This PR significantly enhances the
Toolsystem to provide deeper and more native integration withpydantic-ai. It addresses limitations in schema generation for complex context types (likeAgentContext) and enables advanced features like theprepareprotocol for dynamic tool configuration.Key Changes
Core Tooling (
src/agentpool/tools/base.py)prepareProtocol Support: Addedpreparefield toToolto supportpydantic-ai's dynamic tool configuration (middleware-like behavior before tool execution).function_schemato allow direct injection of Pydantic validators and schemas, bypassing auto-generation when needed.pydantic_ai.function_schema, and gracefully falls back toschemezfor complex signatures (e.g., those involvingAgentContextor forward references) that usually break native generation._detect_takes_ctxto correctly identify and handleRunContextinjection requirements.to_pydantic_aiConversion: Now utilizespydantic_ai.Tool.from_schemawhen custom schemas or overrides are present, ensuring full validation capabilities (includingvalidate_json) are preserved even with custom definitions.Agent Integration (
src/agentpool/agents/native_agent/agent.py)create_pydantic_ai_agentto properly utilize the enhancedTool.to_pydantic_ai()method, ensuring all tools (native or wrapped) carry their correct schemas and hooks.deps_typeasAgentContext[TDeps]to ensure correct context propagation throughout the agent lifecycle.Configuration (
src/agentpool_config/tools.py)prepareHooks: Added support for definingpreparefunctions via import strings (e.g.,module:func) in YAML configuration.Testing
tests/tools/test_tool_schema.pycovering:RunContextvsAgentContexthandling.Motivation
Previously, tools requiring
AgentContextor complex dependencies often failed standard PydanticAI schema generation or lost validation capabilities when manually overridden. This PR ensures that:schemezfallback.preparefeatures for JIT tool modification.validate_json) remains intact even when schemas are customized.