Code Examples

View as Markdown

Use these examples for implementation surfaces:

Preferred Integration Order

Choose the highest option your framework boundary supports:

  1. Managed execution wrappers.
  2. Explicit start and end lifecycle calls.
  3. Standalone conditional-execution helpers.
  4. Standalone request-intercept helpers.
  5. Mark events for milestones that are not full tool or LLM calls.

Managed execution wrappers give NeMo Relay the most complete lifecycle: middleware order, parent-child scope relationships, request and response event payloads, execution intercepts, and subscriber visibility. Fallback helpers are useful when the framework owns the real callback internally.

Managed Execution Wrappers

Use managed wrappers when the framework exposes a stable callable boundary.

SurfacePythonNode.jsRust
Tool executenemo_relay.tools.execute(...)toolCallExecute(...)nemo_relay::api::tool::tool_call_execute
LLM executenemo_relay.llm.execute(...)llmCallExecute(...)nemo_relay::api::llm::llm_call_execute
LLM stream executenemo_relay.typed.llm_stream_execute(...)typedLlmStreamExecute(...)nemo_relay::api::llm::llm_stream_call_execute

Fallback: Explicit API Calls

Use explicit API calls when the framework has start and finish hooks but owns the invocation internally.

What you lose from managed execution wrappers:

  • Execution intercepts cannot wrap the real callback.
  • The framework must preserve the start handle and call the matching end helper.
  • Request intercepts and conditional execution must be invoked explicitly if you still need them.
  • Error paths need deliberate end-event or mark-event handling.
1import nemo_relay
2from nemo_relay import LLMRequest
3
4def framework_tool_started(name: str, args: dict):
5 return nemo_relay.tools.call(name, args)
6
7def framework_tool_finished(handle, result: dict) -> None:
8 nemo_relay.tools.call_end(handle, result)
9
10def framework_llm_started(provider: str, payload: dict):
11 request = LLMRequest({}, payload)
12 return nemo_relay.llm.call(provider, request, model_name=payload.get("model"))
13
14def framework_llm_finished(handle, response: dict) -> None:
15 nemo_relay.llm.call_end(handle, response)

Conditional Execution

Use conditional-execution helpers when the framework needs an allow-or-block decision before it continues down its own invocation path.

1import nemo_relay
2from nemo_relay import LLMRequest
3
4nemo_relay.tools.conditional_execution("search", {"query": "weather"})
5nemo_relay.llm.conditional_execution(LLMRequest({}, {"messages": []}))

Request Intercepts

Use request-intercept helpers when the framework wants NeMo Relay to rewrite arguments or provider requests before the framework invokes its own downstream code.

1import nemo_relay
2from nemo_relay import LLMRequest
3
4rewritten_args = nemo_relay.tools.request_intercepts("search", {"query": "weather"})
5outcome = nemo_relay.llm.request_intercepts(
6 "demo-provider",
7 LLMRequest({}, {"messages": []}),
8)
9rewritten_request = outcome.request

Mark Events

Use mark events when the framework exposes important milestones but not a full lifecycle boundary.

1import nemo_relay
2
3nemo_relay.scope.event("scheduler.retry", data={"attempt": 2})