How can I use function_tool to create a function p...
# ask-ai
p
How can I use function_tool to create a function programatically?
t
Thanks for reaching out, Mario. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's walk through how you can create a function programmatically using
function_tool
. You’ll define a callable function and provide it with a name, description, and, if needed, a raw schema for more complex parameter setups. Here’s a simple step-by-step guide to help you along: 1. First, define the function you'd like to use as the tool handler. For example:
Copy code
async def my_function(raw_arguments: dict[str, object], context: RunContext):
    # Your function logic here
    return "Function executed successfully!"
1. Next, wrap your function with
function_tool
, and don't forget to add some metadata like the name and description to keep things clear:
Copy code
tool = function_tool(my_function, name="my_tool", description="This is a sample tool.")
1. If you want to create dynamic tools, you can define an inner function and make use of
raw_schema
for extra flexibility. Here's an example:
Copy code
def create_dynamic_tool(tool_name: str):
    schema = {
        "type": "function",
        "name": tool_name,
        "description": f"Dynamic tool: {tool_name}",
        "parameters": {
            "type": "object",
            "properties": {
                "param": {"type": "string", "description": "A parameter"}
            },
            "required": ["param"]
        }
    }

    async def handler(raw_arguments: dict[str, object], context: RunContext):
        param = raw_arguments["param"]
        return f"Handled parameter: {param}"

    return function_tool(handler, raw_schema=schema)
You can add these tools when initializing your agent, or update them dynamically anytime using
agent.update_tools()
. If anything feels unclear or you’d like more examples, please don’t hesitate to ask—I’m here to help! Sources: Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs | README.md | rpc/rpc_agent.py
p
I'm getting 2025-09-18 191212,368 - ERROR livekit.agents - Error in _traceable_on_enter
AttributeError: 'method' object has no attribute '__livekit_tool_info'
t
I'd love to help you get to the bottom of this! Could you share a bit more about when this error pops up? For instance: • Are you working with a custom tool or something pre-defined? • Which specific code or function seems to trigger the error? • Do you have any extra logs or steps that happen just before the error appears? Having these details will really help us figure out what's going on together.