Skip to content

_rearrange_events_for_latest_function_response attributes a reused function-call id to the oldest matching call, silently truncating history #7269

Closed

Description

@mkbctrl

Summary

When a session's history contains two function calls that share the same id (some model
providers reuse call ids across turns — see below), _rearrange_events_for_latest_function_response
in google/adk/flows/llm_flows/contents.py (relocated on upstream main to
google/adk/flows/llm_flows/tools/_rearranger.py, same function body, as
rearrange_events_for_latest_function_response) attaches the newest function response to the
oldest call event carrying that id, instead of the nearest preceding one.

The consequence is silent, not an exception: every event between the oldest matching call and the
latest response — including intervening user turns — is dropped from the request built for the
model, and the response text ends up merged onto the wrong call.

This is exactly the class of bug that was fixed for the sibling function
_rearrange_events_for_async_function_responses_in_history in commit
deee6d2c47
("fix(flows): pair a function response with the call it answers", closing #6761, in 2.8.0). That
fix attributes each response to the nearest preceding call sharing its id. The same fix was never
applied to _rearrange_events_for_latest_function_response, which has its own, independent walk
over the same kind of history and reproduces the same family of bug.

Affected versions

  • Confirmed on google-adk 2.7.1 and 2.8.0 (installed from PyPI).
  • Confirmed still present on upstream main as of 2026-09-24 (pip install git+https://github.com/google/adk-python, resolved to 2.9.2), where the function lives at
    google/adk/flows/llm_flows/tools/_rearranger.py::rearrange_events_for_latest_function_response.

When it triggers

The latest event in history is a function response that does not directly follow its own call
event — i.e., something sits between the call and its final response. This is the normal shape of
a Human-in-the-Loop (HITL) tool-confirmation resume: ADK stores a placeholder response at the pause
point, then the real response when the tool actually runs, so the "latest response" is never
adjacent to its call.

It only becomes visible when a function-call id is reused across two different calls in the
same session — i.e. an earlier, already-answered call happens to carry the same id as the call this
response answers. Some model providers do this: routing a request through LiteLLM to Gemini 3.x
forwards Gemini's native tool-call ids (call_NNNNNN) unchanged, and Gemini draws those ids from a
small enough space that a long-running session can see the same id issued twice.

Minimal repro

Self-contained script using only google-adk's public Event / google.genai.types surface, plus
the one private function under test (imported from whichever module the installed version keeps it
in):

"""Minimal repro: `_rearrange_events_for_latest_function_response` mis-attributes a reused
function-call id to the OLDEST matching call instead of the nearest one, truncating history.

Uses only google-adk's public `Event`/`google.genai.types` surface, plus the one private
rearranger function under test (imported from wherever the installed version keeps it).

Scenario:
  Turn 1: user "q1" -> model calls `lookup` under id `call_1` -> response.
  Turn 2: user "q2" -> model calls `update`, REUSING id `call_1` -> a placeholder response
          (the HITL pause) -> the real response (the HITL resume).

Expected: rearranging for the latest ("real") response should attribute it to the turn-2
`update` call (the nearest preceding call with that id) and leave turn 1 untouched.

Actual (google-adk 2.8.0 and earlier): the outer backward walk in
`_rearrange_events_for_latest_function_response` never stops at the first (nearest) match,
so it keeps walking past the turn-2 call and settles on the turn-1 `lookup` call instead.
Every event between the turn-1 call and the latest response — including all of turn 2's
user text and the placeholder — is then dropped, and the real "update" response gets merged
onto the wrong (turn-1, `lookup`) call.

A `--control` run uses distinct ids for the two calls and stays intact, isolating the bug to
id reuse rather than to the rearranger's general behavior.
"""

import sys

from google.adk.events.event import Event
from google.genai import types

try:  # google-adk >= 2.8.0 (and earlier): google/adk/flows/llm_flows/contents.py
    from google.adk.flows.llm_flows.contents import (
        _rearrange_events_for_latest_function_response as rearrange,
    )
except ImportError:  # upstream main: relocated to flows/llm_flows/tools/_rearranger.py
    from google.adk.flows.llm_flows.tools._rearranger import (
        rearrange_events_for_latest_function_response as rearrange,
    )


def describe(label: str, events: list[Event]) -> None:
    print(f"--- {label}: {len(events)} events")
    for i, event in enumerate(events):
        calls = [(c.name, c.id) for c in event.get_function_calls()]
        responses = [(r.name, r.id) for r in event.get_function_responses()]
        text = ""
        if event.content and event.content.parts:
            text = "".join(p.text or "" for p in event.content.parts)[:40]
        print(f"  {i:>2} {event.author:<8} calls={calls or ''} responses={responses or ''} {text!r}")


def user(text: str) -> Event:
    return Event(author="user", content=types.Content(role="user", parts=[types.Part(text=text)]))


def call(author: str, name: str, call_id: str) -> Event:
    return Event(
        author=author,
        content=types.Content(
            role="model",
            parts=[types.Part(function_call=types.FunctionCall(id=call_id, name=name, args={}))],
        ),
    )


def response(author: str, name: str, call_id: str, payload: dict) -> Event:
    return Event(
        author=author,
        content=types.Content(
            role="user",
            parts=[
                types.Part(
                    function_response=types.FunctionResponse(id=call_id, name=name, response=payload)
                )
            ],
        ),
    )


def build(reused: bool) -> list[Event]:
    update_id = "call_1" if reused else "call_2"
    return [
        user("q1"),
        call("worker", "lookup", "call_1"),
        response("worker", "lookup", "call_1", {"result": "looked up"}),
        user("q2"),
        call("worker", "update", update_id),
        response("worker", "update", update_id, {"placeholder": True}),  # HITL pause
        response("worker", "update", update_id, {"applied": True}),  # HITL resume
    ]


def run(label: str, reused: bool) -> bool:
    """Correct behavior: turn 1 ('q1', lookup) survives untouched, turn 2's user text
    ('q2') survives, and the final merged response is attributed to the 'update' call.
    """
    events = build(reused)
    print(f"\n=== {label} (reused={reused})")
    describe("IN", events)
    out = rearrange(events)
    describe("OUT", out)

    q2_survived = any(
        event.content and event.content.parts and any(p.text == "q2" for p in event.content.parts)
        for event in out
    )
    merged_responses = out[-1].get_function_responses()
    paired_with_update = all(r.name == "update" for r in merged_responses)

    ok = q2_survived and paired_with_update
    if not ok:
        print(f"BROKEN: q2 text survived={q2_survived}, merged response paired with update call={paired_with_update}")
        if not paired_with_update:
            print(f"  merged response(s) instead read as: {[(r.name, r.id) for r in merged_responses]}")
    else:
        print("OK")
    return ok


if __name__ == "__main__":
    import google.adk

    print(f"google-adk version: {getattr(google.adk, '__version__', 'unknown')}")
    reused_ok = run("reused id (bug case)", reused=True)
    control_ok = run("distinct ids (control)", reused=False)
    print(f"\nreused case: {'PASS' if reused_ok else 'FAIL (truncation bug reproduced)'}")
    print(f"control case: {'PASS' if control_ok else 'FAIL'}")
    sys.exit(0 if reused_ok and control_ok else 1)

Actual output — google-adk 2.8.0 (PyPI)

google-adk version: 2.8.0

=== reused id (bug case) (reused=True)
--- IN: 7 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('lookup', 'call_1')] ''
   3 user     calls= responses= 'q2'
   4 worker   calls=[('update', 'call_1')] responses= ''
   5 worker   calls= responses=[('update', 'call_1')] ''
   6 worker   calls= responses=[('update', 'call_1')] ''
--- OUT: 3 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('update', 'call_1')] ''
BROKEN: q2 text survived=False, merged response paired with update call=True

