Skip to navigation

Generated from python/plugin/src/nemo_relay_plugin/_api.py.

Module nemo_relay_plugin.

Author out-of-process NeMo Relay worker plugins in Python.

Classes

ApplicationExecutionContext(TypedDict)

Concrete invocation context with optional application-owned JSON data.

EventSanitizeFields(TypedDict)

Observability fields returned by event sanitizer callbacks.

LlmCodecIdentity

Structured identity of the codec active for one worker invocation.

Methods

LlmCodecIdentity
def LlmCodecIdentity(kind: str, id: str | None = None) -> None

Create an instance.

RuntimeDiagnostic

One bounded runtime diagnostic reported by the Relay host.

Methods

RuntimeDiagnostic
def RuntimeDiagnostic(code: str, message: str, count: int) -> None

Create an instance.

RuntimeRegistrationKind(str, Enum)

Global runtime registration surfaces that a worker can gate.

RuntimeRegistrationOwnerKind(str, Enum)

Owner categories reported by host registration discovery.

RuntimeRegistrationOwner

Owner metadata for a discovered runtime registration.

Methods

RuntimeRegistrationOwner
def RuntimeRegistrationOwner(kind: RuntimeRegistrationOwnerKind, plugin_kind: str | None = None, component_ordinal: int | None = None) -> None

Create an instance.

RuntimeRegistrationIdentity

Structured identity for a global gateable runtime registration.

Methods

RuntimeRegistrationIdentity
def RuntimeRegistrationIdentity(kind: RuntimeRegistrationKind, local_name: str, effective_name: str, owner: RuntimeRegistrationOwner) -> None

Create an instance.

ConditionalMiddlewareGuardrailHandle

Opaque activation-owned key for removing a worker-created gate.

Methods

ConditionalMiddlewareGuardrailHandle
def ConditionalMiddlewareGuardrailHandle(_value: str, _callback_name: str) -> None

Create an instance.

RuntimeDiagnostics

Bounded snapshot of active host runtime diagnostics.

Methods

RuntimeDiagnostics
def RuntimeDiagnostics(entries: tuple[RuntimeDiagnostic, ...]) -> None

Create an instance.

get
def get(code: str) -> RuntimeDiagnostic | None

Return the diagnostic with code, when present.

LlmSanitizeRequestContext

Request codec context shared by sanitizer and execution callbacks.

Methods

LlmSanitizeRequestContext
def LlmSanitizeRequestContext(codec: LlmCodecIdentity, _runtime: 'PluginRuntime | None' = None, _capability_id: str | None = None, _invocation_id: str | None = None) -> None

Create an instance.

resolve_codec
def resolve_codec() -> 'WorkerRequestCodec | None'

Return the active request-codec proxy for this invocation.

LlmSanitizeResponseContext

Response codec context shared by sanitizer and execution callbacks.

Methods

LlmSanitizeResponseContext
def LlmSanitizeResponseContext(codec: LlmCodecIdentity, _runtime: 'PluginRuntime | None' = None, _capability_id: str | None = None, _invocation_id: str | None = None) -> None

Create an instance.

resolve_codec
def resolve_codec() -> 'WorkerResponseCodec | None'

Return the active response-codec proxy for this invocation.

WorkerRequestCodec

Invocation-scoped async proxy for an active request codec.

Methods

WorkerRequestCodec
def WorkerRequestCodec(_runtime: 'PluginRuntime', _capability_id: str, _invocation_id: str) -> None

Create an instance.

decode
async def decode(request: LlmRequest) -> AnnotatedLlmRequest

Decode a wire request with the active host codec.

encode
async def encode(annotated: AnnotatedLlmRequest, original: LlmRequest) -> LlmRequest

Encode an annotated request with the active host codec.

WorkerResponseCodec

Invocation-scoped async proxy for an active response codec.

Methods

WorkerResponseCodec
def WorkerResponseCodec(_runtime: 'PluginRuntime', _capability_id: str, _invocation_id: str) -> None

Create an instance.

decode
async def decode(response: Json) -> Json

Decode a wire response with the active host codec.

LlmApplicationExecutionContext(TypedDict)

Application data for one LLM call.

LlmExecutionContext

Directional codec context for one LLM execution invocation.

Methods

LlmExecutionContext
def LlmExecutionContext(request_codec: LlmRequestContext, response_codec: LlmResponseContext | None, execution_context: LlmApplicationExecutionContext | None = None) -> None

Create an instance.

DataSchema

