Repository navigation
feat(interactions): add automatic_continuation to interactions.create in the Python SDK - #135
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
copybara-service
Bot
force-pushed
the
copybara/995364951
branch
from
October 28, 2026 05:11
8727e43 to
2a0bedf
Compare
… in the Python SDK Foreground `interactions.create()` calls are capped per request by the server (32k output tokens unary, 64k streaming) and stop with `status='requires_continuation'` plus a `continuation_token`. `GeminiNextGenInteractions.create` and `AsyncGeminiNextGenInteractions.create` now accept `automatic_continuation` (default `True`) and chain those continuation hops client-side so a plain `create()` returns the full decode. Request chaining: - Each hop after the first sends the `continuation_token` from the previous hop. With `store=True` (default) it sets `previous_interaction_id` to the hop that issued the token and omits `input`; with `store=False` it resends the original `input`. - Unary foreground model calls default `generation_config.max_output_tokens` to 65536 when unset (streaming is left to the server's 64k default). Explicit values are never overridden. - Stops on `completed`, `incomplete` (token preserved for manual resumption), `requires_action`, `failed`, `cancelled`, a missing token, or a repeated token. Skipped for `agent` / `agent_config` requests (server-managed loops) and `background=True`; `background=True` with `automatic_continuation=False` raises `ValueError`. Unary (`stream=False`) merging: - Hops are merged into one `Interaction`: `steps` concatenated in hop order (leading `user_input` steps dropped on later hops), `usage` counts and per-modality breakdowns summed across hops with `usage.initial_input_tokens` taken from hop 1, `id` / `status` / `continuation_token` / `updated` from the final hop, and `previous_interaction_id` / `created` / `input` from hop 1. - `continuation_ids` lists every hop id in order (the last equals `id`), de-duplicated with any server-provided ids. `output_text` skips intervening thought steps across hop boundaries. Streaming (`stream=True`): - Events from all hops are yielded verbatim on the returned `Stream` / `AsyncStream`. Consumers should treat `interaction.completed` with `status='requires_continuation'` as intermediate. - Mid-hop transport drops or premature EOF are resumed from the latest `interaction.status_update` checkpoint token when no content has been yielded since that checkpoint, with at most 3 no-progress reconnects. SSE `error` events are terminal. HTTP status retries stay with `http_options.retry_options`. Timeouts and errors: - A numeric per-call `timeout` bounds the whole `create()` call across hops: each hop is sent the remaining budget and no further hop starts once it expires. `http_options.timeout`, `httpx.Timeout` and `http_options.retry_options` continue to apply per hop. - A failure (or timeout) on a hop after earlier hops already produced output raises `errors.ContinuationInterruptedError` (underlying error as `__cause__`) carrying `partial_interaction` (unary: the hops merged so far, status `requires_continuation`), `continuation_token`, `interaction_id` (to send as `previous_interaction_id`) and `continuation_ids`, so billed work is not lost and the call can be resumed manually. Failures on the first hop propagate unchanged. Opt-out: - `automatic_continuation=False` returns the single server response unchanged (no budget injection, no merging). A one-time `UserWarning` is emitted when such a response stops at `requires_continuation` and the caller did not supply a `continuation_token` themselves. Tests: tests/interactions/test_automatic_continuation.py (unit and edge cases, sync and async) and tests/interactions/test_continuation_conformance.py (cross-language conformance suite). PiperOrigin-RevId: 995364951
copybara-service
Bot
force-pushed
the
copybara/995364951
branch
from
October 28, 2026 05:18
2a0bedf to
5a96b23
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(interactions): add automatic_continuation to interactions.create in the Python SDK
Foreground
interactions.create()calls are capped per request by the server (32k output tokens unary, 64k streaming) and stop withstatus='requires_continuation'plus acontinuation_token.GeminiNextGenInteractions.createandAsyncGeminiNextGenInteractions.createnow acceptautomatic_continuation(defaultTrue) and chain those continuation hops client-side so a plaincreate()returns the full decode.Request chaining:
continuation_tokenfrom the previous hop. Withstore=True(default) it setsprevious_interaction_idto the hop that issued the token and omitsinput; withstore=Falseit resends the originalinput.generation_config.max_output_tokensto 65536 when unset (streaming is left to the server's 64k default). Explicit values are never overridden.completed,incomplete(token preserved for manual resumption),requires_action,failed,cancelled, a missing token, or a repeated token. Skipped foragent/agent_configrequests (server-managed loops) andbackground=True;background=Truewithautomatic_continuation=FalseraisesValueError.Unary (
stream=False) merging:Interaction:stepsconcatenated in hop order (leadinguser_inputsteps dropped on later hops),usagecounts and per-modality breakdowns summed across hops withusage.initial_input_tokenstaken from hop 1,id/status/continuation_token/updatedfrom the final hop, andprevious_interaction_id/created/inputfrom hop 1.continuation_idslists every hop id in order (the last equalsid), de-duplicated with any server-provided ids.output_textskips intervening thought steps across hop boundaries.Streaming (
stream=True):Stream/AsyncStream. Consumers should treatinteraction.completedwithstatus='requires_continuation'as intermediate.interaction.status_updatecheckpoint token when no content has been yielded since that checkpoint, with at most 3 no-progress reconnects. SSEerrorevents are terminal. HTTP status retries stay withhttp_options.retry_options.Timeouts and errors:
timeoutbounds the wholecreate()call across hops: each hop is sent the remaining budget and no further hop starts once it expires.http_options.timeout,httpx.Timeoutandhttp_options.retry_optionscontinue to apply per hop.errors.ContinuationInterruptedError(underlying error as__cause__) carryingpartial_interaction(unary: the hops merged so far, statusrequires_continuation),continuation_token,interaction_id(to send asprevious_interaction_id) andcontinuation_ids, so billed work is not lost and the call can be resumed manually. Failures on the first hop propagate unchanged.Opt-out:
automatic_continuation=Falsereturns the single server response unchanged (no budget injection, no merging). A one-timeUserWarningis emitted when such a response stops atrequires_continuationand the caller did not supply acontinuation_tokenthemselves.Tests: tests/interactions/test_automatic_continuation.py (unit and edge cases, sync and async) and tests/interactions/test_continuation_conformance.py (cross-language conformance suite).