Create a scope for the duration of a ``with`` block. OTEL status codes will be automatically recorded in the scope's metadata. Args: name: Human-readable name for the new scope. scope_type: Semantic scope type, such as ``ScopeType.Agent`` or ``ScopeType.Function
(
name: str,
scope_type: ScopeType,
*,
handle: ScopeHandle | None = None,
attributes: ScopeAttributes | None = None,
data: Json | None = None,
metadata: Json | None = None,
input: Json | None = None,
timestamp: datetime | None = None,
end_timestamp: datetime | None = None,
)
| 205 | |
| 206 | @contextmanager |
| 207 | def scope( |
| 208 | name: str, |
| 209 | scope_type: ScopeType, |
| 210 | *, |
| 211 | handle: ScopeHandle | None = None, |
| 212 | attributes: ScopeAttributes | None = None, |
| 213 | data: Json | None = None, |
| 214 | metadata: Json | None = None, |
| 215 | input: Json | None = None, |
| 216 | timestamp: datetime | None = None, |
| 217 | end_timestamp: datetime | None = None, |
| 218 | ) -> Iterator[ScopeHandle]: |
| 219 | """Create a scope for the duration of a ``with`` block. |
| 220 | |
| 221 | OTEL status codes will be automatically recorded in the scope's metadata. |
| 222 | |
| 223 | Args: |
| 224 | name: Human-readable name for the new scope. |
| 225 | scope_type: Semantic scope type, such as ``ScopeType.Agent`` or |
| 226 | ``ScopeType.Function``. |
| 227 | handle: Optional parent scope handle. When omitted, the current |
| 228 | top-of-stack scope becomes the parent. |
| 229 | attributes: Optional native scope attributes attached to the emitted |
| 230 | start event. |
| 231 | data: Optional JSON application payload stored on the scope handle. |
| 232 | metadata: Optional JSON metadata recorded on the scope start event. |
| 233 | input: Optional JSON payload exported as the semantic scope input. |
| 234 | timestamp: Optional timezone-aware ``datetime`` recorded as the handle |
| 235 | start time and on the scope start event. |
| 236 | end_timestamp: Optional timezone-aware ``datetime`` recorded on the |
| 237 | scope end event. |
| 238 | |
| 239 | Yields: |
| 240 | ScopeHandle: Handle for the scope that remains active inside the |
| 241 | ``with`` block. |
| 242 | |
| 243 | Notes: |
| 244 | The scope is always popped when the ``with`` block exits, even if the |
| 245 | body raises an exception. Timestamp arguments must be timezone-aware |
| 246 | ``datetime`` objects; strings and naive datetimes are rejected. |
| 247 | |
| 248 | Example:: |
| 249 | |
| 250 | import nemo_relay |
| 251 | |
| 252 | with nemo_relay.scope.scope( |
| 253 | "demo", |
| 254 | nemo_relay.ScopeType.Agent, |
| 255 | handle=None, |
| 256 | attributes=None, |
| 257 | data={"stage": "start"}, |
| 258 | metadata={"owner": "docs"}, |
| 259 | ) as handle: |
| 260 | nemo_relay.scope.event("inside", handle=handle, data={"ok": True}, metadata={"step": 1}) |
| 261 | """ |
| 262 | _ensure_scope_stack() |
| 263 | pushed_handle = None |
| 264 | status_code = "UNSET" |
no test coverage detected