MCPcopy
hub / github.com/openai/swarm / test_handoff

Function test_handoff

tests/test_core.py:118–145  ·  view source on GitHub ↗
(mock_openai_client: MockOpenAIClient)

Source from the content-addressed store, hash-verified

116
117
118def test_handoff(mock_openai_client: MockOpenAIClient):
119 def transfer_to_agent2():
120 return agent2
121
122 agent1 = Agent(name="Test Agent 1", functions=[transfer_to_agent2])
123 agent2 = Agent(name="Test Agent 2")
124
125 # set mock to return a response that triggers the handoff
126 mock_openai_client.set_sequential_responses(
127 [
128 create_mock_response(
129 message={"role": "assistant", "content": ""},
130 function_calls=[{"name": "transfer_to_agent2"}],
131 ),
132 create_mock_response(
133 {"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT}
134 ),
135 ]
136 )
137
138 # set up client and run
139 client = Swarm(client=mock_openai_client)
140 messages = [{"role": "user", "content": "I want to talk to agent 2"}]
141 response = client.run(agent=agent1, messages=messages)
142
143 assert response.agent == agent2
144 assert response.messages[-1]["role"] == "assistant"
145 assert response.messages[-1]["content"] == DEFAULT_RESPONSE_CONTENT

Callers

nothing calls this directly

Calls 5

runMethod · 0.95
AgentClass · 0.90
create_mock_responseFunction · 0.90
SwarmClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…