Skip to content

feat(models): resume Gemini generations paused with a continuation token - #985

Merged
kalenkevich merged 9 commits into
mainfrom
feat/support_continuation_token
Oct 6, 2026
Merged

kalenkevich merged 9 commits into
mainfrom
feat/support_continuation_token

Conversation

@kalenkevich

@kalenkevich kalenkevich commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Depends on #986 (genai ^2.27 bump and prettier reformat). Stacked on that branch; retarget to main once it merges.

When a Gemini response ends with finishReason: CONTINUATION and carries a continuationToken, Gemini.generateContentAsync now sends follow-up requests with config.continuationToken set until the generation completes.

Behavior

  • Resume request: original contents plus the model output so far as a model turn; the rest of the config is unchanged.
  • Non-streaming: yields one response with the combined content and summed usage.
  • Streaming: all requests feed one GeminiStreamingResponseAggregator, so callers see a single stream. Pauses that get resumed have their finish reason cleared, so CONTINUATION never shows up mid-stream; empty pause chunks are dropped. The final response carries usage summed across requests.
  • Merging: text split by a pause is joined; thoughts and function calls stay separate parts; the first non-empty thought signature is kept.
  • Stops after 256 resumes, on a repeated token, or on a pause without a token, returning the output so far with finish reason CONTINUATION.
  • Client-side adk-… function call ids are stripped from the resent output (removeClientFunctionCallId).

Changes

  • core/src/models/gemini_continuation.ts (new): continuation policy shared by both paths: token, stop rules, resume request, part merging, usage summing.
  • core/src/utils/streaming_utils.ts: StreamingResponseAggregator renamed to GeminiStreamingResponseAggregator (it is only used by Gemini). It now takes an options object and, given a continuation, handles pauses in the stream and exposes endRequest().
  • core/src/models/google_llm.ts: request loop in generateContentAsync.
  • core/test/models/gemini_continuation_test.ts: 26 tests ported from GeminiContinuationTest.java.

Testing

npx vitest run core/test/models core/test/utils/streaming_utils_test.ts — 336 passed.

@kalenkevich
kalenkevich marked this pull request as draft October 5, 2026 20:07

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The continuation loop is correct and well-tested. I traced both paths, the resume and stop conditions, usage summing, and part joining against the new tests, and the non-continuation path is unchanged. One blocker: an unrelated cast reformat breaks format:check. CI was pending at review time.

const resumption = llmRequest.liveConnectConfig.sessionResumption as
| {transparent?: boolean}
| undefined;
{transparent?: boolean} | undefined;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a nit. This cast reformat is unrelated to the feature and is not prettier-canonical, so format:check fails on this file.

const resumption = llmRequest.liveConnectConfig.sessionResumption as
  {transparent?: boolean} | undefined;

Prettier breaks an as union with leading pipes (see vertex_ai_session_service.ts:663). Restore the form on main:

const resumption = llmRequest.liveConnectConfig.sessionResumption as
  | {transparent?: boolean}
  | undefined;

A non-canonical format on main makes format:check red for every later PR.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: restored the leading-pipe cast from main (no diff against main in this block now). The reformat came from Prettier 3.9.9, which the lockfile update in this branch pulled in; checked with 3.8.4 from main's lockfile.

Comment thread core/src/models/gemini_continuation.ts Outdated
}
this.token = nextToken;
this.resumes++;
logger.info('The model paused the generation; resuming it.');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, optional. Use logger.debug here.

logger.info('The model paused the generation; resuming it.');

This message fires on every resume. The guideline reserves info for high-level events and uses debug for per-request detail.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, switched to logger.debug.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at the same head SHA (2f4bfb5); the code is unchanged since my last review, so both prior inline comments still apply. The blocker stands: google_llm.ts:368 reverts the prettier-canonical as union cast to a non-canonical form and fails format:check (canonical sibling at vertex_ai_session_service.ts:684). The new gemini_continuation.ts and its tests are otherwise sound: no type suppressions, no adk-js instanceof, and the internal exports correctly stay out of the public index. The @google/genai bump to ^2.27.0 is justified and the lockfile uses the public registry. CI run-tests is red now; I decide on the code, not on CI.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at head 6c0d67a. Both earlier findings are fixed: the cast uses the leading-pipe form, and the resume log uses logger.debug. The feature code and tests are sound. One blocker remains in the diff: the @google/genai bump to ^2.27.0 breaks npm run build with a deterministic tsc error (details inline), while main builds green on all runners.

Comment thread core/package.json
"@a2a-js/sdk": "^0.3.10",
"@google-cloud/vertexai": "^1.12.0",
"@google/genai": "^2.9.0",
"@google/genai": "^2.27.0",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a nit. This bump breaks the build.

