Scenario Testing
This example demonstrates how to use the Scenario framework for agentic simulation-based testing. Scenario enables you to simulate conversations between agents, user simulators, and judges, making it easy to test and evaluate agent behaviors in a controlled environment.
Tip: Want to see a more advanced scenario? Check out the Customer support scenario example for a more complex agent, including tool calls and advanced scenario features.
Code
1import pytest2import scenario3from kern.agent import Agent4from kern.models.openai import OpenAIResponses56# Configure Scenario defaults (model for user simulator and judge)7scenario.configure(default_model="openai/gpt-4.1-mini")89@pytest.mark.agent_test10@pytest.mark.asyncio11async def test_vegetarian_recipe_agent() -> None:12 # 1. Define an AgentAdapter to wrap your agent13 class VegetarianRecipeAgentAdapter(scenario.AgentAdapter):14 agent: Agent1516 def __init__(self) -> None:17 self.agent = Agent(18 model=OpenAIResponses(id="gpt-5.2"),19 markdown=True,20 debug_mode=True,21 instructions="You are a vegetarian recipe agent.",22 )2324 async def call(self, input: scenario.AgentInput) -> scenario.AgentReturnTypes:25 response = self.agent.run(26 input=input.last_new_user_message_str(), # Pass only the last user message27 session_id=input.thread_id, # Pass the thread id, this allows the agent to track history28 )29 return response.content3031 # 2. Run the scenario simulation32 result = await scenario.run(33 name="dinner recipe request",34 description="User is looking for a vegetarian dinner idea.",35 agents=[36 VegetarianRecipeAgentAdapter(),37 scenario.UserSimulatorAgent(),38 scenario.JudgeAgent(39 criteria=[40 "Agent should not ask more than two follow-up questions",41 "Agent should generate a recipe",42 "Recipe should include a list of ingredients",43 "Recipe should include step-by-step cooking instructions",44 "Recipe should be vegetarian and not include any sort of meat",45 ]46 ),47 ],48 )4950 # 3. Assert and inspect the result51 assert result.successUsage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateSet your API key
1export OPENAI_API_KEY=xxx2export LANGWATCH_API_KEY=xxx # Optional, required for Simulation monitoringInstall dependencies
1uv pip install -U openai kern-ai langwatch-scenario pytest pytest-asyncio2# or3uv add kern-ai langwatch-scenario openai pytestRun Agent
1pytest cookbook/agent_basics/other/scenario_testing.py