adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

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

Open surfai opened this issue 8 months ago • 25 comments

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

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

surfai avatar Mar 30 '26 04:03 surfai

Further testing across models:

Model output_schema + 7 tools Tool loop? Structured output produced?
gemini-2.5-flash HalOutput (BaseModel) Yes — loops on check_backend_health ~15x, eventually breaks out Yes, after loop
gemini-3-flash-preview HalOutput (BaseModel) Yes — 47 model calls, never terminates within 300s No
gemini-3.1-pro-preview HalOutput (BaseModel) No — 1 model call Yes, first attempt

gemini-3.1-pro-preview handles output_schema + tools correctly on first call. The flash models (both 2.5 and 3.0) are the ones susceptible to the infinite loop.

This suggests the issue is specific to the flash model family's handling of set_model_response when other tools are available — the flash models prefer calling tools over producing the structured output.

nino-robotfutures-co avatar Mar 30 '26 12:03 nino-robotfutures-co

@surfai Traced this down to _OutputSchemaRequestProcessor — the instruction injected for primitive output_schema types isn't explicit enough, so the model keeps looping back to other tools instead of calling set_model_response to terminate. That's what's driving the cycle.

Swapping in a stricter instruction for non-BaseModel schemas seems to do the trick on my end — something along the lines of "CRITICAL: call set_model_response exactly once as your FINAL action, do not call any other tools after". Could you try that workaround and see if it holds up in your setup too?

If it does, it'd be worth running it through your test cases before we push a fix!

surajksharma07 avatar Mar 30 '26 17:03 surajksharma07

Thanks @nino-robotfutures-co for the model comparison table — that data was key to understanding this. And @surajksharma07 for tracing it to _OutputSchemaRequestProcessor and offering a direction.

We ran into this issue building a multi-agent Workflow (ADK 2.0.0a2) and worked around it by removing output_schema from any agent that also has tools. Went back and traced the code path to understand the mechanism — a few findings that might help:

1. The issue is non-deterministic — it's LLM behavior, not a code path bug.

The ADK code is fully deterministic: _OutputSchemaRequestProcessor always adds the set_model_response tool + instruction, and execute_tools always routes back to call_llm when the LLM produces function calls. Whether the loop terminates depends entirely on whether the LLM chooses to call set_model_response vs. other tools. @nino-robotfutures-co's table confirms this — same code, different models, different outcomes.

2. The instruction is identical for all schema types — and it's an ADK internal, not user-facing.

@surajksharma07 mentions the instruction for primitive types isn't explicit enough. Looking at _output_schema_processor.py:57-64, the instruction text is actually the same for str, BaseModel, and all other schema types — no branching by type. It's also hardcoded inside ADK's request processor pipeline (_create_workflow_request_processors in _call_llm_node.py), which is a fixed list of internal processors with no public extension point. Users can't customize or override this instruction without monkey-patching.

What does differ by type is the set_model_response tool signature the LLM sees:

  • BaseModel → set_model_response(*, summary: str, score: int) — distinct named fields
  • str → set_model_response(*, response: str) — single generic field

Flash models may treat the trivial single-field signature as redundant ("I can just respond with text") rather than recognizing it as a required termination mechanism. The named fields in the BaseModel case likely act as a stronger signal.

A type-aware instruction could help — differentiating the emphasis by schema complexity:

# _output_schema_processor.py:52-64 — proposed type-aware instruction
from ..utils._schema_utils import is_basemodel_schema

if is_basemodel_schema(agent.output_schema):
    instruction = (
        'After completing any needed tool calls, provide your final '
        'response by calling set_model_response with the required fields.'
    )
else:
    instruction = (
        'CRITICAL: You MUST call the set_model_response tool exactly once '
        'as your FINAL action. Do NOT respond with plain text. Do NOT call '
        'any other tools after set_model_response. This is the ONLY way '
        'to complete the task.'
    )

3. A stricter instruction improves probability but can't guarantee prevention.

