async def test_invalid_handoff():
    """Test handling of invalid agent handoff."""
    agent1 = FunctionAgent(
        name="agent1",
        description="test",
        llm=MockLLM(
            responses=[
                ChatMessage(
                    role=MessageRole.ASSISTANT,
                    content="handoff invalid_agent Because reasons",
                    additional_kwargs={
                        "tool_calls": [
                            ToolSelection(
                                tool_id="one",
                                tool_name="handoff",
                                tool_kwargs={
                                    "to_agent": "invalid_agent",
                                    "reason": "Because reasons",
                                },
                            )
                        ]
                    },
                ),
                ChatMessage(role=MessageRole.ASSISTANT, content="guess im stuck here"),
            ],
        ),
    )

    agent2 = FunctionAgent(
        **agent1.model_dump(exclude={"llm"}), llm=MockLLM(responses=[])
    )
    agent2.name = "agent2"

    workflow = AgentWorkflow(
        agents=[agent1, agent2],
        root_agent="agent1",
    )

    handler = workflow.run(user_msg="test")
    events = []
    async for event in handler.stream_events():
        events.append(event)

    response = await handler
    assert "Agent invalid_agent not found" in str(events)
Test handling of invalid agent handoff.
llama-index-core/tests/agent/workflow/test_multi_agent_workflow.py