How can I make parameter of a tool become optional...
# ask-ai
a
How can I make parameter of a tool become optional?
t
Thanks for reaching out, Rod. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to make a tool parameter optional — here's a concise way to do that when defining tool parameters with Zod in LiveKit Agents. 1. Mark the field as optional in the Zod schema using `.optional()`:
Copy code
parameters: z.object({
  location: z.string().describe('The location to get weather for'),
  unit: z.enum(['celsius','fahrenheit']).optional(), // optional parameter
})
1. Provide a default inside
execute
so your code has a fallback when the parameter is omitted:
Copy code
execute: async ({ location, unit = 'celsius' }) => {
  // unit will be 'celsius' if omitted
}
That’s all — the parameter can be omitted when invoking the tool, and your
execute
handler receives either the provided value or the default. Would you like an example for a different parameter type or validation rule? Sources: Agents (Node.js) | livekit/livekit_composite | DeepWiki
a
I am using python
t
Thanks — since you’re using Python, make the parameter optional by giving it a default value in your tool function. The
@function_tool
decorator will treat parameters with defaults as optional. Example:
Copy code
from livekit.agents import function_tool

@function_tool()
async def get_weather(
    location: str,            # required
    unit: str = "celsius"     # optional with default
) -> str:
    return f"Weather in {location}: 20 degrees {unit}"
Do you want an example that validates allowed values for
unit
(e.g., only "celsius" or "fahrenheit") or one that uses
Optional[...]
typing? Sources: Tool definition and use | LiveKit docs