Skip to content

Commit 180609c

Browse files
committed
fix: include handoffs when deciding to inject JSON
prompt The JSON prompt injection was only triggered when tools list was non-empty, but handoffs are converted to function tools and added separately. This meant that agents using only handoffs with output_schema would not get the prompt injection even when enable_structured_output_with_tools=True, causing Gemini to error with 'Function calling with response mime type application/json is unsupported.' Changes: - Combine tools and handoffs before checking if JSON prompt should be injected - Add test case for handoffs-only scenario - Update inline comment to clarify why handoffs must be included This ensures the opt-in flag works correctly for multi-agent scenarios where an agent might use handoffs without regular tools.
1 parent cfcd777 commit 180609c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/agents/extensions/models/litellm_model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,12 @@ async def _fetch_response(
292292

293293
# Check if we need to inject JSON output prompt for models that don't support
294294
# tools + structured output simultaneously (like Gemini)
295+
# Note: handoffs are converted to function tools, so we need to include them in the check
296+
tools_and_handoffs = list(tools) if tools else []
297+
if handoffs:
298+
tools_and_handoffs.extend(handoffs)
295299
inject_json_prompt = should_inject_json_prompt(
296-
output_schema, tools, self.enable_structured_output_with_tools
300+
output_schema, tools_and_handoffs, self.enable_structured_output_with_tools
297301
)
298302
if inject_json_prompt and output_schema:
299303
json_prompt = get_json_output_prompt(output_schema)

tests/utils/test_prompts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,14 @@ def test_should_inject_json_prompt_multiple_tools():
105105
]
106106
result = should_inject_json_prompt(schema, tools, enable_structured_output_with_tools=True)
107107
assert result is True
108+
109+
110+
def test_should_inject_json_prompt_with_handoffs_as_tools():
111+
"""Test that handoffs (passed as tools) trigger injection when enabled."""
112+
schema = AgentOutputSchema(SimpleModel)
113+
# Simulate handoffs being passed in the tools list
114+
handoffs_as_tools = [{"type": "function", "name": "handoff_to_agent"}]
115+
result = should_inject_json_prompt(
116+
schema, handoffs_as_tools, enable_structured_output_with_tools=True
117+
)
118+
assert result is True

0 commit comments

Comments
 (0)