Using the Testing Framework with Realtime Models
Learn how to properly set up and test your agents using the LiveKit testing framework with both standard LLM models and realtime models.
Last Updated:
The LiveKit testing framework allows you to test your agents with both standard LLM models and realtime models. This guide covers how to properly set up and use the testing framework with realtime models.
Setting Up Realtime Model Tests
To test an agent with a realtime model, use the following pattern:
1async with (2openai.realtime.RealtimeModel(modalities=["text"]) as rt_llm,3AgentSession(llm=rt_llm, userdata=userdata) as sess,4):5await sess.start(YourAgent(userdata=userdata))6result = await sess.run(user_input="...")
This pattern uses Python's async context manager to properly initialize and clean up both the realtime model and the agent session.
Important Notes
When testing with realtime models, keep these key points in mind:
- Use text modalities — When using realtime models, make sure to specify
modalities=["text"]as the testing helpers are designed to work with text input and output. - Context manager required — The realtime model must be used within an agent session context manager to ensure proper resource cleanup.
- Suppressing warnings — If you're seeing deprecation or resource warnings in Python 3.12+, you can suppress them with:
1import warnings23warnings.filterwarnings("ignore", category=DeprecationWarning)4warnings.filterwarnings("ignore", category=ResourceWarning)
Basic Test Example
Here's a complete example of testing an agent with a realtime model:
1import pytest2from livekit.agents import AgentSession3from livekit.plugins import openai45from your_agent import YourAgent, UserData67@pytest.mark.asyncio8async def test_agent_greeting():9userdata = UserData()1011async with (12openai.realtime.RealtimeModel(modalities=["text"]) as rt_llm,13AgentSession(llm=rt_llm, userdata=userdata) as sess,14):15await sess.start(YourAgent(userdata=userdata))16result = await sess.run(user_input="Hello!")1718await result.expect.next_event().is_message(role="assistant").judge(19rt_llm, intent="Provides a friendly greeting"20)
Validating Function Calls
You can test that your agent correctly calls function tools:
1@pytest.mark.asyncio2async def test_function_call():3userdata = UserData()45async with (6openai.realtime.RealtimeModel(modalities=["text"]) as rt_llm,7AgentSession(llm=rt_llm, userdata=userdata) as sess,8):9await sess.start(YourAgent(userdata=userdata))10result = await sess.run(user_input="Book an appointment for tomorrow")1112# Verify the function is called with expected arguments13await result.expect.next_event().is_function_call(14name="book_appointment",15arguments={"date": "tomorrow"}16)1718# Verify function output is processed19await result.expect.next_event().is_function_call_output()
Testing Conversation Context
To test that your agent maintains conversation context across multiple turns:
1@pytest.mark.asyncio2async def test_conversation_context():3userdata = UserData()45async with (6openai.realtime.RealtimeModel(modalities=["text"]) as rt_llm,7AgentSession(llm=rt_llm, userdata=userdata) as sess,8):9await sess.start(YourAgent(userdata=userdata))1011# First turn - establish context12result1 = await sess.run(user_input="My name is Alice")1314# Second turn - verify context is maintained15result2 = await sess.run(user_input="What's my name?")1617await result2.expect.next_event().is_message(role="assistant").judge(18rt_llm, intent="Correctly recalls that the user's name is Alice"19)
Mocking Function Tools
For isolated unit tests, you can mock function tools to control their behavior:
1from livekit.agents.voice.run_result import mock_tools23@pytest.mark.asyncio4async def test_with_mocked_tools():5userdata = UserData()67async with (8openai.realtime.RealtimeModel(modalities=["text"]) as rt_llm,9AgentSession(llm=rt_llm, userdata=userdata) as sess,10):11await sess.start(YourAgent(userdata=userdata))1213# Mock the function to return a specific value14with mock_tools(YourAgent, {15"check_availability": lambda date: "Available at 2pm and 4pm"16}):17result = await sess.run(user_input="Check availability for Monday")1819await result.expect.next_event().is_function_call(name="check_availability")20await result.expect.next_event().is_function_call_output(21output="Available at 2pm and 4pm"22)
Example Test Implementations
For complete reference implementations demonstrating these patterns in real-world agents, check out:
- Comprehensive Agent Testing — Complete test suite with fixtures, mocks, and conversation flows
- Doheny Surf Desk Agent — Multi-agent booking system with extensive testing patterns
Summary
| Pattern | Use Case |
|---|---|
RealtimeModel(modalities=["text"]) | Testing with OpenAI realtime models |
async with context managers | Proper resource management |
result.expect.next_event() | Asserting on agent responses |
mock_tools() | Isolated unit testing of function calls |
Additional Resources
For more examples and advanced testing patterns, refer to our voice agents examples repository.