Stale function responses can hard-fail content processing after retries or session restarts
Describe the bug
rearrangeEventsForLatestFunctionResponse in internal/llminternal/contents_processor.go returns an error when the latest event is a function response and no matching function-call event can be found in session history:
if functionCallEventIdx == -1 {
return nil, fmt.Errorf(
"no function call event found for function responses ids: %v",
responseIDs,
)
}
That is appropriate for truly malformed history, but it can also happen when a stale tool result is replayed after a retry, reconnect, or session restart. In that case, the response is already dangling; failing the whole request prevents the next turn from continuing even though the stale response could be safely ignored.
To Reproduce
- Start or resume a session with normal user/model history.
- Submit a function-response event whose
IDno longer matches any function-call event in the session history. - Let
ContentsRequestProcessorbuild request contents. rearrangeEventsForLatestFunctionResponsesees the latest event is a function response.- No matching function-call event is found, so content processing returns:
no function call event found for function responses ids: ...
Expected behavior
If the latest function-response event is stale/orphaned and has no matching function-call event in history, ADK Go should treat it as a no-op for content construction rather than failing the whole turn.
The previous valid history should still be included in the request contents.
Observed behavior
The whole content processing path fails with no function call event found..., even though dropping the dangling latest response would allow the turn to continue.
Proposed fix
When no matching function-call event is found for the latest function-response event:
- log a warning with the orphaned response IDs
- drop only the trailing stale function-response event
- continue building contents from the remaining valid history
This should be limited to the "latest response has no matching call" case. Other validation errors where a response set partially matches the wrong call event should remain errors.
Testing
Add content processor tests for:
- a trailing orphaned function response after normal user history
- a trailing orphaned function response after a legitimate model text reply
Both tests should assert that the stale response is dropped and earlier valid contents remain.
Alignment with adk-python
This is specific to ADK Go's event history rearrangement for function responses. The cross-language invariant is that stale client retries should not make otherwise valid session history unusable.
+1 Stale network retries definitely shouldn't crash the entire session history. Dropping the orphaned response is the right way to handle this. Thanks for reporting it and providing the PR!
Two things worth recording here after going through the current behavior on main.
The report still reproduces on v2. contents_processor.go:299-304 still returns no function call event found for function responses ids: …, and that still fails the whole turn.
The proposed fix above drops more than the stale response. Discarding the trailing event rather than the orphaned parts is lossy, because one event can carry a text part alongside a function response, and runner.go:657-659 stores a caller's Content verbatim as a single event. A client that sends its next question in the same message as a replayed tool result loses the question, and since the last surviving content is then the model's reply, the framework appends its "Continue processing previous requests as instructed" turn. The request succeeds and the model answers the previous exchange instead.
Restricting the fix to the trailing event also misses how most orphans arise. An orphan appears whenever the matching call event is filtered out of this request rather than genuinely absent from the session — the isolation-scope filter at contents_processor.go:101-102 and history truncation both do that. In those cases the response being discarded is a real tool result, not a stale one.
Worth noting the cross-language framing in the report is the other way round: adk-python has the same code path and hit the same problem, and fixed it with _drop_orphaned_function_responses, run as a pre-pass before the rearrangement rather than inside it. It strips orphaned response parts and keeps the rest of the event, applies to the whole history rather than only the tail, logs a warning naming the dropped IDs, and deliberately leaves responses that have no ID alone, since IDs are stripped on the way out for some model families.
Tracking the fix in #766. #880 proposes the same change and is a duplicate of it.