Skip to content

Commit 623f578

Browse files
committed
docs: make StoryFlowAgent example fully async/await (#379)
1 parent 3dd3436 commit 623f578

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

examples/python/snippets/agents/custom-agent/storyflow_agent.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import logging
216
from typing import AsyncGenerator
317
from typing_extensions import override
@@ -219,7 +233,7 @@ async def _run_async_impl(
219233
# --- Setup Runner and Session ---
220234
session_service = InMemorySessionService()
221235
initial_state = {"topic": "a brave kitten exploring a haunted house"}
222-
session = session_service.create_session(
236+
session = await session_service.create_session(
223237
app_name=APP_NAME,
224238
user_id=USER_ID,
225239
session_id=SESSION_ID,
@@ -234,12 +248,12 @@ async def _run_async_impl(
234248
)
235249

236250
# --- Function to Interact with the Agent ---
237-
def call_agent(user_input_topic: str):
251+
async def call_agent(user_input_topic: str):
238252
"""
239253
Sends a new topic to the agent (overwriting the initial one if needed)
240254
and runs the workflow.
241255
"""
242-
current_session = session_service.get_session(app_name=APP_NAME,
256+
current_session = await session_service.get_session(app_name=APP_NAME,
243257
user_id=USER_ID,
244258
session_id=SESSION_ID)
245259
if not current_session:
@@ -261,7 +275,7 @@ def call_agent(user_input_topic: str):
261275
print("\n--- Agent Interaction Result ---")
262276
print("Agent Final Response: ", final_response)
263277

264-
final_session = session_service.get_session(app_name=APP_NAME,
278+
final_session = await session_service.get_session(app_name=APP_NAME,
265279
user_id=USER_ID,
266280
session_id=SESSION_ID)
267281
print("Final Session State:")
@@ -270,5 +284,5 @@ def call_agent(user_input_topic: str):
270284
print("-------------------------------\n")
271285

272286
# --- Run the Agent ---
273-
call_agent("a lonely robot finding a friend in a junkyard")
287+
await call_agent("a lonely robot finding a friend in a junkyard")
274288
# --8<-- [end:story_flow_agent]

0 commit comments

Comments
 (0)