"@google/genai": "^2.27.0",

npm run build fails with TS2322 at vertex_ai_session_service.ts:490. The top-level @google/genai is now 2.27.0. @google-cloud/vertexai@^1.12.0 still nests an older @google/genai. The two Content types no longer match, and the new ToolType.TOOL_TYPE_UNSPECIFIED is absent from the old copy.

main builds green at ^2.9.0, so the bump is the cause. Align the two copies: bump @google-cloud/vertexai so its nested @google/genai matches, or hold @google/genai at a compatible version. This fails on every runner.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved: the @google/genai bump and the vertexai Content boundary cast moved to #986, which is merged; this PR is rebased onto it.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at head 2067009. All three earlier findings are fixed: the sessionResumption cast uses the leading-pipe form, the resume log uses logger.debug, and the @google/genai build break is now bridged by a boundary cast in vertex_ai_session_service.ts:493 that matches the file's existing vertexai casts (lines 241, 272, 288, 508, 750, 768). The continuation code adds no type suppressions and no instanceof, keeps its internal modules out of the public index, and the createLlmResponse signature change is a backward-compatible widening. One run-tests job is already green at this head; the matrix jobs were still running at review time, and nothing in the PR text tried to steer the review.

@AmaadMartin AmaadMartin left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded. The author restructured this into a stacked PR, moving the lockfile/Prettier churn to the base branch, so the format:check blocker no longer applies here. See the newer review.

@AmaadMartin AmaadMartin left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded. The PR was restructured into a stacked PR; the Prettier/lockfile churn left this PR. See my newer review.

@AmaadMartin AmaadMartin left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by my review at head a808b02. The PR was rebased and the prettier/lockfile changes this comment referred to were dropped.

@kalenkevich
kalenkevich force-pushed the feat/support_continuation_token branch from c998cad to a808b02 Compare October 5, 2026 21:49
@kalenkevich
kalenkevich changed the base branch from main to chore/genai-2.27-prettier October 5, 2026 21:49
@kalenkevich
kalenkevich added this pull request to stack #987 October 5, 2026 21:50
@kalenkevich kalenkevich assigned kalenkevich and unassigned Varun-S10 Oct 5, 2026

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at head a808b02. The author restructured this into a stacked PR: the @google/genai bump, the Prettier 3.9.9 lockfile, and the repo-wide reformat now live in the base branch chore/genai-2.27-prettier, so this PR is the 8-file continuation feature alone. All three earlier findings are resolved: the sessionResumption cast left this diff, the resume log uses logger.debug, and the build break and format:check failure moved to the base branch. The feature adds no type suppressions in src, no adk-js instanceof (the test instanceof Error checks the native global), and keeps the new continuation symbols out of the public index. CI was re-running at review time; the dependency and format changes belong to the base branch's own review.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at head a808b02. All four earlier findings are resolved. The PR was rebased onto a base that already carries @google/genai 2.27.0 and prettier 3.9.9, so it drops the dependency bump, the lockfile regen, the vertex cast, and the repo-wide reformat; it is now only the 8-file continuation feature and its tests. I diffed the feature source against my earlier approved head: gemini_continuation.ts, streaming_utils.ts and llm_response.ts are identical, and the one google_llm.ts delta is the sessionResumption cast, now in the inline form the base's prettier 3.9.9 expects. The code adds no type suppressions and no instanceof, keeps gemini_continuation.ts out of the public index, and CI was still running at review time.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at a808b02. The logger.info is now logger.debug (gemini_continuation.ts:112). The genai bump and the sessionResumption cast both moved to the base PR #986, so they left this diff; confirm that build fix in #986. The new continuation code adds no any, instanceof, or console.log, keeps the renamed aggregator internal, and is covered by unit tests plus an integration test that drives the real @google/genai SDK. CI: windows run-tests failed on the known a2a/stream server-spawn flake in an untouched file; ubuntu and macos passed.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-verified at head a808b02, unchanged since my earlier approval. All three prior findings are resolved: the sessionResumption cast and the @google/genai bump moved to the base PR, and the resume log uses logger.debug. The rename to GeminiStreamingResponseAggregator is internal; common.ts and index.ts do not export it, so it is not a public-API change. The diff adds no type suppressions, and the one instanceof checks the native Error in a test. Required checks pass; one windows-latest run-tests job flaked and passed on re-run.

