nemo_flow.intercepts#
Global middleware intercept registration for tools and LLMs.
Request intercepts transform inputs before execution. Execution intercepts wrap the downstream callable and can observe, modify, or replace the result.
Example:
import nemo_flow
def add_header(name, request, annotated):
request.headers["X-Trace"] = "demo"
return request, annotated
nemo_flow.intercepts.register_llm_request("trace-header", 10, False, add_header)
Functions#
|
Register an intercept that rewrites tool arguments before execution. |
|
Remove a previously registered tool request intercept. |
|
Register middleware around tool execution. |
Remove a previously registered tool execution intercept. |
|
|
Register an intercept that rewrites an |
|
Remove a previously registered LLM request intercept. |
|
Register middleware around non-streaming LLM execution. |
|
Remove a previously registered LLM execution intercept. |
|
Register middleware around streaming LLM execution. |
Remove a previously registered streaming LLM execution intercept. |
Module Contents#
- nemo_flow.intercepts.register_tool_request(
- name: str,
- priority: int,
- break_chain: bool,
- fn: nemo_flow.ToolRequestIntercept,
Register an intercept that rewrites tool arguments before execution.
- Parameters:
name – Unique intercept name used for later replacement or removal.
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 intercept is registered.
- Return type:
None
Notes
Request intercepts run after conditional-execution guardrails and before sanitize-request guardrails or execution intercepts.
Example:
import nemo_flow def add_trace_id(tool_name, args): return {**args, "trace_id": "req-123"} nemo_flow.intercepts.register_tool_request( "trace-id", 10, False, add_trace_id, )
- nemo_flow.intercepts.deregister_tool_request(name: str) bool#
Remove a previously registered tool request intercept.
- Parameters:
name – Intercept name previously passed to
register_tool_request().- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removal affects only future executions. In-flight calls continue using the intercept chain they already resolved.
- nemo_flow.intercepts.register_tool_execution(
- name: str,
- priority: int,
- fn: nemo_flow.ToolExecutionIntercept,
Register middleware around tool execution.
- Parameters:
name – Unique intercept name used for later replacement or removal.
priority – Execution order for the intercept. Lower values run first.
fn – Callable invoked as
fn(tool_name, args, next_call). The callback may await or callnext_call(args)to continue the chain, modify the result, or bypass downstream execution entirely.
- Returns:
This function returns after the intercept is registered.
- Return type:
None
Notes
Execution intercepts wrap the downstream tool callback. They are the right place for timing, retries, short-circuiting, or result shaping.
- nemo_flow.intercepts.deregister_tool_execution(name: str) bool#
Remove a previously registered tool execution intercept.
- Parameters:
name – Intercept name previously passed to
register_tool_execution().- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removal affects only future executions. In-flight calls continue using the execution chain they already resolved.
- nemo_flow.intercepts.register_llm_request(
- name: str,
- priority: int,
- break_chain: bool,
- fn: nemo_flow.LlmRequestIntercept,
Register an intercept that rewrites an
LLMRequestbefore execution.- Parameters:
name – Unique intercept name used for later replacement or removal.
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 intercept or the provider callback.
- Returns:
This function returns after the intercept is registered.
- Return type:
None
Notes
annotatedisNoneunless a request codec was supplied to the managed LLM call. Intercepts should preserve both values when they do not need to mutate them.Example:
import nemo_flow def add_header(name, request, annotated): request.headers["X-Trace"] = "req-123" return request, annotated nemo_flow.intercepts.register_llm_request( "trace-header", 10, False, add_header, )
- nemo_flow.intercepts.deregister_llm_request(name: str) bool#
Remove a previously registered LLM request intercept.
- Parameters:
name – Intercept name previously passed to
register_llm_request().- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removal affects only future executions. In-flight calls continue using the intercept chain they already resolved.
- nemo_flow.intercepts.register_llm_execution(
- name: str,
- priority: int,
- fn: nemo_flow.LlmExecutionIntercept,
Register middleware around non-streaming LLM execution.
- Parameters:
name – Unique intercept name used for later replacement or removal.
priority – Execution order for the intercept. Lower values run first.
fn – Callable invoked as
fn(name, request, next_call). The callback may callnext_call(request)to continue execution, modify the result, or short-circuit the provider call.
- Returns:
This function returns after the intercept is registered.
- Return type:
None
Notes
Execution intercepts wrap only non-streaming LLM execution. Use
register_llm_stream_execution()for streaming callbacks.
- nemo_flow.intercepts.deregister_llm_execution(name: str) bool#
Remove a previously registered LLM execution intercept.
- Parameters:
name – Intercept name previously passed to
register_llm_execution().- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removal affects only future executions. In-flight calls continue using the execution chain they already resolved.
- nemo_flow.intercepts.register_llm_stream_execution(
- name: str,
- priority: int,
- fn: nemo_flow.LlmStreamExecutionIntercept,
Register middleware around streaming LLM execution.
- Parameters:
name – Unique intercept name used for later replacement or removal.
priority – Execution order for the intercept. Lower values run first.
fn – Callable invoked as
fn(request, next_call)that returns an async iterator of JSON chunks, either by delegating tonext_call(request)or by replacing the stream entirely.
- Returns:
This function returns after the intercept is registered.
- Return type:
None
Notes
Streaming execution intercepts wrap chunk production only. They do not replace the separate collector or finalizer callbacks.
- nemo_flow.intercepts.deregister_llm_stream_execution(name: str) bool#
Remove a previously registered streaming LLM execution intercept.
- Parameters:
name – Intercept name previously passed to
register_llm_stream_execution().- Returns:
Trueif an intercept was removed, otherwiseFalse.- Return type:
bool
Notes
Removal affects only future executions. In-flight streams continue using the execution chain they already resolved.