Skip to content

Commit 3931b29

Browse files
jaycee-licopybara-github
authored andcommitted
feat: support continuation_token in GenerateContent
PiperOrigin-RevId: 991770518
1 parent c6212e9 commit 3931b29

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎google/genai/batches.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ def _Candidate_from_mldev(
528528
getv(from_object, ['urlContextMetadata']),
529529
)
530530

531+
if getv(from_object, ['continuationToken']) is not None:
532+
setv(
533+
to_object,
534+
['continuation_token'],
535+
getv(from_object, ['continuationToken']),
536+
)
537+
531538
return to_object
532539

533540

@@ -1146,6 +1153,13 @@ def _GenerateContentConfig_to_mldev(
11461153
getv(from_object, ['audio_transcription_config']),
11471154
)
11481155

1156+
if getv(from_object, ['continuation_token']) is not None:
1157+
setv(
1158+
parent_object,
1159+
['continuationToken'],
1160+
getv(from_object, ['continuation_token']),
1161+
)
1162+
11491163
return to_object
11501164

11511165

‎google/genai/models.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ def _Candidate_from_mldev(
151151
getv(from_object, ['urlContextMetadata']),
152152
)
153153

154+
if getv(from_object, ['continuationToken']) is not None:
155+
setv(
156+
to_object,
157+
['continuation_token'],
158+
getv(from_object, ['continuationToken']),
159+
)
160+
154161
return to_object
155162

156163

@@ -1367,6 +1374,13 @@ def _GenerateContentConfig_to_mldev(
13671374
getv(from_object, ['audio_transcription_config']),
13681375
)
13691376

1377+
if getv(from_object, ['continuation_token']) is not None:
1378+
setv(
1379+
parent_object,
1380+
['continuationToken'],
1381+
getv(from_object, ['continuation_token']),
1382+
)
1383+
13701384
return to_object
13711385

13721386

@@ -1564,6 +1578,13 @@ def _GenerateContentConfig_to_vertex(
15641578
getv(from_object, ['audio_transcription_config']),
15651579
)
15661580

1581+
if getv(from_object, ['continuation_token']) is not None:
1582+
setv(
1583+
parent_object,
1584+
['continuationToken'],
1585+
getv(from_object, ['continuation_token']),
1586+
)
1587+
15671588
return to_object
15681589

15691590

‎google/genai/types.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ class FinishReason(_common.CaseInSensitiveEnum):
549549
"""Image generation stopped because the generated image may be a recitation from a source."""
550550
IMAGE_OTHER = 'IMAGE_OTHER'
551551
"""Image generation stopped for a reason not otherwise specified."""
552+
CONTINUATION = 'CONTINUATION'
553+
"""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."""
552554

553555

554556
class HarmProbability(_common.CaseInSensitiveEnum):
@@ -6720,6 +6722,13 @@ class GenerateContentConfig(_common.BaseModel):
67206722
description="""Optional. Configuration for audio transcription (speech recognition).
67216723
""",
67226724
)
6725+
continuation_token: Optional[bytes] = Field(
6726+
default=None,
6727+
description="""An opaque continuation token used to resume generation from a
6728+
previous response that stopped with `finish_reason` set to
6729+
`CONTINUATION`.
6730+
""",
6731+
)
67236732

67246733
@pydantic.field_validator('response_schema', mode='before')
67256734
@classmethod
@@ -6941,6 +6950,12 @@ class GenerateContentConfigDict(TypedDict, total=False):
69416950
"""Optional. Configuration for audio transcription (speech recognition).
69426951
"""
69436952

6953+
continuation_token: Optional[bytes]
6954+
"""An opaque continuation token used to resume generation from a
6955+
previous response that stopped with `finish_reason` set to
6956+
`CONTINUATION`.
6957+
"""
6958+
69446959

69456960
GenerateContentConfigOrDict = Union[
69466961
GenerateContentConfig, GenerateContentConfigDict
@@ -8355,6 +8370,12 @@ class Candidate(_common.BaseModel):
83558370
default=None,
83568371
description="""Output only. Metadata returned when the model uses the `url_context` tool to get information from a user-provided URL.""",
83578372
)
8373+
continuation_token: Optional[bytes] = Field(
8374+
default=None,
8375+
description="""An opaque continuation token returned when `finish_reason` is
8376+
`CONTINUATION`. Pass it in a subsequent request to continue generation.
8377+
""",
8378+
)
83588379

83598380

83608381
class CandidateDict(TypedDict, total=False):
@@ -8401,6 +8422,11 @@ class CandidateDict(TypedDict, total=False):
84018422
url_context_metadata: Optional[UrlContextMetadataDict]
84028423
"""Output only. Metadata returned when the model uses the `url_context` tool to get information from a user-provided URL."""
84038424

8425+
continuation_token: Optional[bytes]
8426+
"""An opaque continuation token returned when `finish_reason` is
8427+
`CONTINUATION`. Pass it in a subsequent request to continue generation.
8428+
"""
8429+
84048430

84058431
CandidateOrDict = Union[Candidate, CandidateDict]
84068432

0 commit comments

Comments
 (0)