-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(runners): Add get_session_config property to RunConfig #3662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nimanthadilz
wants to merge
2
commits into
google:main
Choose a base branch
from
nimanthadilz:add-get-session-config-to-run-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,8 @@ | |
| from google.adk.events.event import Event | ||
| from google.adk.plugins.base_plugin import BasePlugin | ||
| from google.adk.runners import Runner | ||
| from google.adk.sessions.base_session_service import BaseSessionService | ||
| from google.adk.sessions.base_session_service import GetSessionConfig | ||
| from google.adk.sessions.in_memory_session_service import InMemorySessionService | ||
| from google.adk.sessions.session import Session | ||
| from google.adk.tools.function_tool import FunctionTool | ||
|
|
@@ -1321,5 +1323,134 @@ def test_infer_agent_origin_detects_mismatch_for_user_agent( | |
| assert "actual_name" in runner._app_name_alignment_hint | ||
|
|
||
|
|
||
| class TestRunnerGetSessionConfig: | ||
| """Tests for Runner get_session_config passing to session service.""" | ||
|
|
||
| def setup_method(self): | ||
| """Set up test fixtures.""" | ||
| self.mock_session_service = AsyncMock(spec=BaseSessionService) | ||
| self.artifact_service = InMemoryArtifactService() | ||
| self.root_agent = MockLlmAgent("root_agent") | ||
|
|
||
| # Create a mock session to return | ||
| self.mock_session = Session( | ||
| id=TEST_SESSION_ID, | ||
| app_name=TEST_APP_ID, | ||
| user_id=TEST_USER_ID, | ||
| events=[], | ||
| ) | ||
|
|
||
| # Configure the mock to return the session | ||
| self.mock_session_service.get_session = AsyncMock( | ||
| return_value=self.mock_session | ||
| ) | ||
|
|
||
| self.runner = Runner( | ||
| app_name=TEST_APP_ID, | ||
| agent=self.root_agent, | ||
| session_service=self.mock_session_service, | ||
| artifact_service=self.artifact_service, | ||
| ) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_async_passes_get_session_config(self): | ||
| """Test that run_async passes get_session_config to session service.""" | ||
| config = GetSessionConfig(num_recent_events=5) | ||
| run_config = RunConfig(get_session_config=config) | ||
|
|
||
| agen = self.runner.run_async( | ||
| user_id=TEST_USER_ID, | ||
| session_id=TEST_SESSION_ID, | ||
| new_message=types.Content(role="user", parts=[types.Part(text="test")]), | ||
| run_config=run_config, | ||
| ) | ||
|
|
||
| # Consume first event to trigger get_session call | ||
| try: | ||
| await agen.__anext__() | ||
| except StopAsyncIteration: | ||
| pass | ||
| finally: | ||
| await agen.aclose() | ||
|
|
||
| # Verify get_session was called with the config | ||
| self.mock_session_service.get_session.assert_called_once() | ||
| call_kwargs = self.mock_session_service.get_session.call_args.kwargs | ||
| assert call_kwargs["config"] == config | ||
| assert call_kwargs["app_name"] == TEST_APP_ID | ||
| assert call_kwargs["user_id"] == TEST_USER_ID | ||
| assert call_kwargs["session_id"] == TEST_SESSION_ID | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_async_passes_none_when_no_config(self): | ||
| """Test that run_async passes None when get_session_config is not set.""" | ||
| agen = self.runner.run_async( | ||
| user_id=TEST_USER_ID, | ||
| session_id=TEST_SESSION_ID, | ||
| new_message=types.Content(role="user", parts=[types.Part(text="test")]), | ||
| ) | ||
|
|
||
| # Consume first event to trigger get_session call | ||
| try: | ||
| await agen.__anext__() | ||
| except StopAsyncIteration: | ||
| pass | ||
| finally: | ||
| await agen.aclose() | ||
|
|
||
| # Verify get_session was called with config=None | ||
| self.mock_session_service.get_session.assert_called_once() | ||
| call_kwargs = self.mock_session_service.get_session.call_args.kwargs | ||
| assert call_kwargs["config"] is None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_debug_passes_get_session_config(self): | ||
| """Test that run_debug passes get_session_config to session service.""" | ||
| # Mock create_session as well since run_debug creates session if not found | ||
| self.mock_session_service.create_session = AsyncMock( | ||
| return_value=self.mock_session | ||
| ) | ||
|
|
||
| config = GetSessionConfig(num_recent_events=10) | ||
| run_config = RunConfig(get_session_config=config) | ||
|
|
||
| await self.runner.run_debug( | ||
| user_id=TEST_USER_ID, | ||
| session_id=TEST_SESSION_ID, | ||
| user_messages="test", | ||
| run_config=run_config, | ||
| quiet=True, | ||
| ) | ||
|
|
||
| # Verify get_session was called with the config | ||
| # Note: get_session is called twice - once in run_debug, once in run_async | ||
| assert self.mock_session_service.get_session.call_count == 2 | ||
| # Check both calls had the config | ||
| for call in self.mock_session_service.get_session.call_args_list: | ||
| assert call.kwargs["config"] == config | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_run_debug_passes_none_when_no_config(self): | ||
| """Test that run_debug passes None when run_config is not provided.""" | ||
| # Mock create_session | ||
| self.mock_session_service.create_session = AsyncMock( | ||
| return_value=self.mock_session | ||
| ) | ||
|
|
||
| await self.runner.run_debug( | ||
| user_id=TEST_USER_ID, | ||
| session_id=TEST_SESSION_ID, | ||
| user_messages="test", | ||
| quiet=True, | ||
| ) | ||
|
|
||
| # Verify get_session was called with config=None | ||
| # Note: get_session is called twice - once in run_debug, once in run_async | ||
| assert self.mock_session_service.get_session.call_count == 2 | ||
| # Check both calls had config=None | ||
| for call in self.mock_session_service.get_session.call_args_list: | ||
| assert call.kwargs["config"] is None | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend adding unit tests for |
||
|
|
||
| if __name__ == "__main__": | ||
| pytest.main([__file__]) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain the same logic, I recommend adding this code at the beginning of the
run_debugmethod and removing the ternary check from theconfigparameter assignmentrun_config = run_config or RunConfig()