Skip to content

Add Kotlin snippet for VertexAiSessionService - #2102

Merged
happyhuman merged 6 commits into
mainfrom
docs-kotlin-vertex-session
Aug 17, 2026
Merged

Add Kotlin snippet for VertexAiSessionService#2102
happyhuman merged 6 commits into
mainfrom
docs-kotlin-vertex-session

Conversation

@happyhuman

@happyhuman happyhuman commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

The VertexAiSessionService section of docs/sessions/session/index.md showed
Python, Go and Java. Kotlin gained the service in adk-kotlin 0.7.0, so this adds
a Kotlin tab and adds Kotlin to that section's language-support badge.

Why this snippet carries so much comment

Kotlin addresses the reasoning engine differently from every sibling tab on
the page, in two independent ways. Both are enforced at runtime, so getting it
wrong fails at the first call rather than at compile time.

how the engine is supplied format
Python app_name on each call full projects/…/reasoningEngines/…
Java app_name on each call bare numeric "123456789"
Go VertexAIService(ctx, modelName) n/a — different shape entirely
Kotlin reasoningEngineId at construction bare numeric only

From the 0.7.0 KDoc:

Unlike the Python and Java ADK, the session key's SessionKey.appName is
never parsed to derive the engine — it is only a label — so the engine must
be supplied explicitly here.

and the constructor:

require(reasoningEngineId.all { it.isDigit() }) {
  "reasoningEngineId must be the numeric reasoning engine id ..., not a resource name"
}

A reader copying the Python idiom directly above would fail twice: passing a
resource name where a numeric id is required, and expecting appName to select
the engine when it is inert. The snippet calls both out inline rather than
burying them here.

Notes for reviewers

  • JVM-only. VertexAiSessionService is in core/src/jvmMain, so it does
    not exist on Android. Kotlin is the only language on this page where that
    distinction applies, so the tab says so above the code.
  • Scoped like the Java tab, the closest analogue: construct the service,
    then create one session. No runner or agent — no sibling tab on this page
    builds one, and this section's prose is a characteristics list rather than a
    wiring guide. An earlier revision added an LlmAgent + Gemini +
    InMemoryRunner and ran to 30 code lines against Python's 5 and Java's 12;
    that was misapplying a note that was correct on sessions/memory, where the
    prose does promise a Runner and the Python tab shows one.
  • runBlocking is the counterpart of the Java tab's .blockingGet()
    createSession is a suspend function, so some bridge is unavoidable.
  • The primary constructor is internal. The snippet uses the public
    secondary one, (project, location, reasoningEngineId, credentials, httpClient). kotlin_api.py sig shows only the primary, so checking the
    signature alone suggests a constructor callers cannot invoke.
  • id = null in SessionKey is deliberate. toAdk(appName, userId, fallbackId) prefers the server-assigned id from the response name and
    falls back to key.id only when absent, so null is the correct way to let
    the service assign one. Upstream does the same.
  • No initial state. createSession(key, state = null) defaults it and the
    Java tab notes none is needed, so passing one would add a concept the
    siblings avoid.
  • Section badge, not page badge. The page-level badge already reads
    Kotlin v0.1.0 and stays there — it marks when Kotlin support for the page
    was introduced. This ### section has its own badge, which had no Kotlin at
    all; it gets v0.7.0, when the service landed.
  • Written inline to match the two existing Kotlin tabs on this page.

Verification

  • verify_snippets.py: L0/L2/L3/L5/L6 pass.
  • Inline snippets never reach Gradle, so this one was additionally compiled
    against the 0.7.0 pin in a scratch file, which is not part of the commit.
    That caught a missing required model argument on LlmAgent that L0 alone
    did not.
  • API surface read from the adk-kotlin v0.7.0 sources.

Pre-existing issue found while reviewing, not fixed here

The page's first Kotlin tab (the InMemorySessionService example, around
L142) does not compile: it calls the suspend createSession outside a
coroutine and has top-level statements. Out of scope for this PR, but it should
be fixed — happy to file or follow up.

