google / google/adk-python

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

Abierto
#5,054 23 comentarios 3 reacciones 2 asignados Reclamado por @Jacksunwei Ver en GitHub
core needs review spam
Lenguaje dominante
Python
Estrellas
21.5k
Forks
4k
Merge medio
1 d 22 h
PR fusionados (30 d)
31

Descripción

## 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).

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.