how do I set the description of function_tool of a...
# ask-ai
f
how do I set the description of function_tool of agent dynamically?
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you dynamically set the description of a
function_tool
in LiveKit! You can achieve this by using the
function_tool
function (not as a decorator) and providing the
description
parameter dynamically at runtime. Here are some friendly ways to do this: • Create Tools Dynamically: You can use
function_tool()
as a function to pass a description that’s generated on the fly. Here’s a simple example:
Copy code
function_tool(
    self._set_profile_field_func_for("phone"),
    name="set_phone_number",
    description="Call this function when user has provided their phone number."
)
• Update Agent Tools at Runtime: If you want to modify the agent’s tools dynamically, you can use
agent.update_tools()
. Check out this example:
Copy code
await agent.update_tools(
    agent.tools + [
        function_tool(
            _random_number,
            name="random_number",
            description="Get a random number between 0 and 100"
        )
    ]
)
• Modify Tools During LLM Node Execution: Another approach is to adjust the tools list dynamically inside the
llm_node
method. Here’s how you might do it:
Copy code
async def llm_node(self, chat_ctx, tools, model_settings):
    tools.append(
        function_tool(
            _get_weather,
            name="get_weather",
            description="Get the weather in a specific location"
        )
    )
    return Agent.default.llm_node(self, chat_ctx, tools, model_settings)
If any part of this feels unclear or if you’d like me to walk you through it step-by-step, please don’t hesitate to ask. I’m here to help! Sources: livekit-agents/livekit/agents/llm/tool_context.py | basics/say_in_voice.py | Docs::Agents | Tool definition and use | LiveKit Docs
f
class Assistant(Agent): def __init__(self, questionEntity: CodingQuestionEntity, prompts) -> None: self.prompts = prompts instructions = self.format_prompt(questionEntity) super().__init__(instructions=instructions) # Set dynamic tool descriptions if PromptCategory.TOOL_END_AFTER_COMPLETION in prompts: self.endInterviewAfterCompletion.func.doc = prompts[PromptCategory.TOOL_END_AFTER_COMPLETION] if PromptCategory.TOOL_END_ON_REQUEST in prompts: self.endInterviewOnCandidateRequest.func.doc = prompts[PromptCategory.TOOL_END_ON_REQUEST] Can I do something like this? where the endInterviewAfterCompletion and endInterviewOnCandidateRequest are functions with the decorator @function_tool?
t
When you use the
@function_tool
decorator, it captures the function’s description from the
__doc__
attribute right at the moment the decorator is applied. So, if you change the
__doc__
attribute afterward, the tool’s description won’t update dynamically. To help you achieve dynamic descriptions, here are a few friendly approaches you might find useful: • Set
__doc__
Before Decoration:
You can set the
__doc__
attribute programmatically before applying the decorator. This way, the description is ready when the decorator runs.
Copy code
tool_impl.__doc__ = "Dynamic description here"
return function_tool()(tool_impl)
• Use `agent.update_tools()`: If you want to update descriptions at runtime, you can replace tools using this method.
Copy code
await agent.update_tools(
    agent.tools + [
        function_tool(
            _random_number,
            name="random_number",
            description="Updated description"
        )
    ]
)
• Create Tools Programmatically: Another way is to use
function_tool
as a function, setting descriptions dynamically when creating the tool.
Copy code
function_tool(
    _get_weather,
    name="get_weather",
    description="Dynamic description"
)
If any part of this feels unclear or if you’d like a hand implementing these steps, please don’t hesitate to reach out—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Docs::Agents | examples/voice_agents/README.md | mcp_client/agent_tools.py | docs/index.yaml
f
Where do I put the code
Copy code
function_tool(
    _get_weather,
    name="get_weather",
    description="Dynamic description"
)
at? can I put it within init of agent?
t
I'm happy to guide you through using
function_tool
within the
__init__
method of an Agent to dynamically define tools. Here’s a friendly walkthrough: • Programmatically Create Tools: You can use
function_tool
as a function to define tools on the fly and add them to your agent's toolset. Here's an example to illustrate:
Copy code
from livekit.agents.llm import function_tool

class DynamicToolAgent(Agent):
    def __init__(self) -> None:
        super().__init__(instructions="You can perform dynamic actions.")

        # Define a function dynamically
        async def dynamic_action(location: str) -> str:
            return f"Performing action in {location}"

        # Decorate it as a tool
        tool = function_tool(
            name="dynamic_action",
            description="Perform an action in a given location"
        )(dynamic_action)

        # Add to agent's tools
        self._tools.append(tool)
• Automatic Registration: Another friendly tip: if you use
@function_tool
as a decorator on your class methods, they’ll be automatically registered as tools when your agent initializes. It’s a neat way to keep things organized! If anything feels unclear or you’d like a bit more help with the implementation, please don’t hesitate to ask. I’m here to help you every step of the way! Sources: Docs::Agents | examples/voice_agents/raw_function_description.py | mcp_client/agent_tools.py
f
What if I define the function tool method within the class as an object method, can I use that as the function tool method work?
t
Absolutely, you can define a method within a class as an object method and use
@function_tool
to make it accessible as a tool. Here’s a friendly example to guide you:
Copy code
from livekit.agents import function_tool, Agent, RunContext

class MyAgent(Agent):
    @function_tool()
    async def lookup_weather(
        self,
        context: RunContext,
        location: str,
    ) -> dict[str, Any]:
        """
        Look up weather information for a given location.

        Args:
            location: The location to look up weather information for.
        """
        return {"weather": "sunny", "temperature_f": 70}
In this example, the
lookup_weather
method is thoughtfully defined within the class and decorated with
@function_tool
. This means it will be automatically registered as a tool when your agent is initialized. If you have any questions or need a bit more help, please don’t hesitate to ask—I’m here to support you! Sources: Docs::Agents | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Tool definition and use | LiveKit Docs