Since the root cause is LLM tool-selection behavior (not a code bug), a prompt change is a probabilistic improvement — it may help flash models comply more often, but there's no instruction that can guarantee a model will always call a specific tool. We can't meaningfully validate this on our end either: passing 5 or 10 times doesn't prove it won't fail on attempt 11, especially since behavior varies by model, temperature, and context length.

A deterministic safety net would complement the instruction fix — two layers:

Layer 1: Force tool_choice on round N-1 — constrain the LLM to only set_model_response, guaranteeing a valid structured response:

# _execute_tools_node.py — at the end of execute_tools()
_MAX_TOOL_ROUNDS = 25

round_key = 'temp:tool_round_count'
rounds = ctx.state.get(round_key, 0) + 1
ctx.state[round_key] = rounds

if rounds >= _MAX_TOOL_ROUNDS - 1:
    # Next call_llm round: force the model to call set_model_response
    ctx.state['temp:force_set_model_response'] = True
yield _continue_event()
# _output_schema_processor.py — read the flag during the next call_llm cycle
if ctx.state.get('temp:force_set_model_response'):
    llm_request.config.tool_config = types.ToolConfig(
        function_calling_config=types.FunctionCallingConfig(
            mode='ANY',
            allowed_function_names=['set_model_response'],
        )
    )

Layer 2: Hard cutoff on round N — last-resort termination if the forced tool_choice still doesn't produce output:

# _execute_tools_node.py — above the tool_choice flag check
if rounds >= _MAX_TOOL_ROUNDS:
    logger.error(
        'Tool execution reached %d rounds without termination, '
        'breaking loop to prevent runaway API costs.',
        _MAX_TOOL_ROUNDS,
    )
    return  # End workflow — don't route back to call_llm

The stricter instruction (point 2) reduces how often the guard is needed; the tool_choice enforcement ensures valid output when the model won't comply; the hard cutoff prevents runaway costs in all cases. All three together would be robust.

4. Secondary: missing return after successful set_model_response in _execute_tools_node.py.

Separate from the infinite loop — when set_model_response IS successfully called, the final_event is yielded (line 364) but there's no return. Execution falls through to yield _continue_event() (line 389), causing one unnecessary extra call_llm round trip even on success:

# _execute_tools_node.py:360-364 — proposed fix
if json_response:
    final_event = _output_schema_processor.create_final_model_response_event(
        invocation_context, json_response
    )
    yield final_event.model_copy()
    return  # Structured output produced — terminate loop

surfai avatar Mar 31 '26 07:03 surfai

Follow-up: two architectural patterns that avoid this problem

While investigating we came across two patterns that reframe this from a non-deterministic instruction-following problem into a deterministic routing problem:

Pattern A: Run tools first, then ask for structured output separately.

The agent runs in two phases. In phase 1, the model has access to all its tools and calls them freely — no output schema is involved. The loop continues until the model decides it has enough information and stops making tool calls. Then, in phase 2, the model is re-invoked in a constrained mode: it receives the full conversation history but is bound to produce only the structured output, with no tools available at all. The model can't get confused between "should I call a tool or produce the structured response?" because those two actions never compete in the same LLM call.

Pattern B: Make the schema a tool, but force the model to always pick a tool.

