|
| 1 | +import agents |
| 2 | +import pytest |
| 3 | +from agents import Agent, GuardrailFunctionOutput, InputGuardrail, Runner |
| 4 | +from pydantic import BaseModel |
| 5 | + |
| 6 | +from weave.integrations.openai_agents.openai_agents import WeaveTracingProcessor |
| 7 | +from weave.trace.weave_client import WeaveClient |
| 8 | + |
| 9 | +# TODO: Responses should be updated once we have patching for the new Responses API |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def setup_tests(): |
| 14 | + # This is required because OpenAI by default adds its own trace processor which causes issues in the test. |
| 15 | + # We can't just add our trace processor with autopatching because it wont remove the OpenAI trace processor. |
| 16 | + # Instead, we manually set the trace processors to just be ours. This simplifies testing. |
| 17 | + # However, by default the autopatching keeps the default OpenAI trace processor, and additionally installs the Weave processor. |
| 18 | + |
| 19 | + agents.set_trace_processors([WeaveTracingProcessor()]) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.skip_clickhouse_client |
| 23 | +@pytest.mark.vcr( |
| 24 | + filter_headers=["authorization"], |
| 25 | + allowed_hosts=["api.wandb.ai", "localhost"], |
| 26 | +) |
| 27 | +def test_openai_agents_quickstart(client: WeaveClient, setup_tests) -> None: |
| 28 | + agent = Agent(name="Assistant", instructions="You are a helpful assistant") |
| 29 | + |
| 30 | + result = Runner.run_sync(agent, "Write a haiku about recursion in programming.") |
| 31 | + calls = client.get_calls() |
| 32 | + |
| 33 | + assert len(calls) == 3 |
| 34 | + |
| 35 | + trace_root = calls[0] |
| 36 | + trace_root.inputs["name"] = "Agent workflow" |
| 37 | + trace_root.output["status"] = "completed" |
| 38 | + trace_root.output["metrics"] = {} |
| 39 | + trace_root.output["metadata"] = {} |
| 40 | + |
| 41 | + agent_call = calls[1] |
| 42 | + agent_call.inputs["name"] = "Assistant" |
| 43 | + agent_call.output["output"] = None |
| 44 | + agent_call.output["metrics"] = {} |
| 45 | + agent_call.output["metadata"] = {"tools": [], "handoffs": [], "output_type": "str"} |
| 46 | + agent_call.output["error"] = None |
| 47 | + |
| 48 | + response_call = calls[2] |
| 49 | + response_call.inputs["name"] = "Response" |
| 50 | + response_call.inputs["input"] = [ |
| 51 | + { |
| 52 | + "content": "Write a haiku about recursion in programming.", |
| 53 | + "role": "user", |
| 54 | + } |
| 55 | + ] |
| 56 | + |
| 57 | + val = response_call.output["output"][0] |
| 58 | + assert val.role == "assistant" |
| 59 | + assert val.type == "message" |
| 60 | + assert val.status == "completed" |
| 61 | + assert ( |
| 62 | + val.content[0].text |
| 63 | + == "Code calls to itself, \nInfinite loops in silence, \nPatterns emerge clear." |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.skip( |
| 68 | + reason="This test works, but the order of requests to OpenAI can be mixed up (by the Agent framework). This causes the test to fail more than reasonable in CI." |
| 69 | +) |
| 70 | +@pytest.mark.skip_clickhouse_client |
| 71 | +@pytest.mark.vcr( |
| 72 | + filter_headers=["authorization"], |
| 73 | + allowed_hosts=["api.wandb.ai", "localhost"], |
| 74 | +) |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_openai_agents_quickstart_homework( |
| 77 | + client: WeaveClient, setup_tests |
| 78 | +) -> None: |
| 79 | + class HomeworkOutput(BaseModel): |
| 80 | + is_homework: bool |
| 81 | + reasoning: str |
| 82 | + |
| 83 | + guardrail_agent = Agent( |
| 84 | + name="Guardrail check", |
| 85 | + instructions="Check if the user is asking about homework.", |
| 86 | + output_type=HomeworkOutput, |
| 87 | + ) |
| 88 | + |
| 89 | + math_tutor_agent = Agent( |
| 90 | + name="Math Tutor", |
| 91 | + handoff_description="Specialist agent for math questions", |
| 92 | + instructions="You provide help with math problems. Explain your reasoning at each step and include examples", |
| 93 | + ) |
| 94 | + |
| 95 | + history_tutor_agent = Agent( |
| 96 | + name="History Tutor", |
| 97 | + handoff_description="Specialist agent for historical questions", |
| 98 | + instructions="You provide assistance with historical queries. Explain important events and context clearly.", |
| 99 | + ) |
| 100 | + |
| 101 | + async def homework_guardrail(ctx, agent, input_data): |
| 102 | + result = await Runner.run(guardrail_agent, input_data, context=ctx.context) |
| 103 | + final_output = result.final_output_as(HomeworkOutput) |
| 104 | + return GuardrailFunctionOutput( |
| 105 | + output_info=final_output, |
| 106 | + tripwire_triggered=not final_output.is_homework, |
| 107 | + ) |
| 108 | + |
| 109 | + triage_agent = Agent( |
| 110 | + name="Triage Agent", |
| 111 | + instructions="You determine which agent to use based on the user's homework question", |
| 112 | + handoffs=[history_tutor_agent, math_tutor_agent], |
| 113 | + input_guardrails=[ |
| 114 | + InputGuardrail(guardrail_function=homework_guardrail), |
| 115 | + ], |
| 116 | + ) |
| 117 | + |
| 118 | + result = await Runner.run( |
| 119 | + triage_agent, "who was the first president of the united states?" |
| 120 | + ) |
| 121 | + with pytest.raises(agents.exceptions.InputGuardrailTripwireTriggered): |
| 122 | + result = await Runner.run(triage_agent, "what is life") |
| 123 | + |
| 124 | + ##################### |
| 125 | + ### Result1 Block ### |
| 126 | + ##################### |
| 127 | + |
| 128 | + calls = client.get_calls() |
| 129 | + assert len(calls) == 14 |
| 130 | + |
| 131 | + # ==================== |
| 132 | + call0 = calls[0] |
| 133 | + assert call0.inputs["name"] == "Agent workflow" |
| 134 | + assert call0.output["status"] == "completed" |
| 135 | + assert call0.output["metrics"] == {} |
| 136 | + assert call0.output["metadata"] == {} |
| 137 | + |
| 138 | + # ==================== |
| 139 | + call1 = calls[1] |
| 140 | + assert call1.inputs["name"] == "Triage Agent" |
| 141 | + assert call1.output["output"] is None |
| 142 | + assert call1.output["metrics"] == {} |
| 143 | + assert call1.output["metadata"]["tools"] == [] |
| 144 | + assert call1.output["metadata"]["handoffs"] == ["History Tutor", "Math Tutor"] |
| 145 | + assert call1.output["metadata"]["output_type"] == "str" |
| 146 | + assert call1.output["error"] is None |
| 147 | + |
| 148 | + # ==================== |
| 149 | + call2 = calls[2] |
| 150 | + assert call2.inputs["name"] == "homework_guardrail" |
| 151 | + assert call2.output["output"] is None |
| 152 | + assert call2.output["metrics"] == {} |
| 153 | + assert call2.output["metadata"] == {"triggered": False} |
| 154 | + assert call2.output["error"] is None |
| 155 | + |
| 156 | + # ==================== |
| 157 | + call3 = calls[3] |
| 158 | + assert call3.inputs["name"] == "Guardrail check" |
| 159 | + assert call3.output["output"] is None |
| 160 | + assert call3.output["metrics"] == {} |
| 161 | + assert call3.output["metadata"]["tools"] == [] |
| 162 | + assert call3.output["metadata"]["handoffs"] == [] |
| 163 | + assert call3.output["metadata"]["output_type"] == "HomeworkOutput" |
| 164 | + assert call3.output["error"] is None |
| 165 | + |
| 166 | + # ==================== |
| 167 | + call4 = calls[4] |
| 168 | + assert call4.inputs["name"] == "Response" |
| 169 | + assert ( |
| 170 | + call4.inputs["input"][0]["content"] |
| 171 | + == "who was the first president of the united states?" |
| 172 | + ) |
| 173 | + assert call4.inputs["input"][0]["role"] == "user" |
| 174 | + |
| 175 | + val4 = call4.output["output"][0] |
| 176 | + assert val4.name == "transfer_to_history_tutor" |
| 177 | + assert val4.type == "function_call" |
| 178 | + assert val4.status == "completed" |
| 179 | + |
| 180 | + # ==================== |
| 181 | + call5 = calls[5] |
| 182 | + assert call5.inputs["name"] == "Handoff" |
| 183 | + assert call5.output["output"] is None |
| 184 | + assert call5.output["metrics"] == {} |
| 185 | + assert call5.output["metadata"]["from_agent"] == "Triage Agent" |
| 186 | + assert call5.output["metadata"]["to_agent"] == "History Tutor" |
| 187 | + assert call5.output["error"] is None |
| 188 | + |
| 189 | + # ==================== |
| 190 | + call6 = calls[6] |
| 191 | + assert call6.inputs["name"] == "Response" |
| 192 | + assert ( |
| 193 | + call6.inputs["input"][0]["content"] |
| 194 | + == "who was the first president of the united states?" |
| 195 | + ) |
| 196 | + assert call6.inputs["input"][0]["role"] == "user" |
| 197 | + |
| 198 | + val6 = call6.output["output"][0] |
| 199 | + assert val6.role == "assistant" |
| 200 | + assert val6.type == "message" |
| 201 | + assert val6.status == "completed" |
| 202 | + |
| 203 | + # ==================== |
| 204 | + call7 = calls[7] |
| 205 | + assert call7.inputs["name"] == "History Tutor" |
| 206 | + assert call7.output["output"] is None |
| 207 | + assert call7.output["metrics"] == {} |
| 208 | + assert call7.output["metadata"]["tools"] == [] |
| 209 | + assert call7.output["metadata"]["handoffs"] == [] |
| 210 | + assert call7.output["metadata"]["output_type"] == "str" |
| 211 | + assert call7.output["error"] is None |
| 212 | + |
| 213 | + # ==================== |
| 214 | + call8 = calls[8] |
| 215 | + assert call8.inputs["name"] == "Response" |
| 216 | + assert ( |
| 217 | + call8.inputs["input"][0]["content"] |
| 218 | + == "who was the first president of the united states?" |
| 219 | + ) |
| 220 | + assert call8.inputs["input"][0]["role"] == "user" |
| 221 | + assert call8.inputs["input"][1]["name"] == "transfer_to_history_tutor" |
| 222 | + assert call8.inputs["input"][1]["type"] == "function_call" |
| 223 | + assert call8.inputs["input"][1]["status"] == "completed" |
| 224 | + |
| 225 | + val8 = call8.output["output"][0] |
| 226 | + assert val8.role == "assistant" |
| 227 | + assert val8.type == "message" |
| 228 | + assert val8.status == "completed" |
| 229 | + |
| 230 | + ##################### |
| 231 | + ### Result2 Block ### |
| 232 | + ##################### |
| 233 | + |
| 234 | + call9 = calls[9] |
| 235 | + assert call9.inputs["name"] == "Agent workflow" |
| 236 | + assert call9.output["status"] == "completed" |
| 237 | + assert call9.output["metrics"] == {} |
| 238 | + assert call9.output["metadata"] == {} |
| 239 | + |
| 240 | + # ==================== |
| 241 | + call10 = calls[10] |
| 242 | + assert call10.inputs["name"] == "Triage Agent" |
| 243 | + assert call10.output["output"] is None |
| 244 | + assert call10.output["metrics"] == {} |
| 245 | + assert call10.output["metadata"]["tools"] == [] |
| 246 | + assert call10.output["metadata"]["handoffs"] == ["History Tutor", "Math Tutor"] |
| 247 | + assert call10.output["metadata"]["output_type"] == "str" |
| 248 | + |
| 249 | + # ==================== |
| 250 | + call11 = calls[11] |
| 251 | + assert call11.inputs["name"] == "homework_guardrail" |
| 252 | + assert call11.output["output"] is None |
| 253 | + assert call11.output["metrics"] == {} |
| 254 | + assert call11.output["metadata"]["triggered"] is True |
| 255 | + assert call11.output["error"] is None |
| 256 | + |
| 257 | + # ==================== |
| 258 | + call12 = calls[12] |
| 259 | + assert call12.inputs["name"] == "Guardrail check" |
| 260 | + assert call12.output["output"] is None |
| 261 | + assert call12.output["metrics"] == {} |
| 262 | + assert call12.output["metadata"]["tools"] == [] |
| 263 | + assert call12.output["metadata"]["handoffs"] == [] |
| 264 | + assert call12.output["metadata"]["output_type"] == "HomeworkOutput" |
| 265 | + |
| 266 | + # ==================== |
| 267 | + call13 = calls[13] |
| 268 | + assert call13.inputs["name"] == "Response" |
| 269 | + assert call13.inputs["input"][0]["content"] == "what is life" |
| 270 | + assert call13.inputs["input"][0]["role"] == "user" |
| 271 | + |
| 272 | + val13 = call13.output["output"][0] |
| 273 | + assert val13.role == "assistant" |
| 274 | + assert val13.type == "message" |
| 275 | + assert val13.status == "completed" |
0 commit comments