=== distinct ids (control) (reused=False)
--- IN: 7 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('lookup', 'call_1')] ''
   3 user     calls= responses= 'q2'
   4 worker   calls=[('update', 'call_2')] responses= ''
   5 worker   calls= responses=[('update', 'call_2')] ''
   6 worker   calls= responses=[('update', 'call_2')] ''
--- OUT: 6 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('lookup', 'call_1')] ''
   3 user     calls= responses= 'q2'
   4 worker   calls=[('update', 'call_2')] responses= ''
   5 worker   calls= responses=[('update', 'call_2')] ''
OK

reused case: FAIL (truncation bug reproduced)
control case: PASS

Exit code: 1 (reused case fails; control passes — isolating the bug to id reuse).

Actual output — upstream main (resolved to 2.9.2, installed via pip install git+https://github.com/google/adk-python)

Identical result, via the relocated google.adk.flows.llm_flows.tools._rearranger module:

google-adk version: 2.9.2

=== reused id (bug case) (reused=True)
--- IN: 7 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('lookup', 'call_1')] ''
   3 user     calls= responses= 'q2'
   4 worker   calls=[('update', 'call_1')] responses= ''
   5 worker   calls= responses=[('update', 'call_1')] ''
   6 worker   calls= responses=[('update', 'call_1')] ''
