|
7 | 7 |
|
8 | 8 | from __future__ import annotations as _annotations |
9 | 9 |
|
10 | | -import inspect |
11 | | -import types |
12 | 10 | import uuid |
13 | 11 | from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Iterable, Sequence |
14 | 12 | from contextlib import AbstractContextManager, ExitStack, asynccontextmanager, contextmanager |
@@ -188,7 +186,7 @@ async def run( |
188 | 186 | """ |
189 | 187 | if infer_name and self.name is None: |
190 | 188 | inferred_name = infer_obj_name(self, depth=2) |
191 | | - if inferred_name is not None: |
| 189 | + if inferred_name is not None: # pragma: no branch |
192 | 190 | self.name = inferred_name |
193 | 191 |
|
194 | 192 | async with self.iter(state=state, deps=deps, inputs=inputs, span=span, infer_name=False) as graph_run: |
@@ -229,9 +227,9 @@ async def iter( |
229 | 227 | A GraphRun instance that can be iterated for step-by-step execution |
230 | 228 | """ |
231 | 229 | if infer_name and self.name is None: |
232 | | - # f_back because `asynccontextmanager` adds one frame |
233 | | - if frame := inspect.currentframe(): # pragma: no branch |
234 | | - self._infer_name(frame.f_back) |
| 230 | + inferred_name = infer_obj_name(self, depth=3) # depth=3 because asynccontextmanager adds one |
| 231 | + if inferred_name is not None: # pragma: no branch |
| 232 | + self.name = inferred_name |
235 | 233 |
|
236 | 234 | with ExitStack() as stack: |
237 | 235 | entered_span: AbstractSpan | None = None |
@@ -277,26 +275,6 @@ def __str__(self) -> str: |
277 | 275 | """ |
278 | 276 | return self.render() |
279 | 277 |
|
280 | | - def _infer_name(self, function_frame: types.FrameType | None) -> None: |
281 | | - """Infer the agent name from the call frame. |
282 | | -
|
283 | | - Usage should be `self._infer_name(inspect.currentframe())`. |
284 | | -
|
285 | | - Copied from `Agent`. |
286 | | - """ |
287 | | - assert self.name is None, 'Name already set' |
288 | | - if function_frame is not None and (parent_frame := function_frame.f_back): # pragma: no branch |
289 | | - for name, item in parent_frame.f_locals.items(): |
290 | | - if item is self: |
291 | | - self.name = name |
292 | | - return |
293 | | - if parent_frame.f_locals != parent_frame.f_globals: # pragma: no branch |
294 | | - # if we couldn't find the agent in locals and globals are a different dict, try globals |
295 | | - for name, item in parent_frame.f_globals.items(): # pragma: no branch |
296 | | - if item is self: |
297 | | - self.name = name |
298 | | - return |
299 | | - |
300 | 278 |
|
301 | 279 | @dataclass |
302 | 280 | class GraphTask: |
|
0 commit comments