GeezerVison is showing github.com — feat: Add google.genai.types.Content as an allowed message type to AD… · googleapis/python-aiplatform@6b131d1
File treeExpand file treeCollapse file tree Open diff view settings vertexai/preview/reasoning_engines/templates
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: tests/unit/vertex_adk/test_reasoning_engine_templates_adk.py +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | from unittest import mock | 18 | 18 |
| 19 | 19 | from google import auth | | 20 | +from google.genai import types | 20 | 21 | import vertexai | 21 | 22 | from google.cloud.aiplatform import initializer | 22 | 23 | from vertexai.preview import reasoning_engines | @@ -172,6 +173,28 @@ def test_stream_query(self): | 172 | 173 | ) | 173 | 174 | assert len(events) == 1 | 174 | 175 |
| | 176 | + def test_stream_query_with_content(self): | | 177 | + app = reasoning_engines.AdkApp( | | 178 | + agent=Agent(name="test_agent", model=_TEST_MODEL) | | 179 | + ) | | 180 | + assert app._tmpl_attrs.get("runner") is None | | 181 | + app.set_up() | | 182 | + app._tmpl_attrs["runner"] = _MockRunner() | | 183 | + events = list( | | 184 | + app.stream_query( | | 185 | + user_id="test_user_id", | | 186 | + message=types.Content( | | 187 | + role="user", | | 188 | + parts=[ | | 189 | + types.Part( | | 190 | + text="test message with content", | | 191 | + ) | | 192 | + ], | | 193 | + ).model_dump(), | | 194 | + ) | | 195 | + ) | | 196 | + assert len(events) == 1 | | 197 | + | 175 | 198 | def test_streaming_agent_run_with_events(self): | 176 | 199 | app = reasoning_engines.AdkApp( | 177 | 200 | agent=Agent(name="test_agent", model=_TEST_MODEL) | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/preview/reasoning_engines/templates/adk.py +19-3Lines changed: 19 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | # See the License for the specific language governing permissions and | 14 | 14 | # limitations under the License. | 15 | 15 | # | 16 | | -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional | | 16 | +from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union | | 17 | + | 17 | 18 |
| 18 | 19 | if TYPE_CHECKING: | | 20 | + try: | | 21 | + from google.genai import types | | 22 | + | | 23 | + ContentDict = types.Content | | 24 | + except (ImportError, AttributeError): | | 25 | + ContentDict = Dict | | 26 | + | 19 | 27 | try: | 20 | 28 | from google.adk.events.event import Event | 21 | 29 |
| @@ -442,7 +450,7 @@ def set_up(self): | 442 | 450 | def stream_query( | 443 | 451 | self, | 444 | 452 | *, | 445 | | - message: str, | | 453 | + message: Union[str, "ContentDict"], | 446 | 454 | user_id: str, | 447 | 455 | session_id: Optional[str] = None, | 448 | 456 | **kwargs, | @@ -466,7 +474,15 @@ def stream_query( | 466 | 474 | """ | 467 | 475 | from google.genai import types | 468 | 476 |
| 469 | | - content = types.Content(role="user", parts=[types.Part(text=message)]) | | 477 | + if isinstance(message, Dict): | | 478 | + content = types.Content.model_validate(message) | | 479 | + elif isinstance(message, str): | | 480 | + content = types.Content(role="user", parts=[types.Part(text=message)]) | | 481 | + else: | | 482 | + raise TypeError( | | 483 | + "message must be a string or a dictionary representing a Content object." | | 484 | + ) | | 485 | + | 470 | 486 | if not self._tmpl_attrs.get("runner"): | 471 | 487 | self.set_up() | 472 | 488 | if not session_id: | |
|