Skip to content

Commit 218700d

Browse files
authored
Python: fix(core): run cleanup hooks when a per-update transform raises in ResponseStream (microsoft#8765) (microsoft#8774)
1 parent 1092349 commit 218700d

2 files changed

Lines changed: 57 additions & 14 deletions

File tree

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,10 +3504,10 @@ async def _record_update(self, update: UpdateT) -> UpdateT:
35043504

35053505
async def __anext__(self) -> UpdateT:
35063506
while True:
3507-
if self._pending_mapped_updates:
3508-
return await self._record_update(self._pending_mapped_updates.pop(0))
3509-
35103507
try:
3508+
if self._pending_mapped_updates:
3509+
return await self._record_update(self._pending_mapped_updates.pop(0))
3510+
35113511
with contextlib.ExitStack() as stack:
35123512
for factory in self._pull_context_manager_factories:
35133513
stack.enter_context(factory())
@@ -3519,6 +3519,18 @@ async def __anext__(self) -> UpdateT:
35193519
stream = await self._get_stream()
35203520
self._iterator = stream.__aiter__()
35213521
update: UpdateT = await self._iterator.__anext__()
3522+
3523+
if self._flat_map_update is not None:
3524+
mapped_updates = self._flat_map_update(update)
3525+
if isawaitable(mapped_updates):
3526+
mapped_updates = await mapped_updates
3527+
self._pending_mapped_updates.extend(mapped_updates)
3528+
continue
3529+
if self._map_update is not None:
3530+
update = self._map_update(update) # type: ignore[assignment]
3531+
if isawaitable(update):
3532+
update = await update
3533+
return await self._record_update(update)
35223534
except StopAsyncIteration:
35233535
self._consumed = True
35243536
await self._run_cleanup_hooks()
@@ -3531,17 +3543,6 @@ async def __anext__(self) -> UpdateT:
35313543
finally:
35323544
self._stream_error = None
35333545
raise
3534-
if self._flat_map_update is not None:
3535-
mapped_updates = self._flat_map_update(update)
3536-
if isawaitable(mapped_updates):
3537-
mapped_updates = await mapped_updates
3538-
self._pending_mapped_updates.extend(mapped_updates)
3539-
continue
3540-
if self._map_update is not None:
3541-
update = self._map_update(update) # type: ignore[assignment]
3542-
if isawaitable(update):
3543-
update = await update
3544-
return await self._record_update(update)
35453546

35463547
async def close(self) -> None:
35473548
"""Close the active iterator and run cleanup hooks.

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4450,6 +4450,48 @@ async def async_cleanup() -> None:
44504450

44514451
assert cleanup_called["value"] is True
44524452

4453+
async def test_transform_hook_raising_runs_cleanup(self) -> None:
4454+
"""Cleanup hook is called when a transform hook raises mid-stream."""
4455+
cleanup_called = {"value": False}
4456+
4457+
def cleanup_hook() -> None:
4458+
cleanup_called["value"] = True
4459+
4460+
def failing_hook(update: ChatResponseUpdate) -> ChatResponseUpdate:
4461+
raise RuntimeError("hook error")
4462+
4463+
stream = ResponseStream(
4464+
_generate_updates(1),
4465+
finalizer=_combine_updates,
4466+
transform_hooks=[failing_hook], # type: ignore[arg-type]
4467+
cleanup_hooks=[cleanup_hook],
4468+
)
4469+
4470+
with pytest.raises(RuntimeError, match="hook error"):
4471+
async for _ in stream:
4472+
pass
4473+
4474+
assert cleanup_called["value"] is True
4475+
4476+
async def test_mapper_raising_runs_cleanup(self) -> None:
4477+
"""Cleanup hook on a mapped stream is called when the mapper raises mid-stream."""
4478+
cleanup_called = {"value": False}
4479+
4480+
def cleanup_hook() -> None:
4481+
cleanup_called["value"] = True
4482+
4483+
def failing_mapper(update: ChatResponseUpdate) -> ChatResponseUpdate:
4484+
raise ValueError("mapper error")
4485+
4486+
inner = ResponseStream(_generate_updates(1), finalizer=_combine_updates)
4487+
outer = inner.map(failing_mapper, _combine_updates).with_cleanup_hook(cleanup_hook)
4488+
4489+
with pytest.raises(ValueError, match="mapper error"):
4490+
async for _ in outer:
4491+
pass
4492+
4493+
assert cleanup_called["value"] is True
4494+
44534495

44544496
class TestResponseStreamResultHooks:
44554497
"""Tests for result hooks (after finalizer)."""

0 commit comments

Comments
 (0)