when I use livekit inference, I am getting error f...
# ask-ai
w
when I use livekit inference, I am getting error for metrics event collection. how do I fix this? - @session.llm.on("metrics_collected") ^^^^^^^^^^^^^^ AttributeError: 'str' object has no attribute 'on'
t
Thanks for reaching out, Sunil. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It looks like the error is happening because the
.on()
method is being called on a string rather than an actual LiveKit component instance. No worries—this is a common hiccup! To get this sorted, just make sure you’re using the correct object that’s returned when you instantiate or load the component. For example, if you’re working with an LLM instance, here’s how it should look:
Copy code
from livekit.plugins import openai
llm = openai.LLM(model="gpt-4o-mini")  # Instantiate the LLM object

def metrics_callback(event):
    print("Metrics collected:", event)

llm.on("metrics_collected", metrics_callback)  # Register the listener
Just double-check that you’re not using a string like
"openai.LLM"
instead of the actual object. If you’re still running into trouble, please feel free to share which component you’re working with, and I’ll be happy to help you further! Sources: metrics/metrics_vad.py | livekit-agents/livekit/agents/voice/agent_activity.py | Events and error handling | LiveKit Docs
w
with livekit inferene, the stt, llm and tts are string
t
I understand this can be a bit tricky, so let’s walk through it together. If the STT, LLM, and TTS components in your implementation are currently just strings, you’ll want to replace those with their proper instantiated objects. Here’s a friendly guide on how to set them up correctly: For STT:
Copy code
from livekit.plugins import deepgram
stt = deepgram.STT()  # This creates the STT object for you

def stt_metrics_callback(event):
    print("STT Metrics:", event)

stt.on("metrics_collected", stt_metrics_callback)
For LLM:
Copy code
from livekit.plugins import openai
llm = openai.LLM(model="gpt-4o-mini")  # Here we instantiate the LLM object

def llm_metrics_callback(event):
    print("LLM Metrics:", event)

llm.on("metrics_collected", llm_metrics_callback)
For TTS:
Copy code
from livekit.plugins import google
tts = google.TTS()  # And this sets up the TTS object

def tts_metrics_callback(event):
    print("TTS Metrics:", event)

tts.on("metrics_collected", tts_metrics_callback)
Just make sure to swap out any string assignments with these object creations as shown. If anything feels unclear or you want me to walk you through any part in more detail, please don’t hesitate to ask—I’m here to help!
w
do you understand livekit inference?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!