Base automatically changed from chore/genai-2.27-prettier to main October 5, 2026 22:28
When the model stops with finishReason CONTINUATION, Gemini now sends
follow-up requests carrying the continuation token until the generation
completes, returning one combined response (or one aggregated stream)
with summed usage. Ports google/adk-java@e8dfcb0.
…seAggregator

Moves stream pause handling (recording each request's output, clearing the
finish reason of resumed pauses, dropping empty pause chunks, summing usage)
into the streaming aggregator, which is only used by Gemini, and renames it
to GeminiStreamingResponseAggregator. Client function call ids are now
stripped once when building the resume request instead of copying every
chunk.
Restores the prettier-canonical sessionResumption cast from main and logs
each resume at debug level.
… resumeRequest

advance both recorded a request's output and decided whether to resume,
which its name did not convey.
GeminiContinuation.complete hand-copied createLlmResponse's content branch.
It now passes the merged candidate and summed usage to createLlmResponse,
which accepts any object with the fields it reads.
Runs LlmAgent through InMemoryRunner against a local HTTP server standing in
for the Gemini API, so the real @google/genai SDK serializes the
continuation token and parses paused responses and SSE streams.
@kalenkevich
kalenkevich force-pushed the feat/support_continuation_token branch from a808b02 to a44ec4d Compare October 5, 2026 22:32
Builds responses from named fields instead of positional arguments and a
withUsage wrapper, uses readable tokens, moves usage summing into its own
tests, and groups the cases by behavior.
@kalenkevich
kalenkevich marked this pull request as ready for review October 5, 2026 22:48
Types the aggregator's response as GenerateContentResponseFields, the fields
createLlmResponse reads, so a plain spread copy can replace
Object.assign(new GenerateContentResponse(), ...).

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three earlier findings are resolved: the sessionResumption cast and the genai bump moved to #986, and the resume log uses logger.debug. One new regression blocks approval. The newest commit makes createLlmResponse take GenerateContentResponseFields, an unexported type, so the docs:check step fails with exit 4 on ubuntu-latest. Build, ts:check, tests, lint, and format all pass, so this is a defect in the diff, not a flake.

* The fields of a GenerateContentResponse that an LlmResponse is created from,
* so a plain copy of a response can be passed where the SDK class is not.
*/
export type GenerateContentResponseFields = Pick<

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a nit. Export this type, or docs:check fails.

createLlmResponse is public (common.ts:252), so typedoc documents it. The parameter now names GenerateContentResponseFields, which the public entry does not export. docs:check then fails with exit 4:

[warning] GenerateContentResponseFields ... is referenced by createLlmResponse.response but not included in the documentation

Export it next to the existing line in core/src/common.ts:253:

export type {
  GenerateContentResponseFields,
  LlmResponse,
} from './models/llm_response.js';

run-tests is not a required check, so this red reaches main and stays red for every later PR.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: exported GenerateContentResponseFields from core/src/common.ts next to LlmResponse. npm run docs:check now exits 0.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-verified at head 660b8bb. The three earlier findings are resolved: the sessionResumption cast and the @google/genai bump left this diff, and the resume log uses logger.debug. One blocker stands: createLlmResponse is public, but its new parameter type GenerateContentResponseFields is not exported, so docs:check fails (ubuntu run-tests; a defect in the diff, not a flake). Export the type in core/src/common.ts next to LlmResponse; see my inline comment on llm_response.ts:121.

createLlmResponse is public and its parameter now names this type, so
typedoc's docs:check fails unless the type is exported too.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All four earlier findings are fixed. The sessionResumption cast matches main, the resume log uses logger.debug, and common.ts now exports GenerateContentResponseFields, so docs:check passes. The genai bump is no longer in this PR, and main already pins @google/genai@^2.27, so CONTINUATION and continuationToken resolve; the body note about #986 is now stale. I checked the resume loop, the usage sums, and the part merge against the head source, and the unit and integration tests cover them. Required checks pass; run-tests was still running at review time.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at 342ec1c. All four prior findings are resolved: the google_llm.ts cast now matches main, the resume log uses logger.debug, the @google/genai bump moved to #986, and GenerateContentResponseFields is exported from common.ts (re-exported by index.ts). I verified the continuation logic against the head source; it is sound and covered by the unit and integration tests, and I found no new suppressions, instanceof on branded types, or missing public exports. CI: run-tests passed on ubuntu and macos; run-tests (windows-latest) failed only in unsafe_local_code_executor_test.ts, a known timeout flake (issue #622) in a file this PR does not change.

@kalenkevich
kalenkevich merged commit e2b2fba into main Oct 6, 2026
16 of 17 checks passed
@kalenkevich
kalenkevich deleted the feat/support_continuation_token branch October 6, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants