mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: aclose all async generators to fix OTel tracing context
See https://github.com/google/adk-python/issues/1670#issuecomment-3115891100 PiperOrigin-RevId: 794659547
This commit is contained in:
committed by
Copybara-Service
parent
c5af44cfc0
commit
a30c63c593
@@ -46,13 +46,19 @@ async def main():
|
||||
role='user', parts=[types.Part.from_text(text=new_message)]
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
# TODO - migrate try...finally to contextlib.aclosing after Python 3.9 is
|
||||
# no longer supported.
|
||||
agen = runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
)
|
||||
try:
|
||||
async for event in agen:
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
finally:
|
||||
await agen.aclose()
|
||||
|
||||
async def run_prompt_bytes(session: Session, new_message: str):
|
||||
content = types.Content(
|
||||
@@ -64,14 +70,20 @@ async def main():
|
||||
],
|
||||
)
|
||||
print('** User says:', content.model_dump(exclude_none=True))
|
||||
async for event in runner.run_async(
|
||||
# TODO - migrate try...finally to contextlib.aclosing after Python 3.9 is
|
||||
# no longer supported.
|
||||
agen = runner.run_async(
|
||||
user_id=user_id_1,
|
||||
session_id=session.id,
|
||||
new_message=content,
|
||||
run_config=RunConfig(save_input_blobs_as_artifacts=True),
|
||||
):
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
)
|
||||
try:
|
||||
async for event in agen:
|
||||
if event.content.parts and event.content.parts[0].text:
|
||||
print(f'** {event.author}: {event.content.parts[0].text}')
|
||||
finally:
|
||||
await agen.aclose()
|
||||
|
||||
start_time = time.time()
|
||||
print('Start time:', start_time)
|
||||
|
||||
Reference in New Issue
Block a user