From efe542146b58b4ffb79d767f65d6e8ff10ec47be Mon Sep 17 00:00:00 2001 From: James Hills <70035505+jhills20@users.noreply.github.com> Date: Fri, 6 Jun 2025 11:21:35 -0700 Subject: [PATCH] Return valid JSON for handoff transfers --- src/agents/handoffs.py | 4 ++-- tests/test_handoff_tool.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/agents/handoffs.py b/src/agents/handoffs.py index 50dd417a2..76c93a298 100644 --- a/src/agents/handoffs.py +++ b/src/agents/handoffs.py @@ -1,6 +1,7 @@ from __future__ import annotations import inspect +import json from collections.abc import Awaitable from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Callable, Generic, cast, overload @@ -99,8 +100,7 @@ class Handoff(Generic[TContext]): """ def get_transfer_message(self, agent: Agent[Any]) -> str: - base = f"{{'assistant': '{agent.name}'}}" - return base + return json.dumps({"assistant": agent.name}) @classmethod def default_tool_name(cls, agent: Agent[Any]) -> str: diff --git a/tests/test_handoff_tool.py b/tests/test_handoff_tool.py index a2a06208f..0a8f064f1 100644 --- a/tests/test_handoff_tool.py +++ b/tests/test_handoff_tool.py @@ -1,3 +1,4 @@ +import json from typing import Any import pytest @@ -276,3 +277,10 @@ def test_handoff_input_schema_is_strict(): "additionalProperties" in obj.input_json_schema and not obj.input_json_schema["additionalProperties"] ), "Input schema should be strict and have additionalProperties=False" + + +def test_get_transfer_message_is_valid_json() -> None: + agent = Agent(name="foo") + obj = handoff(agent) + transfer = obj.get_transfer_message(agent) + assert json.loads(transfer) == {"assistant": agent.name}