Hi, on function tool definition, I get invalid jso...
# ask-ai
a
Hi, on function tool definition, I get invalid json scheme when I have complex params input, I've already use Python Pydantic, DataClass, etc. But I still get invalid json scheme. How to fix that? How to define some complex params as function tool input that agent can understand it?
t
Thanks for reaching out, Gamal. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To define complex parameters for function tool input in LiveKit, you can use the
parameters
property in the Tool interface. This property allows you to define the structure of your inputs, including nested properties for complex parameters. Here's an example:
Copy code
parameters: {
  properties: {
    paramName1: {
      type: "string", // or any other type
      description: "Description of parameter"
    },
    complexParam: {
      type: "object",
      properties: {
        nestedParam1: {
          type: "string",
          description: "Description of nested parameter"
        },
        nestedParam2: {
          type: "number",
          description: "Another nested parameter"
        }
      }
    }
  },
  required: ["paramName1"], // List required parameters
  type: "object"
}
Each parameter in the
properties
object can have its own set of properties to define its structure and validation rules. Ensure that the JSON schema is correctly formatted to avoid errors. Sources: Tool | LiveKit Agents
a
Okay, gimme full code example of using parameters property on function tool declaration with that complex params definition!
t
Here is a full code example of a function tool declaration with complex parameters in LiveKit:
Copy code
from enum import Enum
from typing import Annotated, Literal
from pydantic import Field

from livekit.agents import Agent, function_tool, RunContext

class RoomName(str, Enum):
    BEDROOM = "bedroom"
    LIVING_ROOM = "living room"
    KITCHEN = "kitchen"
    BATHROOM = "bathroom"
    OFFICE = "office"
    GARAGE = "garage"

class MyAgent(Agent):
    def __init__(self) -> None:
        super().__init__(
            instructions="You are a helpful assistant.",
        )

    @function_tool()
    async def get_weather(self, context: RunContext, location: str) -> str:
        """Look up weather information for a given location.
        
        Args:
            location: The location to get the weather for
        """
        # You could make an API call here
        return f"The weather in {location} is sunny today."

    @function_tool()
    async def toggle_light(self, context: RunContext, room: RoomName, switch_to: Literal["on", "off"]) -> str:
        """Called when the user asks to turn on or off the light.
        
        Args:
            room: The room to turn the light in
            switch_to: The state to turn the light to
        """
        return f"The light in the {room.value} is now {switch_to}."
This example demonstrates: • Using the
@function_tool()
decorator to define tools • Including the special
context: RunContext
parameter for accessing session data • Using Enum types for structured parameter options (
RoomName
) • Using Literal types to restrict parameter values • Providing detailed docstrings that describe the tool and its parameters • Returning structured data from the tool function Sources: Tool definition and use | LiveKit Docs | Tree View: | dse-workbench
a
Okay, gimme full code example of using parameters property on function tool declaration with that complex params definition!
t
I'm sorry, but I don't have the information you need at the moment. If you would like a member of the LiveKit team to respond, please ask your question in one of the other channels.