nemo_flow.scope#
Scope stack operations.
Scopes define the hierarchy that tool calls, LLM calls, and mark events attach to. They are the main way to model agents, tasks, and nested units of work.
Example:
import nemo_flow
with nemo_flow.scope.scope("demo-agent", nemo_flow.ScopeType.Agent) as handle:
nemo_flow.scope.event("checkpoint", handle=handle, data={"step": 1})
Functions#
Return the current top-of-stack |
|
|
Push a new child scope and return its handle. |
|
Pop a scope previously returned by |
|
Emit a |
|
Create a scope for the duration of a |
Module Contents#
- nemo_flow.scope.get_handle() nemo_flow._native.ScopeHandle#
Return the current top-of-stack
ScopeHandle.- Returns:
The scope currently at the top of the active scope stack.
- Return type:
Notes
If the current Python context does not yet have a scope stack, one is created automatically before the handle lookup.
- nemo_flow.scope.push(
- name: str,
- scope_type: nemo_flow._native.ScopeType,
- *,
- handle: nemo_flow._native.ScopeHandle | None = None,
- attributes: nemo_flow._native.ScopeAttributes | None = None,
- data: nemo_flow.Json | None = None,
- metadata: nemo_flow.Json | None = None,
- input: nemo_flow.Json | None = None,
- timestamp: datetime.datetime | None = None,
Push a new child scope and return its handle.
- Parameters:
name – Human-readable name for the new scope.
scope_type – Semantic scope type, such as
ScopeType.AgentorScopeType.Function.handle – Optional parent scope handle. When omitted, the current top-of-stack scope becomes the parent.
attributes – Optional native scope attributes attached to the emitted start event.
data – Optional JSON application payload stored on the scope handle.
metadata – Optional JSON metadata recorded on the scope start event.
input – Optional JSON payload exported as the semantic scope input.
timestamp – Optional timezone-aware
datetimerecorded as the handle start time and on the scope start event. When omitted, the current runtime time is used.
- Returns:
Handle for the newly pushed scope.
- Return type:
Notes
A scope stack is created automatically if the current context does not yet have one.
timestampmust be a timezone-awaredatetime; strings and naive datetimes are rejected.Example:
import nemo_flow with nemo_flow.scope.scope("parent", nemo_flow.ScopeType.Agent) as parent: handle = nemo_flow.scope.push( "worker", nemo_flow.ScopeType.Function, handle=parent, attributes=None, data={"step": 1}, metadata={"source": "scope.push"}, ) nemo_flow.scope.pop(handle)
- nemo_flow.scope.pop(
- handle: nemo_flow._native.ScopeHandle,
- *,
- output: nemo_flow.Json | None = None,
- timestamp: datetime.datetime | None = None,
Pop a scope previously returned by
push()orscope().- Parameters:
handle – Scope handle to close.
output – Optional JSON payload exported as the semantic scope output.
timestamp – Optional timezone-aware
datetimerecorded on the scope end event. When omitted, the runtime default end timestamp is used.
- Returns:
This function returns after the scope is closed successfully.
- Return type:
None
Notes
The handle must correspond to an active scope in the current scope stack. Popping a scope also removes any scope-local registrations owned by that scope.
timestampmust be a timezone-awaredatetime; strings and naive datetimes are rejected.
- nemo_flow.scope.event(
- name: str,
- *,
- handle: nemo_flow._native.ScopeHandle | None = None,
- data: nemo_flow.Json | None = None,
- metadata: nemo_flow.Json | None = None,
- timestamp: datetime.datetime | None = None,
Emit a
Markevent under the current or provided scope.- Parameters:
name – Event name to emit.
handle – Optional scope handle that should own the event. When omitted, the current top-of-stack scope is used.
data – Optional JSON payload attached to the event.
metadata – Optional JSON metadata attached to the event.
timestamp – Optional timezone-aware
datetimerecorded on the mark event. When omitted, the current runtime time is used.
- Returns:
This function returns after the event has been emitted.
- Return type:
None
Notes
A scope stack is created automatically when needed before the event is emitted through the native runtime.
timestampmust be a timezone-awaredatetime; strings and naive datetimes are rejected.
- nemo_flow.scope.scope(
- name: str,
- scope_type: nemo_flow._native.ScopeType,
- *,
- handle: nemo_flow._native.ScopeHandle | None = None,
- attributes: nemo_flow._native.ScopeAttributes | None = None,
- data: nemo_flow.Json | None = None,
- metadata: nemo_flow.Json | None = None,
- input: nemo_flow.Json | None = None,
- timestamp: datetime.datetime | None = None,
- end_timestamp: datetime.datetime | None = None,
Create a scope for the duration of a
withblock.- Parameters:
name – Human-readable name for the new scope.
scope_type – Semantic scope type, such as
ScopeType.AgentorScopeType.Function.handle – Optional parent scope handle. When omitted, the current top-of-stack scope becomes the parent.
attributes – Optional native scope attributes attached to the emitted start event.
data – Optional JSON application payload stored on the scope handle.
metadata – Optional JSON metadata recorded on the scope start event.
input – Optional JSON payload exported as the semantic scope input.
timestamp – Optional timezone-aware
datetimerecorded as the handle start time and on the scope start event.end_timestamp – Optional timezone-aware
datetimerecorded on the scope end event.
- Yields:
ScopeHandle – Handle for the scope that remains active inside the
withblock.
Notes
The scope is always popped when the
withblock exits, even if the body raises an exception. Timestamp arguments must be timezone-awaredatetimeobjects; strings and naive datetimes are rejected.Example:
import nemo_flow with nemo_flow.scope.scope( "demo", nemo_flow.ScopeType.Agent, handle=None, attributes=None, data={"stage": "start"}, metadata={"owner": "docs"}, ) as handle: nemo_flow.scope.event("inside", handle=handle, data={"ok": True}, metadata={"step": 1})