how to unit test newly introduced workflows?
# ask-ai
b
how to unit test newly introduced workflows?
t
Thanks for reaching out, mateuszkasprowicz. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To unit test your newly updated workflows in LiveKit, here’s a simple guide to help you through the process: • Write Tests: Try using
pytest
with async support to simulate agent sessions and user inputs. This way, you can check that your workflows produce the right outputs and handle any exceptions smoothly. • Run Locally: Start by installing the necessary dependencies with
pip install -r requirements.txt
or
uv sync
. Then, you can run your tests easily using
python -m pytest -sv ./tests
. • CI Integration: Don’t forget to include your tests in the GitHub Actions pipeline. This ensures your unit tests run automatically, your workflows are evaluated, and your code quality stays top-notch. If you’d like, I’m here to provide specific examples or walk you through any part of this. Just let me know! Sources: Agent Testing & CI/CD | livekit/livekit_composite | DeepWiki | tests/test_workflows.py | sample-livestream/src/test/java/io/livekit/android/sample/livestream/Exampl... | .circleci/config.yml
b
I meant how to test workflow like that (AgentTask) from livekit.agents import AgentTask, function_tool class CollectConsent(AgentTask[bool]): def __init__(self, chat_ctx=None): super().__init__( instructions=""" Ask for recording consent and get a clear yes or no answer. Be polite and professional. """, chat_ctx=chat_ctx, ) async def on_enter(self) -> None: await self.session.generate_reply( instructions=""" Briefly introduce yourself, then ask for permission to record the call for quality assurance and training purposes. Make it clear that they can decline. """ ) @function_tool async def consent_given(self) -> None: """Use this when the user gives consent to record.""" self.complete(True) @function_tool async def consent_denied(self) -> None: """Use this when the user denies consent to record.""" self.complete(False)
t
To unit test your custom
AgentTask
workflow like
CollectConsent
, here’s a friendly guide to help you through the process: • Set Up: Start by using
AgentSession
with a compatible LLM, such as OpenAI GPT models. • Simulate Task: Kick off the task by running
await sess.start(CollectConsent())
. • Provide Input: You can simulate user responses easily with
await sess.run(user_input="Yes, you can record")
. • Assert Results: Finally, check the task’s output with assertions like
assert result.final_output is True
if consent was given, or
assert result.final_output is False
if it was denied. Here’s a simple example to illustrate:
Copy code
import pytest
from livekit.agents import AgentSession
from livekit.plugins import openai

@pytest.mark.asyncio
async def test_collect_consent():
    async with openai.LLM(model="gpt-4o") as llm, AgentSession(llm=llm) as sess:
        await sess.start(CollectConsent())

        result = await sess.run(user_input="Yes, I agree to be recorded")
        assert result.final_output is True

        # Test denial case
        await sess.start(CollectConsent())
        result = await sess.run(user_input="No, I do not want to be recorded")
        assert result.final_output is False
If you have any questions or want to explore specific scenarios, please don’t hesitate to reach out—I’m here to help! Sources: Workflows | LiveKit docs | examples/frontdesk/test_agent.py
b
RuntimeError: Attempted to use an http session outside of a job context. This is probably because you are trying to use a plugin without using the agent worker api. You may need to create your own aiohttp.ClientSession, pass it into the plugin constructor as a kwarg, and manage its lifecycle. tts = agents.tts.StreamAdapter( tts=openai.TTS(model="gpt-4o-mini-tts", voice=voice, speed=1) ) stt = deepgram.STT(model="nova-2") default_llm_kwargs = {"model": "gpt-5-mini", "tool_choice": "auto"} if _llm_kwargs_: default_llm_kwargs.update(_llm_kwargs_) llm = openai.LLM(**default_llm_kwargs) vad = silero.VAD.load( _min_silence_duration_=1.2, _activation_threshold_=0.45, _min_speech_duration_=0.05, _prefix_padding_duration_=0.5, ) can I test this pipeline?
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!
b
I meant can I use this pipeline for unit testing tts = agents.tts.StreamAdapter( tts=openai.TTS(model="gpt-4o-mini-tts", voice=voice, speed=1) ) stt = deepgram.STT(model="nova-2") default_llm_kwargs = {"model": "gpt-5-mini", "tool_choice": "auto"} if _llm_kwargs_: default_llm_kwargs.update(_llm_kwargs_) llm = openai.LLM(**default_llm_kwargs) vad = silero.VAD.load( _min_silence_duration_=1.2, _activation_threshold_=0.45, _min_speech_duration_=0.05, _prefix_padding_duration_=0.5, )