I need a example for pytest with realtime model
# ask-ai
w
I need a example for pytest with realtime model
t
Thanks for reaching out, Chiranjeevi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to create a pytest test for a LiveKit agent using a realtime model, here’s a friendly step-by-step guide to help you out: 1. First, make sure you have the necessary packages installed:
pytest
and
pytest-asyncio
. 2. Use the
@pytest.mark.asyncio
decorator to handle async test functions smoothly. 3. Create an
AgentSession
with a
RealtimeModel
inside an async context manager — this keeps things neat and tidy. 4. Start the agent and run your test interactions using
session.run(user_input=...)
. 5. Don’t forget to add assertions to check the agent’s behavior, like evaluating intent with
.judge()
. Here’s a quick example of how to initialize a realtime model for your tests:
Copy code
rt_llm = openai.realtime.RealtimeModel(
    modalities=["text"],  # makes sure it works well with text-based testing
    voice="alloy",        # optional voice setting if you want it
    temperature=0.7       # tweak this if you want to change the randomness
)
And here’s what the test setup might look like:
Copy code
async with (
    openai.LLM(model="gpt-4o-mini") as judge_llm,  # this LLM helps judge responses
    AgentSession(llm=rt_llm, userdata=userdata) as sess,
):
    await sess.start(YourAgent(userdata=userdata))
    result = await sess.run(user_input="...")  # pop in your test input here
    await result.expect.next_event().is_message(role="assistant").judge(
        judge_llm,
        intent="Expected agent behavior based on the prompt"
    )
If you want me to walk you through anything else or provide some tailored examples, just let me know — I’m happy to help! Sources: Testing and evaluation | LiveKit Docs | Using the Testing Framework with Realtime Models | Realtime model integrations | LiveKit Docs | Adjusting Model Parameters | LiveKit Docs | OpenAI Realtime API integration guide | LiveKit Docs
w
TypeError: 'AgentSession' object does not support the asynchronous context manager protocol
t
Hey there! That error
TypeError: 'AgentSession' object does not support the asynchronous context manager protocol
usually pops up if the
AgentSession
isn’t being used quite right or if the library version you have doesn’t support it yet. Here’s a quick way to tackle it: 1. Double-check that you’re using the right version of the LiveKit library that actually supports asynchronous context management for
AgentSession
. 2. Make sure you’re importing and using
AgentSession
the right way. For example:
Copy code
from livekit import AgentSession
1. See if your version of the library needs you to initialize
AgentSession
with some extra parameters or in a specific way. If you’re still stuck, feel free to share the exact bit of code where you’re setting up and using
AgentSession
. I’d be happy to take a closer look and help you sort it out!