Skip to content

Commit 2f4f561

Browse files
kiberguscopybara-github
authored andcommitted
fix:Remove unnecessary Aclosing
PiperOrigin-RevId: 822496695
1 parent 4df9263 commit 2f4f561

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

src/google/adk/agents/base_agent.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -282,27 +282,22 @@ async def run_async(
282282
Event: the events generated by the agent.
283283
"""
284284

285-
async def _run_with_trace() -> AsyncGenerator[Event, None]:
286-
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
287-
ctx = self._create_invocation_context(parent_context)
288-
tracing.trace_agent_invocation(span, self, ctx)
289-
if event := await self._handle_before_agent_callback(ctx):
290-
yield event
291-
if ctx.end_invocation:
292-
return
293-
294-
async with Aclosing(self._run_async_impl(ctx)) as agen:
295-
async for event in agen:
296-
yield event
297-
298-
if ctx.end_invocation:
299-
return
285+
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
286+
ctx = self._create_invocation_context(parent_context)
287+
tracing.trace_agent_invocation(span, self, ctx)
288+
if event := await self._handle_before_agent_callback(ctx):
289+
yield event
290+
if ctx.end_invocation:
291+
return
300292

301-
if event := await self._handle_after_agent_callback(ctx):
293+
async with Aclosing(self._run_async_impl(ctx)) as agen:
294+
async for event in agen:
302295
yield event
303296

304-
async with Aclosing(_run_with_trace()) as agen:
305-
async for event in agen:
297+
if ctx.end_invocation:
298+
return
299+
300+
if event := await self._handle_after_agent_callback(ctx):
306301
yield event
307302

308303
@final
@@ -320,24 +315,19 @@ async def run_live(
320315
Event: the events generated by the agent.
321316
"""
322317

323-
async def _run_with_trace() -> AsyncGenerator[Event, None]:
324-
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
325-
ctx = self._create_invocation_context(parent_context)
326-
tracing.trace_agent_invocation(span, self, ctx)
327-
if event := await self._handle_before_agent_callback(ctx):
328-
yield event
329-
if ctx.end_invocation:
330-
return
331-
332-
async with Aclosing(self._run_live_impl(ctx)) as agen:
333-
async for event in agen:
334-
yield event
318+
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
319+
ctx = self._create_invocation_context(parent_context)
320+
tracing.trace_agent_invocation(span, self, ctx)
321+
if event := await self._handle_before_agent_callback(ctx):
322+
yield event
323+
if ctx.end_invocation:
324+
return
335325

336-
if event := await self._handle_after_agent_callback(ctx):
326+
async with Aclosing(self._run_live_impl(ctx)) as agen:
327+
async for event in agen:
337328
yield event
338329

339-
async with Aclosing(_run_with_trace()) as agen:
340-
async for event in agen:
330+
if event := await self._handle_after_agent_callback(ctx):
341331
yield event
342332

343333
async def _run_async_impl(

0 commit comments

Comments
 (0)