MCPcopy
hub / github.com/openai/openai-agents-python / test_structured_output

Function test_structured_output

tests/test_agent_runner.py:1291–1344  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1289
1290@pytest.mark.asyncio
1291async def test_structured_output():
1292 model = FakeModel()
1293 agent_1 = Agent(
1294 name="test",
1295 model=model,
1296 tools=[get_function_tool("bar", "bar_result")],
1297 output_type=Foo,
1298 )
1299
1300 agent_2 = Agent(
1301 name="test",
1302 model=model,
1303 tools=[get_function_tool("foo", "foo_result")],
1304 handoffs=[agent_1],
1305 )
1306
1307 model.add_multiple_turn_outputs(
1308 [
1309 # First turn: a tool call
1310 [get_function_tool_call("foo", json.dumps({"bar": "baz"}))],
1311 # Second turn: a message and a handoff
1312 [get_text_message("a_message"), get_handoff_tool_call(agent_1)],
1313 # Third turn: tool call with preamble message
1314 [
1315 get_text_message(json.dumps(Foo(bar="preamble"))),
1316 get_function_tool_call("bar", json.dumps({"bar": "baz"})),
1317 ],
1318 # Fourth turn: structured output
1319 [get_final_output_message(json.dumps(Foo(bar="baz")))],
1320 ]
1321 )
1322
1323 result = await Runner.run(
1324 agent_2,
1325 input=[
1326 get_text_input_item("user_message"),
1327 get_text_input_item("another_message"),
1328 ],
1329 run_config=RunConfig(nest_handoff_history=True),
1330 )
1331
1332 assert result.final_output == Foo(bar="baz")
1333 assert len(result.raw_responses) == 4, "should have four model responses"
1334 assert len(result.to_input_list()) == 10, (
1335 "should have input: conversation summary, function call, function call result, message, "
1336 "handoff, handoff output, preamble message, tool call, tool call result, final output"
1337 )
1338 assert len(result.to_input_list(mode="normalized")) == 6, (
1339 "should have normalized replay input: conversation summary, carried-forward message, "
1340 "preamble message, tool call, tool call result, final output"
1341 )
1342
1343 assert result.last_agent == agent_1, "should have handed off to agent_1"
1344 assert result.final_output == Foo(bar="baz"), "should have structured output"
1345
1346
1347def remove_new_items(handoff_input_data: HandoffInputData) -> HandoffInputData:

Callers

nothing calls this directly

Calls 13

AgentClass · 0.90
RunConfigClass · 0.90
FakeModelClass · 0.85
get_function_toolFunction · 0.85
get_function_tool_callFunction · 0.85
get_text_messageFunction · 0.85
get_handoff_tool_callFunction · 0.85
get_final_output_messageFunction · 0.85
get_text_input_itemFunction · 0.85
FooClass · 0.70
runMethod · 0.45

Tested by

no test coverage detected