nemo_flow.scope_local#
Scope-local middleware and subscriber registration.
These helpers mirror the global guardrails, intercepts, and
subscribers modules, but the registrations apply only while the owning
scope is active. When that scope is popped, the registrations are removed
automatically.
Example:
import nemo_flow
def redact(tool_name, args):
return {**args, "api_key": "***"}
with nemo_flow.scope.scope("request", nemo_flow.ScopeType.Agent) as handle:
nemo_flow.scope_local.register_tool_sanitize_request(handle, "redact", 10, redact)
Functions#
|
Register a scope-local tool sanitize-request guardrail. |
|
Remove a scope-local tool sanitize-request guardrail. |
|
Register a scope-local tool sanitize-response guardrail. |
|
Remove a scope-local tool sanitize-response guardrail. |
|
Register a scope-local tool conditional-execution guardrail. |
|
Remove a scope-local tool conditional-execution guardrail. |
|
Register a scope-local tool request intercept. |
|
Remove a scope-local tool request intercept. |
|
Register scope-local middleware around tool execution. |
|
Remove a scope-local tool execution intercept. |
|
Register a scope-local LLM sanitize-request guardrail. |
|
Remove a scope-local LLM sanitize-request guardrail. |
|
Register a scope-local LLM sanitize-response guardrail. |
|
Remove a scope-local LLM sanitize-response guardrail. |
|
Register a scope-local LLM conditional-execution guardrail. |
|
Remove a scope-local LLM conditional-execution guardrail. |
|
Register a scope-local LLM request intercept. |
|
Remove a scope-local LLM request intercept. |
|
Register scope-local middleware around non-streaming LLM execution. |
|
Remove a scope-local LLM execution intercept. |
|
Register scope-local middleware around streaming LLM execution. |
|
Remove a scope-local streaming LLM execution intercept. |
|
Register an event subscriber that is active only for |
|
Remove a scope-local event subscriber. |
Module Contents#
- nemo_flow.scope_local.register_tool_sanitize_request(
- scope_handle,
- name,
- priority,
- guardrail,
Register a scope-local tool sanitize-request guardrail.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique guardrail name within the owning scope.
priority – Execution order for the guardrail. Lower values run first.
guardrail – Callable invoked as
guardrail(tool_name, args)that returns the sanitized payload recorded on emitted start events.
- Returns:
This function returns after the scope-local guardrail is registered.
- Return type:
None
Notes
As with the global variant, this sanitizes emitted event payloads only.
- nemo_flow.scope_local.deregister_tool_sanitize_request(scope_handle, name)#
Remove a scope-local tool sanitize-request guardrail.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Guardrail name previously passed to
register_tool_sanitize_request().
- Returns:
Trueif a guardrail was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the guardrail early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_tool_sanitize_response(
- scope_handle,
- name,
- priority,
- guardrail,
Register a scope-local tool sanitize-response guardrail.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique guardrail name within the owning scope.
priority – Execution order for the guardrail. Lower values run first.
guardrail – Callable invoked as
guardrail(tool_name, result)that returns the sanitized payload recorded on emitted end events.
- Returns:
This function returns after the scope-local guardrail is registered.
- Return type:
None
Notes
As with the global variant, this sanitizes emitted event payloads only.
- nemo_flow.scope_local.deregister_tool_sanitize_response(scope_handle, name)#
Remove a scope-local tool sanitize-response guardrail.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Guardrail name previously passed to
register_tool_sanitize_response().
- Returns:
Trueif a guardrail was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the guardrail early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_tool_conditional_execution(
- scope_handle,
- name,
- priority,
- guardrail,
Register a scope-local tool conditional-execution guardrail.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique guardrail name within the owning scope.
priority – Execution order for the guardrail. Lower values run first.
guardrail – Callable invoked as
guardrail(tool_name, args). ReturnNoneto allow execution or a rejection message to block it.
- Returns:
This function returns after the scope-local guardrail is registered.
- Return type:
None
Notes
Scope-local conditional guardrails run in addition to global conditional guardrails for calls emitted under the owning scope.
- nemo_flow.scope_local.deregister_tool_conditional_execution(scope_handle, name)#
Remove a scope-local tool conditional-execution guardrail.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Guardrail name previously passed to
register_tool_conditional_execution().
- Returns:
Trueif a guardrail was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the guardrail early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_tool_request(scope_handle, name, priority, break_chain, fn)#
Register a scope-local tool request intercept.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique intercept name within the owning scope.
priority – Execution order for the intercept. Lower values run first.
break_chain – Whether to stop applying lower-priority request intercepts after this intercept runs.
fn – Callable invoked as
fn(tool_name, args)that returns the rewritten tool arguments.
- Returns:
This function returns after the scope-local intercept is registered.
- Return type:
None
Notes
Scope-local request intercepts are merged with global intercepts using the same priority ordering rules.
- nemo_flow.scope_local.deregister_tool_request(scope_handle, name)#
Remove a scope-local tool request intercept.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Intercept name previously passed to
register_tool_request().
- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the intercept early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_tool_execution(scope_handle, name, priority, fn)#
Register scope-local middleware around tool execution.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique intercept name within the owning scope.
priority – Execution order for the intercept. Lower values run first.
fn – Callable invoked as
fn(tool_name, args, next_call). It may callnext_call(args)to continue execution, modify the result, or short-circuit the tool call entirely.
- Returns:
This function returns after the scope-local intercept is registered.
- Return type:
None
Notes
Execution intercepts wrap only calls emitted while the owning scope remains active.
- nemo_flow.scope_local.deregister_tool_execution(scope_handle, name)#
Remove a scope-local tool execution intercept.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Intercept name previously passed to
register_tool_execution().
- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the intercept early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_llm_sanitize_request(scope_handle, name, priority, guardrail)#
Register a scope-local LLM sanitize-request guardrail.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique guardrail name within the owning scope.
priority – Execution order for the guardrail. Lower values run first.
guardrail – Callable invoked as
guardrail(request)that returns the sanitized request recorded on emitted start events.
- Returns:
This function returns after the scope-local guardrail is registered.
- Return type:
None
Notes
As with the global variant, this sanitizes emitted event payloads only.
- nemo_flow.scope_local.deregister_llm_sanitize_request(scope_handle, name)#
Remove a scope-local LLM sanitize-request guardrail.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Guardrail name previously passed to
register_llm_sanitize_request().
- Returns:
Trueif a guardrail was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the guardrail early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_llm_sanitize_response(
- scope_handle,
- name,
- priority,
- guardrail,
Register a scope-local LLM sanitize-response guardrail.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique guardrail name within the owning scope.
priority – Execution order for the guardrail. Lower values run first.
guardrail – Callable invoked as
guardrail(response)that returns the sanitized payload recorded on emitted end events.
- Returns:
This function returns after the scope-local guardrail is registered.
- Return type:
None
Notes
As with the global variant, this sanitizes emitted event payloads only.
- nemo_flow.scope_local.deregister_llm_sanitize_response(scope_handle, name)#
Remove a scope-local LLM sanitize-response guardrail.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Guardrail name previously passed to
register_llm_sanitize_response().
- Returns:
Trueif a guardrail was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the guardrail early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_llm_conditional_execution(
- scope_handle,
- name,
- priority,
- guardrail,
Register a scope-local LLM conditional-execution guardrail.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique guardrail name within the owning scope.
priority – Execution order for the guardrail. Lower values run first.
guardrail – Callable invoked as
guardrail(request). ReturnNoneto allow execution or a rejection message to block it.
- Returns:
This function returns after the scope-local guardrail is registered.
- Return type:
None
Notes
Scope-local conditional guardrails run in addition to global conditional guardrails for calls emitted under the owning scope.
- nemo_flow.scope_local.deregister_llm_conditional_execution(scope_handle, name)#
Remove a scope-local LLM conditional-execution guardrail.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Guardrail name previously passed to
register_llm_conditional_execution().
- Returns:
Trueif a guardrail was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the guardrail early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_llm_request(scope_handle, name, priority, break_chain, fn)#
Register a scope-local LLM request intercept.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique intercept name within the owning scope.
priority – Execution order for the intercept. Lower values run first.
break_chain – Whether to stop applying lower-priority request intercepts after this intercept runs.
fn – Callable invoked as
fn(name, request, annotated)that returns a tuple of(request, annotated)for the next stage.
- Returns:
This function returns after the scope-local intercept is registered.
- Return type:
None
Notes
Scope-local request intercepts are merged with global intercepts using the same priority ordering rules.
- nemo_flow.scope_local.deregister_llm_request(scope_handle, name)#
Remove a scope-local LLM request intercept.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Intercept name previously passed to
register_llm_request().
- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the intercept early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_llm_execution(scope_handle, name, priority, fn)#
Register scope-local middleware around non-streaming LLM execution.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique intercept name within the owning scope.
priority – Execution order for the intercept. Lower values run first.
fn – Callable invoked as
fn(name, request, next_call)that may callnext_call(request)to continue execution or short-circuit it.
- Returns:
This function returns after the scope-local intercept is registered.
- Return type:
None
Notes
Execution intercepts wrap only calls emitted while the owning scope remains active.
- nemo_flow.scope_local.deregister_llm_execution(scope_handle, name)#
Remove a scope-local LLM execution intercept.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Intercept name previously passed to
register_llm_execution().
- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the intercept early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_llm_stream_execution(scope_handle, name, priority, fn)#
Register scope-local middleware around streaming LLM execution.
- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique intercept name within the owning scope.
priority – Execution order for the intercept. Lower values run first.
fn – Callable invoked as
fn(request, next_call)that returns an async iterator of chunks, either by delegating tonext_callor by replacing the stream entirely.
- Returns:
This function returns after the scope-local intercept is registered.
- Return type:
None
Notes
Streaming execution intercepts wrap chunk production only. They do not replace the collector or finalizer callbacks.
- nemo_flow.scope_local.deregister_llm_stream_execution(scope_handle, name)#
Remove a scope-local streaming LLM execution intercept.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Intercept name previously passed to
register_llm_stream_execution().
- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the intercept early affects only future work in the owning scope. Popping the scope would also remove it automatically.
- nemo_flow.scope_local.register_subscriber(scope_handle, name, callback)#
Register an event subscriber that is active only for
scope_handle.- Parameters:
scope_handle – Owning scope handle. The registration is removed when this scope is popped.
name – Unique subscriber name within the owning scope.
callback – Callable invoked as
callback(event)for each lifecycle event emitted while the scope remains active.
- Returns:
This function returns after the scope-local subscriber is registered.
- Return type:
None
Notes
The subscriber observes only events emitted while the owning scope remains active.
Example:
import nemo_flow def log_event(event): print(event.kind, event.name) with nemo_flow.scope.scope("request", nemo_flow.ScopeType.Agent) as handle: nemo_flow.scope_local.register_subscriber(handle, "logger", log_event)
- nemo_flow.scope_local.deregister_subscriber(scope_handle, name)#
Remove a scope-local event subscriber.
- Parameters:
scope_handle – Scope handle that owns the registration.
name – Subscriber name previously passed to
register_subscriber().
- Returns:
Trueif a subscriber was removed, otherwiseFalse.- Return type:
bool
Notes
Removing the subscriber early affects only future event delivery in the owning scope. Popping the scope would also remove it automatically.