The VertexAiSessionService section showed Python, Go and Java. Kotlin gained
the service in adk-kotlin 0.7.0, so add a Kotlin tab and Kotlin to that
section's language-support badge.

Kotlin addresses the reasoning engine differently from every sibling tab on
the page, so the snippet says so at the point of use:

- The engine is fixed at construction via `reasoningEngineId`. The 0.7.0 KDoc
  is explicit that, unlike the Python and Java ADK, `SessionKey.appName` is
  never parsed to derive the engine -- it is only a label. The Python tab
  above passes the engine through `app_name` on each call.
- `reasoningEngineId` must be the bare numeric id. The constructor rejects a
  full resource name outright (`require(reasoningEngineId.all { it.isDigit() })`),
  while the Python tab passes
  `projects/.../locations/.../reasoningEngines/...`.

A reader copying the adjacent Python idiom would therefore fail twice over.

Written inline to match the two existing Kotlin tabs on this page. Inline
snippets never reach Gradle, so this one was additionally compiled against the
0.7.0 pin in a scratch file that is not part of the commit.
@netlify

netlify Bot commented Aug 10, 2026

Copy link
Copy Markdown

Deploy Preview for adk-docs-preview ready!

Name Link
🔨 Latest commit d879111
🔍 Latest deploy log https://app.netlify.com/projects/adk-docs-preview/deploys/6a83838ad777290008f401a9
😎 Deploy Preview https://deploy-preview-2102--adk-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Five fixes from self-review against the 0.7.0 sources and upstream's own
VertexAiSessionServiceExample.kt, which I should have consulted before writing
the first version:

- State that the service is JVM-only. It lives in core/src/jvmMain, so it does
  not exist on Android. Kotlin is the only language on this page where that
  distinction applies, so if the Kotlin tab omits it, nothing carries it.
- Show the service actually being used. The snippet stopped at an uncalled
  `suspend fun`; it now hands the service to an InMemoryRunner, which is what
  the section is about and what the upstream example does.
- Use `runBlocking` in a `main`, matching upstream, instead of a suspend
  function nothing calls.
- Drop `state = mapOf(...)`. It defaults to null, upstream omits it, and the
  Java tab explicitly notes no initial state is needed, so it introduced a
  concept the sibling tabs deliberately avoid.
- Widen the comparison from "the Python and Java tabs" to all the other tabs.
  The KDoc phrasing names Python and Java, but this page also has a Go tab.

The added LlmAgent needs an explicit `model`; the first draft would not have
compiled without it, which the scratch compile caught.
The previous revision added an LlmAgent, a Gemini model and an InMemoryRunner,
taking the tab to 30 code lines against Python's 5, Go's 8 and Java's 12. No
sibling tab on this page constructs a runner or an agent, and this section's
prose never mentions one -- it is a characteristics list, not a wiring guide.

That change came from misapplying a review finding. On sessions/memory the
equivalent note was right: the prose there says "instantiating the
VertexAiMemoryBankService and passing it to the Runner" and the Python tab
shows exactly that. Neither holds here, so the runner was answering a question
this page does not ask.

Now scoped like the Java tab, the closest analogue: construct the service, then
create one session. That is still enough to demonstrate both divergences -- the
engine pinned at construction as a bare numeric id, and appName being only a
label -- since showing the second requires a SessionKey.

`runBlocking` stays, because createSession is a suspend function; it is the
direct counterpart of the Java tab's `.blockingGet()`, and the comment now says
so. Recompiled against the 0.7.0 pin in a scratch file.
@happyhuman happyhuman self-assigned this Aug 10, 2026

@joefernandez joefernandez 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.

thanks for the update!

@happyhuman
happyhuman merged commit f9ae6d9 into main Aug 17, 2026
7 of 8 checks passed
@happyhuman
happyhuman deleted the docs-kotlin-vertex-session branch August 17, 2026 21:57
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