--- OUT: 3 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('update', 'call_1')] ''
BROKEN: q2 text survived=False, merged response paired with update call=True

=== distinct ids (control) (reused=False)
--- IN: 7 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('lookup', 'call_1')] ''
   3 user     calls= responses= 'q2'
   4 worker   calls=[('update', 'call_2')] responses= ''
   5 worker   calls= responses=[('update', 'call_2')] ''
   6 worker   calls= responses=[('update', 'call_2')] ''
--- OUT: 6 events
   0 user     calls= responses= 'q1'
   1 worker   calls=[('lookup', 'call_1')] responses= ''
   2 worker   calls= responses=[('lookup', 'call_1')] ''
   3 user     calls= responses= 'q2'
   4 worker   calls=[('update', 'call_2')] responses= ''
   5 worker   calls= responses=[('update', 'call_2')] ''
OK

reused case: FAIL (truncation bug reproduced)
control case: PASS

Expected vs actual

Expected: the response is attributed to update (turn 2's call, the nearest preceding call
sharing the id), turn 2's user turn ('q2') survives in the rearranged history, and turn 1
(lookup) is left untouched.

Actual: the response is spliced directly after turn 1's lookup call. Turn 2's user turn and
the placeholder response are silently dropped. Nothing raises — the caller gets a shorter,
differently-shaped history back with no indication anything was discarded.

