From 3931b290901eb616d9c4098ed6ba57c9d40a8d23 Mon Sep 17 00:00:00 2001 From: Jaycee Li Date: Thu, 1 Oct 2026 10:43:02 -0700 Subject: [PATCH] feat: support continuation_token in GenerateContent PiperOrigin-RevId: 991770518 --- google/genai/batches.py | 14 ++++++++++++++ google/genai/models.py | 21 +++++++++++++++++++++ google/genai/types.py | 26 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/google/genai/batches.py b/google/genai/batches.py index 1bbdc8f8c..16fb077af 100644 --- a/google/genai/batches.py +++ b/google/genai/batches.py @@ -528,6 +528,13 @@ def _Candidate_from_mldev( getv(from_object, ['urlContextMetadata']), ) + if getv(from_object, ['continuationToken']) is not None: + setv( + to_object, + ['continuation_token'], + getv(from_object, ['continuationToken']), + ) + return to_object @@ -1146,6 +1153,13 @@ def _GenerateContentConfig_to_mldev( getv(from_object, ['audio_transcription_config']), ) + if getv(from_object, ['continuation_token']) is not None: + setv( + parent_object, + ['continuationToken'], + getv(from_object, ['continuation_token']), + ) + return to_object diff --git a/google/genai/models.py b/google/genai/models.py index 8bd6e8655..a38f87d6b 100644 --- a/google/genai/models.py +++ b/google/genai/models.py @@ -151,6 +151,13 @@ def _Candidate_from_mldev( getv(from_object, ['urlContextMetadata']), ) + if getv(from_object, ['continuationToken']) is not None: + setv( + to_object, + ['continuation_token'], + getv(from_object, ['continuationToken']), + ) + return to_object @@ -1367,6 +1374,13 @@ def _GenerateContentConfig_to_mldev( getv(from_object, ['audio_transcription_config']), ) + if getv(from_object, ['continuation_token']) is not None: + setv( + parent_object, + ['continuationToken'], + getv(from_object, ['continuation_token']), + ) + return to_object @@ -1564,6 +1578,13 @@ def _GenerateContentConfig_to_vertex( getv(from_object, ['audio_transcription_config']), ) + if getv(from_object, ['continuation_token']) is not None: + setv( + parent_object, + ['continuationToken'], + getv(from_object, ['continuation_token']), + ) + return to_object diff --git a/google/genai/types.py b/google/genai/types.py index 91bf46176..d328318a8 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -549,6 +549,8 @@ class FinishReason(_common.CaseInSensitiveEnum): """Image generation stopped because the generated image may be a recitation from a source.""" IMAGE_OTHER = 'IMAGE_OTHER' """Image generation stopped for a reason not otherwise specified.""" + CONTINUATION = 'CONTINUATION' + """Token generation stopped because the response reached the per-request token limit, but generation is not yet complete. The response can be continued by passing the returned `continuation_token` in a subsequent request.""" class HarmProbability(_common.CaseInSensitiveEnum): @@ -6720,6 +6722,13 @@ class GenerateContentConfig(_common.BaseModel): description="""Optional. Configuration for audio transcription (speech recognition). """, ) + continuation_token: Optional[bytes] = Field( + default=None, + description="""An opaque continuation token used to resume generation from a + previous response that stopped with `finish_reason` set to + `CONTINUATION`. + """, + ) @pydantic.field_validator('response_schema', mode='before') @classmethod @@ -6941,6 +6950,12 @@ class GenerateContentConfigDict(TypedDict, total=False): """Optional. Configuration for audio transcription (speech recognition). """ + continuation_token: Optional[bytes] + """An opaque continuation token used to resume generation from a + previous response that stopped with `finish_reason` set to + `CONTINUATION`. + """ + GenerateContentConfigOrDict = Union[ GenerateContentConfig, GenerateContentConfigDict @@ -8355,6 +8370,12 @@ class Candidate(_common.BaseModel): default=None, description="""Output only. Metadata returned when the model uses the `url_context` tool to get information from a user-provided URL.""", ) + continuation_token: Optional[bytes] = Field( + default=None, + description="""An opaque continuation token returned when `finish_reason` is + `CONTINUATION`. Pass it in a subsequent request to continue generation. + """, + ) class CandidateDict(TypedDict, total=False): @@ -8401,6 +8422,11 @@ class CandidateDict(TypedDict, total=False): url_context_metadata: Optional[UrlContextMetadataDict] """Output only. Metadata returned when the model uses the `url_context` tool to get information from a user-provided URL.""" + continuation_token: Optional[bytes] + """An opaque continuation token returned when `finish_reason` is + `CONTINUATION`. Pass it in a subsequent request to continue generation. + """ + CandidateOrDict = Union[Candidate, CandidateDict]