openai llm stream text automatically with the live...
# ask-ai
b
openai llm stream text automatically with the livekit plugin? or should it be handled with any specific parameter? Specify.
t
Thanks for reaching out, Andrew. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit's OpenAI plugin supports LLM text streaming automatically without requiring any specific parameters. Streaming is enabled by default when you create an LLMStream using the plugin. Here are the details: • Python Implementation: The LLMStream class automatically configures streaming by setting
stream=True
and
stream_options={"include_usage": True}
when calling the OpenAI chat completions API. • JavaScript Implementation: Similarly, the JavaScript version enables streaming with
stream: true
and
stream_options: { include_usage: true }
in the chat completions request. For example, in Python:
Copy code
from livekit.plugins import openai
from livekit.agents import ChatContext

llm = openai.LLM(model="gpt-4o-mini")
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="Hello, this is a test message!")

# Streaming happens automatically
stream = llm.chat(chat_ctx=chat_ctx)
async for chunk in stream:
    # Process streaming chunks
    pass
The plugin handles all the streaming configuration internally, so you can achieve real-time text streaming without any additional setup. Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/__init__.py | plugins/openai/src/realtime/realtime_model.ts | Large language model (LLM) integrations | LiveKit Docs
b
is streaming automatic in AgentSession abstraction too?
t
Absolutely! Streaming happens automatically in the AgentSession abstraction because it’s a fundamental part of how it’s built. The AgentSession is crafted to bring together media streams, speech/LLM components, and tool orchestration into one seamless, real-time voice agent experience. That means streaming is essential for live audio processing, detecting turns, handling interruptions, and managing multi-step tool calls. Here are some key things to keep in mind: • Streaming is baked right into the framework, so you don’t need to turn it on separately. • The framework supports only STT and TTS providers that have streaming endpoints, making sure everything in AgentSession works smoothly in real-time. • AgentSession manages features like turn detection, endpointing, and interruptions through event callbacks, which depend on continuous streaming of audio and data. So, when you set up an AgentSession and plug in the right providers (like STT and TTS), streaming just works—no extra configuration needed. Sources: TTSCapabilities | LiveKit Agents | livekit-agents/livekit/agents/voice/agent_session.py | agents/src/tts/tts.ts | STTCapabilities | LiveKit Agents