Skip to content

Conversation

@AlanPonnachan
Copy link

Allow dynamic configuration of built-in tools via RunContext

This PR closes #3555

Adding support for dynamic configuration of built-in tools (e.g., WebSearchTool) based on the agent's RunContext.

Previously, builtin_tools only accepted static instances. Now, it accepts AbstractBuiltinTool instances or a BuiltinToolFunc — a callable that takes RunContext and returns an AbstractBuiltinTool or None. This allows users to inject context-aware parameters (like a user's location) into tools at runtime.

Key Changes:

  • New Type: Added BuiltinToolFunc alias for Callable[[RunContext], Awaitable[AbstractBuiltinTool | None] | AbstractBuiltinTool | None].
  • Agent Logic: Updated _agent_graph.py to resolve these dynamic functions before generating model parameters.
  • Signatures: Updated type hints for Agent, AbstractAgent, WrapperAgent, and durable execution integrations (DBOS, Prefect, Temporal).
  • Tests: Added tests/test_dynamic_builtin_tools.py covering sync/async definitions, conditional omission (returning None), and runtime injection.

Example Usage:

async def dynamic_search(ctx: RunContext[UserDeps]) -> WebSearchTool:
    return WebSearchTool(
        user_location={'city': ctx.deps.city}
    )

agent = Agent(builtin_tools=[dynamic_search], deps_type=UserDeps)

@DouweM
Copy link
Collaborator

DouweM commented Dec 1, 2025

@AlanPonnachan Nice work! Can you please mention this in the Builtin tools docs as well?

@AlanPonnachan AlanPonnachan force-pushed the feat/dynamic-builtin-tools branch from 8caac69 to 289f8d0 Compare December 2, 2025 14:49
@AlanPonnachan
Copy link
Author

@DouweM I added docs as well. Can you please review now

@AlanPonnachan
Copy link
Author

@DouweM Thank you for the review. I’ve made the requested changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Built-in tool should allow for dynamic configuration

2 participants