Skip to content

Commit 43c5379

Browse files
hkt74copybara-github
authored andcommitted
feat(chats): Allow user to create chat session with list of ContentDict
Fixes #467 PiperOrigin-RevId: 739210228
1 parent 2d12f54 commit 43c5379

File tree

2 files changed

+172
-79
lines changed

2 files changed

+172
-79
lines changed

Diff for: google/genai/chats.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import _transformers as t
2020
from . import types
2121
from .models import AsyncModels, Models
22-
from .types import Content, GenerateContentConfigOrDict, GenerateContentResponse, Part, PartUnionDict
22+
from .types import Content, ContentOrDict, GenerateContentConfigOrDict, GenerateContentResponse, Part, PartUnionDict
2323

2424

2525
if sys.version_info >= (3, 10):
@@ -116,14 +116,21 @@ def __init__(
116116
*,
117117
model: str,
118118
config: Optional[GenerateContentConfigOrDict] = None,
119-
history: list[Content],
119+
history: list[ContentOrDict],
120120
):
121121
self._model = model
122122
self._config = config
123-
self._comprehensive_history = history
123+
content_models = []
124+
for content in history:
125+
if not isinstance(content, Content):
126+
content_model = Content.model_validate(content)
127+
else:
128+
content_model = content
129+
content_models.append(content_model)
130+
self._comprehensive_history = content_models
124131
"""Comprehensive history is the full history of the chat, including turns of the invalid contents from the model and their associated inputs.
125132
"""
126-
self._curated_history = _extract_curated_history(history)
133+
self._curated_history = _extract_curated_history(content_models)
127134
"""Curated history is the set of valid turns that will be used in the subsequent send requests.
128135
"""
129136

@@ -210,7 +217,7 @@ def __init__(
210217
modules: Models,
211218
model: str,
212219
config: Optional[GenerateContentConfigOrDict] = None,
213-
history: list[Content],
220+
history: list[ContentOrDict],
214221
):
215222
self._modules = modules
216223
super().__init__(
@@ -344,7 +351,7 @@ def create(
344351
*,
345352
model: str,
346353
config: Optional[GenerateContentConfigOrDict] = None,
347-
history: Optional[list[Content]] = None,
354+
history: Optional[list[ContentOrDict]] = None,
348355
) -> Chat:
349356
"""Creates a new chat session.
350357
@@ -373,7 +380,7 @@ def __init__(
373380
modules: AsyncModels,
374381
model: str,
375382
config: Optional[GenerateContentConfigOrDict] = None,
376-
history: list[Content],
383+
history: list[ContentOrDict],
377384
):
378385
self._modules = modules
379386
super().__init__(
@@ -501,7 +508,7 @@ def create(
501508
*,
502509
model: str,
503510
config: Optional[GenerateContentConfigOrDict] = None,
504-
history: Optional[list[Content]] = None,
511+
history: Optional[list[ContentOrDict]] = None,
505512
) -> AsyncChat:
506513
"""Creates a new chat session.
507514

0 commit comments

Comments
 (0)