Identify the schema of a mark’s opaque data payload.

Methods

DataSchema
def DataSchema(name: str, version: str) -> None

Create an instance.

to_json
def to_json() -> dict[str, str]

Return the canonical JSON representation.

LogSeverity(str, Enum)

Telemetry severity assigned to an exported mark log.

EventCategory(str, Enum)

Well-known semantic categories for Relay events.

MetricKind(str, Enum)

OpenTelemetry instrument kind recorded by a metric mark.

MetricValueType(str, Enum)

Explicit numeric representation of a metric measurement value.

MetricMeasurement(TypedDict)

One recording operation in a Relay metric-measurement mark.

WorkerSdkError(Exception)

Report a worker SDK, host-call, or protocol error to plugin code.

DiagnosticLevel(str, Enum)

Identify the severity of a plugin configuration diagnostic.

ConfigDiagnostic

Describe one problem found while validating plugin configuration.

Methods

ConfigDiagnostic
def ConfigDiagnostic(level: DiagnosticLevel | str, code: str, message: str, component: str | None = None, field: str | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Any]

Convert the diagnostic to its Relay JSON representation.

PendingMarkSpec

Describe a mark Relay emits under a managed lifecycle scope.

Methods

PendingMarkSpec
def PendingMarkSpec(name: str, category: EventCategory | str | None = None, category_profile: Json | None = None, data: Json | None = None, metadata: Json | None = None, data_schema: DataSchema | Mapping[str, Json] | None = None, severity: LogSeverity | str | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert this pending mark to its canonical JSON object.

LlmOptimizationDataSchema

Identify the schema of an opaque optimization contribution payload.

Methods

LlmOptimizationDataSchema
def LlmOptimizationDataSchema(name: str, version: str) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert the schema tag to its canonical wire representation.

from_json
def from_json(value: Mapping[str, Json]) -> LlmOptimizationDataSchema

Decode a canonical schema tag.

LlmOptimizationModel

Identify one model for optimization accounting and repricing.

Methods

LlmOptimizationModel
def LlmOptimizationModel(model: str, provider: str | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert the model identity to canonical JSON.

from_json
def from_json(value: Mapping[str, Json]) -> LlmOptimizationModel

Decode a canonical model identity.

LlmOptimizationModelTransition

Describe the counterfactual and effective models for one optimization.

Methods

LlmOptimizationModelTransition
def LlmOptimizationModelTransition(baseline: LlmOptimizationModel | None = None, effective: LlmOptimizationModel | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert the model transition to canonical JSON.

from_json
def from_json(value: Mapping[str, Json]) -> LlmOptimizationModelTransition

Decode a canonical model transition.

LlmOptimizationTokens

Retain token evidence independently from monetary pricing.

Methods

LlmOptimizationTokens
def LlmOptimizationTokens(prompt_tokens: int | None = None, completion_tokens: int | None = None, cache_read_tokens: int | None = None, cache_write_tokens: int | None = None, total_tokens: int | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert populated token fields to canonical JSON.

from_json
def from_json(value: Mapping[str, Json]) -> LlmOptimizationTokens

Decode canonical token evidence.

LlmOptimizationEvidenceQuality(str, Enum)

Classify token evidence as directly observed or estimated.

LlmOptimizationTokenImpact

Describe baseline, effective, and explicitly saved token evidence.

Methods

LlmOptimizationTokenImpact
def LlmOptimizationTokenImpact(baseline: LlmOptimizationTokens | None = None, effective: LlmOptimizationTokens | None = None, saved: LlmOptimizationTokens | None = None, quality: LlmOptimizationEvidenceQuality | str | None = None, estimation_method: str | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert the token impact to canonical JSON.

from_json
def from_json(value: Mapping[str, Json]) -> LlmOptimizationTokenImpact

Decode a canonical token impact.

LlmOptimizationContribution

One plugin’s lossless, forward-compatible optimization evidence.

Methods

LlmOptimizationContribution
def LlmOptimizationContribution(producer: str, kind: str, applied: bool = False, id: str | None = None, sequence: int | None = None, model_transition: LlmOptimizationModelTransition | None = None, token_impact: LlmOptimizationTokenImpact | None = None, payload_schema: LlmOptimizationDataSchema | None = None, payload: Json | None = None, extra: dict[str, Json] = ...) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert this contribution while flattening unknown top-level fields.

from_json
def from_json(value: Mapping[str, Json]) -> LlmOptimizationContribution

Decode all known fields and retain unknown top-level fields in extra.

LlmRequestInterceptOutcome

Canonical result returned by a Python worker LLM request intercept.

Methods

LlmRequestInterceptOutcome
def LlmRequestInterceptOutcome(request: LlmRequest, annotated_request: AnnotatedLlmRequest | None = None, pending_marks: list[PendingMarkSpec] = ..., optimization_contributions: list[LlmOptimizationContribution] = ...) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert this outcome to the canonical worker-envelope payload.

ToolExecutionContext

Per-call context provided to a tool execution intercept.

Methods

ToolExecutionContext
def ToolExecutionContext(tool_name: str, args: Json, tool_call_id: str | None = None, execution_context: ApplicationExecutionContext | None = None) -> None

Create an instance.

ToolExecutionResult

Canonical application-visible result of tool execution.

Methods

ToolExecutionResult
def ToolExecutionResult(result: Json, annotation: Json | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert this result to its canonical JSON representation.

from_json
def from_json(value: Json) -> 'ToolExecutionResult'

Decode and validate a canonical JSON representation.

ToolExecutionInterceptOutcome

Canonical result returned by a Python worker tool execution intercept.

Methods

ToolExecutionInterceptOutcome
def ToolExecutionInterceptOutcome(result: Json, pending_marks: list[PendingMarkSpec] = ..., *, annotation: Json | None = None) -> None

Create an instance.

to_json
def to_json() -> dict[str, Json]

Convert this outcome to its canonical JSON representation.

ScopeType(str, Enum)

Identify the semantic category of a Relay execution scope.

ScopeStackEntry

Canonical scope UUID and semantic type from the visible stack.

Methods

ScopeStackEntry
def ScopeStackEntry(scope_id: str, scope_type: ScopeType) -> None

Create an instance.

ScopeStackSnapshot

Detached root-to-top entries including implicit and synthetic roots.

Methods

ScopeStackSnapshot
def ScopeStackSnapshot(scopes: list[ScopeStackEntry]) -> None

Create an instance.

WorkerPlugin

Define the validation and registration contract for a worker plugin.

Methods

validate
def validate(config: Json) -> list[ConfigDiagnostic | dict[str, Any]] | Awaitable[list[ConfigDiagnostic | dict[str, Any]]]

Validate one component configuration before registration.

register
def register(ctx: PluginContext, config: Json) -> None | Awaitable[None]

Register plugin callbacks for one component configuration.

PluginContext

Register component-scoped callbacks and access the Relay host runtime.

Methods

PluginContext
def PluginContext(runtime: PluginRuntime | None = None) -> None

Create an instance.

runtime
def runtime() -> PluginRuntime

Return the host runtime handle for event and scope operations.

register_subscriber
def register_subscriber(name: str, callback: SubscriberCallback) -> None

Register a callback that receives Relay events.

register_conditional_middleware_guardrail
def register_conditional_middleware_guardrail(name: str, kinds: set[RuntimeRegistrationKind], registration_name: str, callback: ConditionalMiddlewareCallback) -> None

Declare a callback-based gate installed with this activation.

register_event_metadata_injector
def register_event_metadata_injector(name: str, callback: EventMetadataInjectorCallback, *, priority: int = 0) -> None

Register a callback that proposes additions to Event metadata.

register_mark_sanitize_guardrail
def register_mark_sanitize_guardrail(name: str, callback: EventSanitizeCallback, *, priority: int = 0) -> None

Register a sanitizer for mark event observability fields.

register_scope_sanitize_start_guardrail
def register_scope_sanitize_start_guardrail(name: str, callback: EventSanitizeCallback, *, priority: int = 0) -> None

Register a sanitizer for scope start event observability fields.

register_scope_sanitize_end_guardrail
def register_scope_sanitize_end_guardrail(name: str, callback: EventSanitizeCallback, *, priority: int = 0) -> None

Register a sanitizer for scope end event observability fields.

register_tool_sanitize_request_guardrail
def register_tool_sanitize_request_guardrail(name: str, callback: ToolSanitizeCallback, *, priority: int = 0) -> None

Register a guardrail that sanitizes tool input for observability.

register_tool_sanitize_response_guardrail
def register_tool_sanitize_response_guardrail(name: str, callback: ToolSanitizeCallback, *, priority: int = 0) -> None

Register a guardrail that sanitizes tool output for observability.

register_tool_conditional_execution_guardrail
def register_tool_conditional_execution_guardrail(name: str, callback: ToolConditionalCallback, *, priority: int = 0) -> None

Register a guardrail that can block tool execution.

register_tool_request_intercept
def register_tool_request_intercept(name: str, callback: ToolRequestCallback, *, priority: int = 0, break_chain: bool = False) -> None

Register an intercept that rewrites tool arguments.

register_tool_execution_intercept
def register_tool_execution_intercept(name: str, callback: ToolExecutionCallback, *, priority: int = 0) -> None

Register middleware around real tool execution.

register_llm_sanitize_request_guardrail
def register_llm_sanitize_request_guardrail(name: str, callback: LlmSanitizeRequestCallback, *, priority: int = 0) -> None

Register a guardrail that sanitizes an LLM request for observability.

register_llm_sanitize_response_guardrail
def register_llm_sanitize_response_guardrail(name: str, callback: LlmSanitizeResponseCallback, *, priority: int = 0) -> None

Register a guardrail that sanitizes an LLM response for observability.

register_llm_conditional_execution_guardrail
def register_llm_conditional_execution_guardrail(name: str, callback: LlmConditionalCallback, *, priority: int = 0) -> None

Register a guardrail that can block LLM execution.

register_llm_request_intercept
def register_llm_request_intercept(name: str, callback: LlmRequestCallback, *, priority: int = 0, break_chain: bool = False) -> None

Register an intercept that rewrites an LLM request.

register_llm_execution_intercept
def register_llm_execution_intercept(name: str, callback: LlmExecutionCallback, *, priority: int = 0) -> None

Register middleware around real LLM execution.

register_llm_stream_execution_intercept
def register_llm_stream_execution_intercept(name: str, callback: LlmStreamExecutionCallback, *, priority: int = 0) -> None

Register middleware around real streaming LLM execution.

PluginRuntime

Call event, scope, and continuation operations on the Relay host.

Methods

PluginRuntime
def PluginRuntime(*, activation_id: str, auth_token: str, host_stub: Any, diagnostic_stub: Any = None) -> None

Create an instance.

diagnostics
async def diagnostics() -> WorkerDiagnostics

Negotiate once; disabled/unsupported facades skip diagnostic factories.

log
async def log(level: str, message: str, *, target: str = '', fields: Mapping[str, Json] | None = None) -> None

Emit one operational log record through Relay’s configured sinks.

trace
async def trace(message: str, *, target: str = '', fields: Mapping[str, Json] | None = None) -> None

Emit a trace-level operational log record through Relay.

debug
async def debug(message: str, *, target: str = '', fields: Mapping[str, Json] | None = None) -> None

Emit a debug-level operational log record through Relay.

info
async def info(message: str, *, target: str = '', fields: Mapping[str, Json] | None = None) -> None

Emit an info-level operational log record through Relay.

warn
async def warn(message: str, *, target: str = '', fields: Mapping[str, Json] | None = None) -> None

Emit a warn-level operational log record through Relay.

error
async def error(message: str, *, target: str = '', fields: Mapping[str, Json] | None = None) -> None

Emit an error-level operational log record through Relay.

list_runtime_registrations
async def list_runtime_registrations(kinds: Iterable[RuntimeRegistrationKind] | None = None) -> list[RuntimeRegistrationIdentity]

List global gateable registrations known to the Relay host.

register_conditional_middleware_guardrail
async def register_conditional_middleware_guardrail(name: str, kinds: set[RuntimeRegistrationKind], registration_name: str, callback: ConditionalMiddlewareCallback) -> ConditionalMiddlewareGuardrailHandle

Register a callback-based gate owned by this worker activation.

deregister_conditional_middleware_guardrail
async def deregister_conditional_middleware_guardrail(handle: ConditionalMiddlewareGuardrailHandle) -> bool

Remove a callback-based gate owned by this worker activation.

inspect_llm_content
async def inspect_llm_content(source: Literal['request', 'response', 'message', 'content'], value: Json, selection: Literal['last_message', 'message_index', 'instructions', 'system_prompt', 'last_user_message', 'output', 'all'], *, message_index: int | None = None) -> codecs.MessageView | None

Select normalized content through the Rust host and retain an owned snapshot.

emit_mark
async def emit_mark(name: str, data: Json | None = None, metadata: Json | None = None, *, data_schema: DataSchema | Mapping[str, Json] | None = None, severity: LogSeverity | str | None = None, category: EventCategory | str | None = None, scope_stack_id: str | None = None, parent_scope_id: str | None = None) -> None

Emit a mark event through the Relay host runtime.

current_scope_stack_snapshot
async def current_scope_stack_snapshot() -> ScopeStackSnapshot | None

Return owned scope UUIDs/types, or None when no context is bound.

runtime_diagnostics
async def runtime_diagnostics() -> RuntimeDiagnostics

Return a bounded snapshot of active host runtime diagnostics.

emit_metric
async def emit_metric(name: str, measurements: Iterable[MetricMeasurement | Mapping[str, Json]], metadata: Json | None = None, *, category: EventCategory | str | None = None, scope_stack_id: str | None = None, parent_scope_id: str | None = None) -> None

Emit a Relay metric-measurement mark through the host runtime.

create_scope_stack
async def create_scope_stack() -> str

Create an isolated, host-owned scope stack.

drop_scope_stack
async def drop_scope_stack(scope_stack_id: str) -> None

Drop an isolated, host-owned scope stack.

push_scope
async def push_scope(name: str, *, scope_type: ScopeType = ScopeType.CUSTOM, data: Json | None = None, metadata: Json | None = None, input: Json | None = None, scope_stack_id: str | None = None, parent_scope_id: str | None = None, timestamp: datetime | None = None) -> str

Start a scope on a Relay host-owned stack.

pop_scope
async def pop_scope(scope_handle_id: str, *, output: Json | None = None, metadata: Json | None = None, timestamp: datetime | None = None) -> None

End a host scope by its handle identifier.

bind_scope_stack
def bind_scope_stack(scope_stack_id: str | None, *, parent_scope_id: str | None = None) -> Generator[None]

Temporarily bind host calls to a worker-selected scope stack.

clear_scope_stack
def clear_scope_stack() -> Generator[None]

Temporarily clear worker scope-stack correlation.

current_scope_stack_id
def current_scope_stack_id() -> str | None

Return the locally bound scope stack identifier.

current_parent_scope_id
def current_parent_scope_id() -> str | None

Return the locally bound parent scope identifier.

ToolNext

Continue the remaining tool execution chain.

Methods

ToolNext
def ToolNext(runtime: PluginRuntime, continuation_id: str, *, callback_name: str | None = None) -> None

Create an instance.

call
async def call(value: Json) -> ToolExecutionResult

Call the remaining tool execution chain with replacement arguments.

LlmNext

Continue the remaining unary LLM execution chain.

Methods

LlmNext
def LlmNext(runtime: PluginRuntime, continuation_id: str, *, callback_name: str | None = None) -> None

Create an instance.

call
async def call(request: LlmRequest) -> Json

Call the remaining LLM execution chain with a replacement request.

LlmStreamNext

Continue the remaining streaming LLM execution chain.

Methods

LlmStreamNext
def LlmStreamNext(runtime: PluginRuntime, continuation_id: str, *, callback_name: str | None = None) -> None

Create an instance.

call
def call(request: LlmRequest) -> AsyncIterator[Json]

Call the remaining LLM stream chain with a replacement request.

Functions

serve_plugin

async def serve_plugin(plugin: _SupportsWorkerPlugin) -> None

Run a local nvidia.nemo.relay.worker.v1 worker until the Relay host shuts it down.

http_error_message

def http_error_message(status: int, message: str) -> str

Format an HTTP error status (400–599) for Relay plugin error forwarding.

Type Aliases And Constants

  • AnnotatedLlmRequest
  • ConditionalMiddlewareCallback
  • Event
  • EventMetadataInjectorCallback
  • EventSanitizeCallback
  • Json
  • LlmConditionalCallback
  • LlmExecutionCallback
  • LlmRequest
  • LlmRequestCallback
  • LlmRequestContext
  • LlmResponseContext
  • LlmSanitizeRequestCallback
  • LlmSanitizeResponseCallback
  • LlmStreamExecutionCallback
  • METRIC_DATA_SCHEMA_NAME
  • METRIC_DATA_SCHEMA_VERSION
  • SubscriberCallback
  • ToolConditionalCallback
  • ToolExecutionCallback
  • ToolRequestCallback
  • ToolSanitizeCallback
  • WORKER_PROTOCOL
  • grpc

Re-exports

  • CALLBACK_NAME
  • DiagnosticDeliveryError
  • MessageLimitInterceptor
  • RpcHostChannel
  • WorkerDiagnostics
  • codecs
  • common_pb
  • content_pb
  • diagnostics_grpc
  • event_pb
  • host_grpc
  • host_pb
  • llm_pb
  • named_message_limits
  • runtime_pb
  • worker_grpc
  • worker_pb