()
| 758 | |
| 759 | @pytest.mark.asyncio |
| 760 | async def test_structured_output(): |
| 761 | model = FakeModel() |
| 762 | agent_1 = Agent( |
| 763 | name="test", |
| 764 | model=model, |
| 765 | tools=[get_function_tool("bar", "bar_result")], |
| 766 | output_type=Foo, |
| 767 | ) |
| 768 | |
| 769 | agent_2 = Agent( |
| 770 | name="test", |
| 771 | model=model, |
| 772 | tools=[get_function_tool("foo", "foo_result")], |
| 773 | handoffs=[agent_1], |
| 774 | ) |
| 775 | |
| 776 | model.add_multiple_turn_outputs( |
| 777 | [ |
| 778 | # First turn: a tool call |
| 779 | [get_function_tool_call("foo", json.dumps({"bar": "baz"}))], |
| 780 | # Second turn: a message and a handoff |
| 781 | [get_text_message("a_message"), get_handoff_tool_call(agent_1)], |
| 782 | # Third turn: tool call with preamble message |
| 783 | [ |
| 784 | get_text_message(json.dumps(Foo(bar="preamble"))), |
| 785 | get_function_tool_call("bar", json.dumps({"bar": "baz"})), |
| 786 | ], |
| 787 | # Fourth turn: structured output |
| 788 | [get_final_output_message(json.dumps(Foo(bar="baz")))], |
| 789 | ] |
| 790 | ) |
| 791 | |
| 792 | result = Runner.run_streamed( |
| 793 | agent_2, |
| 794 | input=[ |
| 795 | get_text_input_item("user_message"), |
| 796 | get_text_input_item("another_message"), |
| 797 | ], |
| 798 | run_config=RunConfig(nest_handoff_history=True), |
| 799 | ) |
| 800 | async for _ in result.stream_events(): |
| 801 | pass |
| 802 | |
| 803 | assert result.final_output == Foo(bar="baz") |
| 804 | assert len(result.raw_responses) == 4, "should have four model responses" |
| 805 | assert len(result.to_input_list()) == 10, ( |
| 806 | "should have input: conversation summary, function call, function call result, message, " |
| 807 | "handoff, handoff output, preamble message, tool call, tool call result, final output" |
| 808 | ) |
| 809 | assert len(result.to_input_list(mode="normalized")) == 6, ( |
| 810 | "should have normalized replay input: conversation summary, carried-forward message, " |
| 811 | "preamble message, tool call, tool call result, final output" |
| 812 | ) |
| 813 | |
| 814 | assert result.last_agent == agent_1, "should have handed off to agent_1" |
| 815 | assert result.final_output == Foo(bar="baz"), "should have structured output" |
| 816 | |
| 817 |
nothing calls this directly
no test coverage detected