The output schema is registered as an additional tool alongside the real tools (similar to ADK's set_model_response). The key difference: tool_choice is set to "any", which means the model must call at least one tool on every turn — it cannot just produce plain text and move on. On each turn, the model either calls a real tool (to keep working) or calls the schema tool (to finish). When the routing logic sees the schema tool was called, it exits the loop. The model can't silently ignore the schema tool because it's forced to pick something from the tool list every turn.

Both patterns make the termination condition deterministic — it doesn't depend on how well a particular model follows a prompt instruction. The tool_choice guard we suggested in point 3 above is essentially a lightweight version of Pattern B, applied as a safety net rather than the primary mechanism.

surfai avatar Mar 31 '26 08:03 surfai

@surfai Good catch on the instruction text — you're right, I was wrong about that. It's actually the same string across all schema types, no branching at all. The real differentiator is exactly what you pointed out: the tool signature itself. Distinct named fields for BaseModel vs. a single generic response: str that flash models tend to treat as optional — that's where the behavior diverges.

The 2-layer approach makes a lot of sense here: type-aware instruction + tool_choice enforcement as a safety net + hard cutoff. That's what makes this deterministic rather than just prompt-probabilistic, which is the right goal.

The missing return after set_model_response success is a solid find too — definitely worth bundling into the same fix rather than leaving it loose.

Could you drop these findings over on #5057? That PR is already targeting this area, so having the full picture there would really help the reviewer.

surajksharma07 avatar Mar 31 '26 15:03 surajksharma07

@surajksharma07 : Can you provide full sample code with two lays implementation with output schema and tool call.

I have created a bug #3940 for the same issue and had a different work around.

I want to try both solution so can you provide a full same code for above work around.

Thanks, Vasanth

Vasanthan88 avatar Apr 11 '26 14:04 Vasanthan88

@Vasanthan88 — worth separating these two since they have different root causes!

The #3940 issue is a slow-tool timing problem where the LLM retries before the tool response arrives. The fix there is RunConfig(dedupe_tool_calls=True) from PR #4189 — worth checking if that's already landed on your current ADK version since the commit went in back in February.

The #5054 loop is a different story — output_schema=str produces a weak set_model_response signature that flash models tend to treat as optional, which is what drives the infinite loop.

The full 2-layer fix (type-aware instruction + tool_choice enforcement + hard cutoff) is laid out in surfai's comments above with code sketches, and PRs #5057 and #5091 are where that's being implemented.

Try dedupe_tool_calls first for the #3940 side and let us know if that's still open on your end!

surajksharma07 avatar Apr 12 '26 16:04 surajksharma07

@surajksharma07 since I opened PRs #5057 and #5091, just checking is there anything you’d like me to update or adjust to help move them forward?

vietnamesekid avatar Apr 13 '26 09:04 vietnamesekid

@surajksharma07 : still dedupe_tool_call is not released . Checked in version 1.28.1 Google adk version.

Can you prove the same code for layer 1 and layer 2 now so that I can try it. I am also facing this issue after introducing output_schema and tool call

Thanks, Vasanth

Vasanthan88 avatar Apr 13 '26 16:04 Vasanthan88

@vietnamesekid Thanks for staying on top of this — the PRs are really appreciated! A few things worth checking before it goes to review:

  1. surfai flagged a missing return after a successful set_model_response in _execute_tools_node.py (point 4 in their second comment) — without it, execution falls through to yield _continue_event() and fires one unnecessary extra call_llm round even on success. Worth bundling into the same PR.
  2. Whether the type-aware instruction branching is in — the emphasis needs to differ between BaseModel vs primitive str schemas (surfai's point 2).
  3. Whether the tool_choice forced-call guard (force set_model_response on round N-1) is included as the safety net — that's what makes the fix deterministic rather than prompt-probabilistic. If all three are covered, the PRs should be in solid shape. Let me know if you want a second look once those are in!

surajksharma07 avatar Apr 14 '26 16:04 surajksharma07

@Vasanthan88 For dedupe_tool_calls — try upgrading to google-adk==1.29.0, that's where PR #4189 most likely landed for the #3940 side. RunConfig(dedupe_tool_calls=True) should be available there.

For the output_schema + tools loop: the two-layer code surfai posted is a patch to ADK's internal source files (_execute_tools_node.py and _output_schema_processor.py) — it's not standalone project code, but if you want to test it now you can apply those changes directly to your local ADK install in site-packages. Just keep in mind it'll be overwritten on the next pip upgrade, so make a note of the changes somewhere. The safest no-risk option in the meantime is simply removing output_schema from any agent that also has tools — that fully avoids the loop without touching anything internal.

PRs #5057 and #5091 will land the proper fix — once they merge, a pip upgrade to the next ADK version will be the clean path forward!

Team is looking more into it.

surajksharma07 avatar Apr 14 '26 16:04 surajksharma07

@Jacksunwei Please have a look into it once.

surajksharma07 avatar Apr 14 '26 16:04 surajksharma07

Hey @surajksharma07 , all four points are already in there

  1. Type aware instruction BaseModel gets the softer prompt, primitives get the "IMPORTANT: you MUST call" one
  2. Force tool_choice on round N-1 mode=ANY + allowed_function_names=['set_model_response']
  3. Hard cutoff on round N end_invocation = True, stops cold
  4. Early return after set_model_response both spots in base_llm_flow.py

Happy to walk through any of them if something looks off

vietnamesekid avatar Apr 14 '26 16:04 vietnamesekid

Hey @surajksharma07 , all four points are already in there

  1. Type aware instruction BaseModel gets the softer prompt, primitives get the "IMPORTANT: you MUST call" one
  2. Force tool_choice on round N-1 mode=ANY + allowed_function_names=['set_model_response']
  3. Hard cutoff on round N end_invocation = True, stops cold
  4. Early return after set_model_response both spots in base_llm_flow.py

Happy to walk through any of them if something looks off

One clarification on point 4 the fix isn't in _execute_tools_node.py (that's the sketch architecture from surfai's comment). The actual ADK codebase doesn't have that file. The equivalent fix lives in base_llm_flow.py return is added at both spots after set_model_response is detected, so execution doesn't fall through to the next call_llm round.

Same behavior, different file

vietnamesekid avatar Apr 14 '26 16:04 vietnamesekid

@vietnamesekid Perfect — all four confirmed, and the base_llm_flow.py clarification is helpful. The fix lands where it needs to regardless of surfai's sketch filename, so that's not a concern.

That's exactly the right shape: type-aware instruction for the probabilistic layer, tool_choice + hard cutoff for the deterministic safety net, and the early return to clean up the success path.

Should be solid for @Jacksunwei's review — thanks for the thorough rundown!

Team is looking more into it and it is under review.

surajksharma07 avatar Apr 15 '26 13:04 surajksharma07

@surajksharma07 - I checked in 1.29 still dedupe_tool_calls is not available in RunConfig.

Getting validation error for RunConfig

Vasanthan88 avatar Apr 15 '26 16:04 Vasanthan88

@Vasanthan88 PR #4189 hasn't been merged into any release yet, which is exactly why you're hitting the ValidationError — RunConfig uses extra='forbid' so any unknown field fails Pydantic validation immediately.

For the #3940 slow-tool loop right now, two things that actually work with the current API: use RunConfig(max_llm_calls=5) to hard-cap total LLM rounds per run (prevents infinite burn even if the lock misses), and keep the session-based lock workaround from December as the dedup layer. That combination is the most defensive setup available until #4189 lands.

Team is looking more into it.

surajksharma07 avatar Apr 19 '26 04:04 surajksharma07

Any updates on this, @surajksharma07 ?

vietnamesekid avatar May 15 '26 03:05 vietnamesekid

Separate from the root-cause fix (validation on output_schema=str + tools) — for defense in depth against this class, a runtime iteration/duplicate-call breaker in front of the call_llm → execute_tools cycle stops it becoming an unbounded spend loop while the schema bug is being fixed. I maintain a small framework-agnostic one (decide() with max-iteration + duplicate-call caps): https://github.com/Fame510/SHACKLE. Not a substitute for the fix, just a backstop so a repro like this can't run away.

Fame510 avatar Jul 04 '26 23:07 Fame510

🚨 Automated Spam Detection Alert 🚨 @maintainers, a suspected spam comment was detected in this thread.

Reason:

@Fame510 posted a promotional comment advertising their own 3rd-party repository (https://github.com/Fame510/SHACKLE).

adk-bot avatar Jul 05 '26 07:07 adk-bot