In a real HITL flow built on top of this (tool-confirmation pause/resume, reusing the paused
call's id for the resumed one), this manifests as: the approved write is paired with the wrong
call, the turn that proposed it disappears from what the model sees next, and — depending on how
strictly the downstream provider validates call/response pairing — either a silent context loss or
a hard "no matching tool call" error from the provider.

Root cause

_rearrange_events_for_latest_function_response walks the history backward from the end to find
the call event that the latest response(s) answer:

function_call_event_idx = -1
# look for corresponding function call event reversely
for idx in range(len(events) - 2, -1, -1):
  event = events[idx]
  function_calls = event.get_function_calls()
  if function_calls:
    for function_call in function_calls:
      if function_call.id in function_responses_ids:
        function_call_event_idx = idx
        function_call_ids = {
            function_call.id for function_call in function_calls
        }
        if not function_responses_ids.issubset(function_call_ids):
          raise ValueError(...)
        function_responses_ids = function_call_ids
        break  # <-- only breaks the INNER loop over this event's calls

The break only exits the inner loop over function_calls for the current event. The outer
for idx in range(...) loop has no corresponding break, so once a match is found the walk keeps
going backward through every older event. If an older call event also carries a call with the same
id (the reused-id case), that older match overwrites function_call_event_idx, and the loop
finishes pointed at the oldest matching call rather than the nearest one — the opposite of what
a reverse/"look for the nearest" walk is supposed to produce.

Everything downstream is built from function_call_event_idx, so the bug is a single missing
break (or equivalent early-exit) after the outer loop finds its first match.

This is the same defect class that _rearrange_events_for_async_function_responses_in_history had
before deee6d2c47
("fix(flows): pair a function response with the call it answers", closing #6761, 2.8.0). That fix
built an index of every call event per id and attributed each response to the nearest preceding
call via bisect_left — the general pattern needed anywhere a call id is not assumed unique across
a session. _rearrange_events_for_latest_function_response has its own, separate walk over the
same kind of history and was not touched by that fix.

Suggested fix

Either:

  1. Minimal: add a break (or a sentinel/return) to the outer loop immediately after the
    inner loop sets function_call_event_idx, so the reverse walk stops at the first (nearest) call
    event that carries a matching id — restoring the "look backward, stop at the first match"
    behavior the loop already reads as intending.
  2. Consistent with the sibling fix: adopt the same nearest-preceding-call attribution
    _rearrange_events_for_async_function_responses_in_history now uses (index every call event by
    id, then use bisect_left against the response's position to find the nearest preceding call)
    rather than an unbounded backward scan, so both rearrangers handle reused ids the same way.

Activity

  1. chelsealong commented on Sep 24, 2026

    @chelsealong
    Contributor

    Picking this one up now — opening a PR shortly. Flagging it here so nobody duplicates the work; if someone is already on it, say so and I will drop mine.

  2. added
    agent config[Component] This issue is related to the Agent Config interface and implementation
    agent engine[Component] This issue is related to Vertex AI Agent Engine
    and removed
    agent config[Component] This issue is related to the Agent Config interface and implementation
    on Sep 25, 2026
  3. added
    tools[Component] This issue is related to tools
    and removed
    agent engine[Component] This issue is related to Vertex AI Agent Engine
    on Sep 28, 2026
  4. llalitkumarrr commented on Sep 28, 2026

    @llalitkumarrr
    Collaborator

    Hello @mkbctrl,

    Thank you for sharing such a detailed report. We tried to reproduce this issue using the latest version (2.10.0) and were able to reproduce it. Upon further investigation, we found that your analysis is correct, the issue originate from rearrange_events_for_latest_function_response because the outer loop does not break.

    Based on your inputs, we tested the patch below which resolved the issue in your reproduction scenario. Could you please take a look and share your feedback?

     for idx in range(len(events) - 2, -1, -1):
        event = events[idx]
        function_calls = event.get_function_calls()
        if function_calls:
          for function_call in function_calls:
            if function_call.id in function_responses_ids:
              function_call_event_idx = idx
              function_call_ids = {
                  function_call.id for function_call in function_calls
              }
              # last response event should only contain the responses for the
              # function calls in the same function call event
              if not function_responses_ids.issubset(function_call_ids):
                raise ValueError(
                    'Last response event should only contain the responses for the'
                    ' function calls in the same function call event. Function'
                    f' call ids found : {function_call_ids}, function response'
                    f' ids provided: {function_responses_ids}'
                )
              # collect all function responses from the function call event to
              # the last response event
              function_responses_ids = function_call_ids
              break
          # Stop at the nearest matching call event; do not keep walking back to
          # older events that may reuse the same function call id (issue #7269).
          if function_call_event_idx != -1:
            break
    
  5. GWeale commented on Oct 1, 2026

    @GWeale
    Collaborator

    fixed on main by 643df96, which pairs the latest function response with the nearest preceding call that has its id. it is not in v2.10.0 yet, so it ships in the next release. please open a new issue if this is still happening.

  6. mkbctrl commented on Oct 4, 2026

    @mkbctrl
    Author

    Confirming this is resolved in v2.11.0 (643df96).

    We had been running an in-process workaround on 2.8.0 that cut the history at the nearest call carrying the latest response's id before rearranging. On 2.11.0 we removed it and checked:

    • Both reproductions pass on the stock function. Shape 1: a read under call_1, then a confirmation-gated write reusing call_1. Shape 2: the same write batched with a second call. The user turn that proposed the write is kept, and the batched shape no longer raises ValueError.
    • Same output as the workaround on every history where the workaround returned a result; on the shapes 2.8.0 refused, 2.11.0 now drops the orphan or splits per owning call instead of raising.
    • End to end through Runner with a LiteLlm model: stored turns that reuse call ids, and approving a write under a reused id, both pair each call with its own response.

    Thanks for the fix. A note for anyone searching later: the 2.11.0 changelog entry credits #6751, so this issue is not listed there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

tools[Component] This issue is related to tools

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions