getting â instead of apostrphes when using openai ...
# ask-ai
a
getting â instead of apostrphes when using openai . Any thoughts?
t
Thanks for reaching out, Cesar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please share a bit more about where and how this issue is happening? For instance, are you using OpenAI within a LiveKit integration, or is it occurring somewhere else? Also, are you sending or receiving text in a particular encoding format like UTF-8? I’m here to help, so any extra details you can provide will really help us get to the bottom of this together!
a
We use livekit cloud services, and host our own agent code.
t
I understand how frustrating it can be to see characters like â instead of apostrophes in your LiveKit setup. Don’t worry, there are some helpful text normalization utilities in LiveKit that can make sure your text is processed consistently and fix these encoding hiccups. Here’s a friendly guide on how to get this sorted: • Try using the
normalizeText
function from LiveKit's turn detector utilities. It applies Unicode NFKC normalization, cleans up unnecessary punctuation, and keeps apostrophes just right. • If you’re working with JavaScript or TypeScript agents, you can use the
normalizeText
utility directly. For Python agents, a similar approach is to use Python’s
unicodedata.normalize('NFKC', text)
function. • For those using TTS services, turning on their built-in text normalization features can really help. For example, Speechify TTS and ElevenLabs TTS offer parameters like
text_normalization
and
apply_text_normalization
that handle these issues smoothly. Following these steps should clear up the encoding problems in your custom agent code. If you run into any bumps or have more questions, feel free to reach out—I’m here to help! Sources: plugins/livekit/src/turn_detector/utils.test.ts | livekit-plugins/livekit-plugins-speechify/livekit/plugins/speechify/tts.py | ElevenLabs TTS | LiveKit Docs
a
It happens on the outcoming text coming from the LLM node.
t
To help you fix those pesky character encoding issues in the outgoing text from your LLM node, you can gently intercept and tweak the output before it reaches the TTS engine. Here are two friendly and effective ways to do this: • Override the
llm_node
Method:
You can customize the LLM output stream by overriding the
llm_node
method in your agent class. This way, you get to process and clean the text chunks as they come through. Here’s a simple example to guide you:
Copy code
class SimpleAgent(Agent):
    async def llm_node(self, chat_ctx, tools, model_settings=None):
        async def process_stream():
            async with self.llm.chat(chat_ctx=chat_ctx, tools=tools) as stream:
                async for chunk in stream:
                    if chunk is None:
                        continue
                    content = getattr(chunk.delta, 'content', None)
                    if content:
                        processed_content = content.replace('â', "'")
                        chunk.delta.content = processed_content
                    yield chunk
        return process_stream()
• Use the
before_tts_cb
Callback:
Another great approach is to apply a final cleanup to the text just before it’s sent to the TTS engine, using the
before_tts_cb
parameter. Here’s how you can do it:
Copy code
async def _before_tts_cb(agent, text):
    if isinstance(text, str):
        return text.replace('â', "'")
    else:
        async def process_stream():
            async for chunk in text:
                yield chunk.replace('â', "'")
        return process_stream()
These friendly methods will help make sure your outgoing text looks just right, free from those encoding hiccups. If you’d like, I’m here to help you walk through implementing these solutions step by step—just let me know! Sources: Modifying LLM output before TTS | LiveKit Docs | pipeline-llm/llm_powered_content_filter.py | chain-of-thought-tts/README.mdoc | Pipeline nodes and hooks | LiveKit Docs
a
no
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!