Repository navigation
feat(models): resume Gemini generations paused with a continuation token - #985
Conversation
AmaadMartin
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| } | ||
| this.token = nextToken; | ||
| this.resumes++; | ||
| logger.info('The model paused the generation; resuming it.'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done, switched to logger.debug.
AmaadMartin
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
| "@a2a-js/sdk": "^0.3.10", | ||
| "@google-cloud/vertexai": "^1.12.0", | ||
| "@google/genai": "^2.9.0", | ||
| "@google/genai": "^2.27.0", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Resolved: the @google/genai bump and the vertexai Content boundary cast moved to #986, which is merged; this PR is rebased onto it.
AmaadMartin
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Superseded by my review at head a808b02. The PR was rebased and the prettier/lockfile changes this comment referred to were dropped.
c998cad to
a808b02
Compare
AmaadMartin
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
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.
a808b02 to
a44ec4d
Compare
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.
Types the aggregator's response as GenerateContentResponseFields, the fields createLlmResponse reads, so a plain spread copy can replace Object.assign(new GenerateContentResponse(), ...).
AmaadMartin
left a comment
There was a problem hiding this comment.
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< |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Fixed: exported GenerateContentResponseFields from core/src/common.ts next to LlmResponse. npm run docs:check now exits 0.
AmaadMartin
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Depends on #986 (genai ^2.27 bump and prettier reformat). Stacked on that branch; retarget to
mainonce it merges.When a Gemini response ends with
finishReason: CONTINUATIONand carries acontinuationToken,Gemini.generateContentAsyncnow sends follow-up requests withconfig.continuationTokenset until the generation completes.Behavior
modelturn; the rest of the config is unchanged.GeminiStreamingResponseAggregator, so callers see a single stream. Pauses that get resumed have their finish reason cleared, soCONTINUATIONnever shows up mid-stream; empty pause chunks are dropped. The final response carries usage summed across requests.CONTINUATION.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:StreamingResponseAggregatorrenamed toGeminiStreamingResponseAggregator(it is only used byGemini). It now takes an options object and, given a continuation, handles pauses in the stream and exposesendRequest().core/src/models/google_llm.ts: request loop ingenerateContentAsync.core/test/models/gemini_continuation_test.ts: 26 tests ported fromGeminiContinuationTest.java.Testing
npx vitest run core/test/models core/test/utils/streaming_utils_test.ts— 336 passed.