Can custom livekit Agent class inherit function_to...
# ask-ai
f
Can custom livekit Agent class inherit function_tools, sst, llm, tts from other custom agent classes?
t
Thanks for reaching out, Taras. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I understand that working with custom LiveKit Agent classes can feel a bit tricky, especially when it comes to inheriting components like
function_tools
,
stt
,
llm
, or
tts
from another custom agent class. While direct class inheritance isn’t supported for these components, don’t worry—LiveKit offers some great alternative ways to reuse and share them: • Shared Tools: You can define tools externally and then pass them to multiple agents through the
tools
argument in the constructor. This way, your tools stay consistent across agents. • Customizable Nodes: If you want to add your own logic but still keep the default behavior, try extending or overriding processing nodes like
stt_node
,
llm_node
, and
tts_node
. • Dynamic Updates: The
agent.update_tools()
method lets you share tools dynamically between agents while your program is running, which can be really handy. • Session-Level Components: Another approach is to configure shared components—like STT or LLM instances—at the session level so multiple agents can use them seamlessly. • Handoff Between Agents: And if you need to transfer context and control between agents, the
llm.handoff
feature is designed just for that. I hope this helps clarify things! If you’d like, I’m here to provide more details or walk you through examples on how to implement any of these methods. Just let me know—I’m happy to assist! Sources: Tool definition and use | LiveKit Docs | Docs::Agents | Pipeline nodes and hooks | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
f
can I just inherit one class from another?
t
Absolutely, you can inherit one class from another in Python, including your custom LiveKit Agent classes. That said, while inheritance lets you bring in methods and attributes from a parent class, LiveKit doesn’t automatically handle inheriting components like
function_tools
,
stt
,
llm
, or
tts
through class inheritance. These components will need to be explicitly defined or passed along to your child class. If you're looking to reuse functionality, a great approach is to define shared methods or attributes in a base class and then have your custom agent classes inherit from that. I’d be happy to walk you through an example if you'd like—just let me know!
f
provide example of inheritance from base class
t
I'd love to walk you through an example of how you can create a base class and then inherit from it in your custom LiveKit Agent classes. This way, you can keep things neat and reusable!
Copy code
from livekit import Agent, function_tool

# Define a base class with shared functionality
class BaseAgent(Agent):
    def __init__(self, tools=None):
        # Initialize the base Agent class with shared tools
        super().__init__(tools=tools)

    @function_tool()
    async def shared_tool(self, context, input_data):
        return {"message": f"Processed: {input_data}"}

# Define a custom agent that inherits from the base class
class CustomAgentA(BaseAgent):
    def __init__(self):
        # Pass shared tools to the base class
        super().__init__(tools=[self.shared_tool])

# Define another custom agent that also inherits from the base class
class CustomAgentB(BaseAgent):
    def __init__(self):
        # Pass shared tools to the base class
        super().__init__(tools=[self.shared_tool])

# Example usage
agent_a = CustomAgentA()
agent_b = CustomAgentB()
Here's what's happening: •
BaseAgent
holds the shared functionality, including a handy tool called
shared_tool
. •
CustomAgentA
and
CustomAgentB
both inherit from
BaseAgent
and get to reuse that shared tool effortlessly. This way, you can keep all your shared logic nicely organized in one place while still having the flexibility to build custom agents that suit your needs. If you'd like me to clarify anything or provide more examples, just let me know—I’m here to help!
f
ValueError: duplicate function name:
t
It looks like you’re encountering the error
ValueError: duplicate function name
, which happens when multiple tools with the same name are registered in an agent. This usually occurs if the same tool is defined or inherited more than once without giving it a unique name. Here are a couple of friendly tips to help you resolve this: 1. Rename the tool: Try using the
name
parameter in the
@function_tool
decorator to assign each tool a unique name. For example:
Copy code
@function_tool(name="shared_tool_a")
async def shared_tool_a(context, input_data):
    return {"message": f"Processed by A: {input_data}"}
2. Make sure tools aren’t registered multiple times: Double-check that each tool is added only once to the
tools
list when you initialize your agent. If you’d like, I’m here to help you debug your specific setup! Feel free to share more about how you’re defining and registering your tools, and we can work through it together.