silly-baker-11446
08/30/2025, 4:00 AMtall-airline-16756
08/30/2025, 4:00 AMtall-airline-16756
08/30/2025, 4:01 AMchunk.choices[0].delta.content. But just a heads-up—there are a few important things to watch out for to make sure everything runs smoothly:
• Make sure chunk.choices isn’t empty or None.
• Double-check that chunk.choices[0].delta isn’t `None`—this can happen sometimes, like when content gets filtered.
• Also, confirm that chunk.choices[0].delta.content isn’t None or empty before you add it to your response.
Here’s a simple example to help you handle the stream safely:
full_response = ""
async for chunk in llm_stream:
if chunk.choices:
content = getattr(chunk.choices[0].delta, 'content', None)
if content:
full_response += content
Using this way, your code will gracefully handle those tricky edge cases without a hitch.
Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/llm.py | pipeline-llm/replacing_llm_output.py