google / google/adk-python

[BUG]: output_schema=str combined with tools causes infinite call_llm/execute_tools loop

Đang mở
#5,054 23 bình luận 3 reaction 2 người được giao Được @Jacksunwei nhận Xem trên GitHub
core needs review spam
Ngôn ngữ chính
Python
Star
21.6k
Fork
4k
Merge trung bình
1 ngày 22 giờ
Pull request đã merge (30 ngày)
31

Mô tả

## Description

Using bare `str` type as `output_schema` on an `Agent` that also has `tools` causes an infinite `call_llm → execute_tools → call_llm` loop. The LLM repeatedly calls tools instead of producing the final structured response via `set_model_response`.

This does not happen with `BaseModel` subclasses as `output_schema`, only with primitive types like `str`.

## Reproduction

```python
from google.adk import Agent, Workflow, Event

def plan_visualization(approach: str, technology: str, key_elements: list[str]) -> str:
"""Plan a visualization."""
return f"Plan: {approach} using {technology}"

def query_data(query: str) -> list[dict]:
"""Query the database."""
return [{"q": "Q1", "revenue": 100}]

# This agent loops infinitely:
plan_agent = Agent(
name="plan_agent",
model="gemini-2.5-pro",
instruction="Call plan_visualization with approach, technology, and key elements.",
tools=[plan_visualization, query_data],
output_schema=str, # <-- bare str causes the loop
mode="single_turn",
)

# Use in a Workflow or with Runner — agent calls tools repeatedly
# without ever settling on a final string response
```

## Expected Behavior

Either:
1. `output_schema=str` works correctly with tools — the LLM calls tools, then uses `set_model_response` to return a plain string, OR
2. ADK raises a validation error at agent creation time: `"output_schema must be a BaseModel subclass when tools are specified"`

## Actual Behavior

The agent enters an infinite loop:
1. LLM generates a `function_call` (e.g., `plan_visualization`)
2. ADK executes the tool and returns the result
3. LLM generates another `function_call` instead of calling `set_model_response`
4. Repeat endlessly — burns API credits silently

## Analysis

ADK auto-adds `set_model_response` when both `output_schema` and `tools` are specified ([documented here](https://github.com/google/adk-python/blob/v2.0.0a1/contributing/samples/output_schema_with_tools/README.md)). With `BaseModel` schemas, the model correctly calls `set_model_response` with structured JSON. With bare `str`, the `set_model_response` schema may be malformed or confusing to the model, causing it to fall back to repeated tool calls.

## Workaround

Remove `output_schema` from agents that have tools. Use other mechanisms to suppress unwanted text output if needed.

## Related Issues

- #3413 — Agent with output_schema + tools enters infinite loop (model regression, fixed)
- #3969 — Output Schema consistently ignored by LLM Agent with tools
- #4868 — output_schema + Tool with structured arg causes infinite loop

## Environment

- `google-adk` 2.0.0a2
- Model: `gemini-2.5-pro` (also tested with `gemini-2.5-flash`)
- Python 3.14

> Context for this issue was developed with AI assistance (Claude).

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.