Skip to content

Commit 4bd9454

Browse files
google-genai-botcopybara-github
authored andcommitted
chore: Add state_delta param to Runner.run
PiperOrigin-RevId: 929454639
1 parent 92d608f commit 4bd9454

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/google/adk/runners.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ def run(
887887
user_id: str,
888888
session_id: str,
889889
new_message: types.Content,
890+
state_delta: Optional[dict[str, Any]] = None,
890891
run_config: Optional[RunConfig] = None,
891892
) -> Generator[Event, None, None]:
892893
"""Runs the agent.
@@ -904,6 +905,7 @@ def run(
904905
user_id: The user ID of the session.
905906
session_id: The session ID of the session.
906907
new_message: A new message to append to the session.
908+
state_delta: Optional state changes to apply to the session.
907909
run_config: The run config for the agent.
908910
909911
Yields:
@@ -919,6 +921,7 @@ async def _invoke_run_async():
919921
user_id=user_id,
920922
session_id=session_id,
921923
new_message=new_message,
924+
state_delta=state_delta,
922925
run_config=run_config,
923926
)
924927
) as agen:

tests/unittests/test_runners.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,45 @@ async def test_run_live_auto_create_session():
724724
assert session is not None
725725

726726

727+
def test_run_passes_state_delta():
728+
"""run should forward state_delta down to run_async."""
729+
import asyncio
730+
731+
session_service = InMemorySessionService()
732+
runner = Runner(
733+
app_name=TEST_APP_ID,
734+
agent=MockAgent("test_agent"),
735+
session_service=session_service,
736+
artifact_service=InMemoryArtifactService(),
737+
auto_create_session=True,
738+
)
739+
740+
state_delta = {"test_key": "test_value"}
741+
742+
events = list(
743+
runner.run(
744+
user_id=TEST_USER_ID,
745+
session_id=TEST_SESSION_ID,
746+
new_message=types.Content(
747+
role="user", parts=[types.Part(text="hello")]
748+
),
749+
state_delta=state_delta,
750+
)
751+
)
752+
753+
assert len(events) >= 1
754+
755+
session = asyncio.run(
756+
session_service.get_session(
757+
app_name=TEST_APP_ID, user_id=TEST_USER_ID, session_id=TEST_SESSION_ID
758+
)
759+
)
760+
session_events = session.events
761+
762+
user_event = next(e for e in session_events if e.author == "user")
763+
assert user_event.actions.state_delta == state_delta
764+
765+
727766
@pytest.mark.asyncio
728767
async def test_run_live_persists_event_callback_modifications():
729768
"""run_live should persist the same event it streams after callback changes."""

0 commit comments

Comments
 (0)