Handle Non-Serializable Data
Use this guide when a framework exposes SDK clients, streams, callbacks, file handles, or custom classes at the same boundary where you need NeMo Relay instrumentation.
What You Build
You will keep non-serializable framework objects in framework-owned storage and pass only stable JSON projections through NeMo Relay middleware and event payloads.
Before You Start
You need:
- A stable request ID, tool call ID, or framework request object that can key external storage.
- A JSON-compatible projection of the data that guardrails, intercepts, and subscribers need.
- A cleanup point for framework-owned object maps.
The Constraint
NeMo Relay middleware surfaces operate on JSON-compatible data. Frameworks do not always expose tool or model requests in that form.
Non-Finite Numbers
NeMo Relay’s runtime payload contract is JSON. JSON numbers must be finite;
NaN, positive infinity, and negative infinity are not valid JSON numbers.
Refer to the JSON Schema numeric reference.
This restriction applies to every language binding. The conversion behavior depends on how the application constructs and passes the payload:
Validate numeric fields before constructing the JSON payload if you need to reject non-finite values rather than lose the distinction through conversion.
In the Python binding, conversion to the runtime JSON model maps non-finite
floats to JSON null, including values nested in dictionaries and lists.
For example, {"top_k": float("nan")} becomes {"top_k": None} when read
back in Python. The same conversion applies to float("inf") and
float("-inf").
For nemo_relay.tools.execute, this conversion happens before conditional
guardrails, request intercepts, and the tool callback run. These callbacks see
None and cannot distinguish an original non-finite float from an explicit
None. The conversion does not raise an error or emit a separate lifecycle
event recording the change.
If your application must reject non-finite numbers, validate the original
arguments before passing them to NeMo Relay. Use math.isfinite for numeric
fields and check nested values when your schema allows them. Do not rely on a
Relay guardrail to detect the original non-finite Python value. If the value
must be represented, encode it explicitly in your application schema, such as
{"kind": "non_finite", "value": "nan"}, before calling Relay.
Recommended Strategies
These strategies keep provider and framework data JSON-compatible before it reaches NeMo Relay.
- Convert provider payloads into a stable request shape before NeMo Relay sees them.
- Preserve opaque framework objects outside the middleware path and pass only the serializable projection into the runtime.
- Store external object references in framework-owned maps keyed by request IDs, not inside NeMo Relay event payloads.
- Use typed wrappers for your application boundary, then serialize at the last responsible moment.
- Keep
ToolExecutionResult.annotationJSON-compatible; store opaque framework objects outside both the business result and its annotation.
Concrete Projection Pattern
Keep framework objects in your own map, but send only the JSON projection through NeMo Relay.
Python
Node.js
Rust
Codec Pattern For Provider Payloads
Use an LLM codec when the framework payload is structurally different from the annotated request model you want intercepts to reason about.
- Decode the provider payload into a normalized annotated request.
- Let request intercepts edit the normalized shape.
- Encode the annotated request back to the provider payload before the real call.
- Keep sockets, streams, callbacks, and SDK objects outside the codec result.
What Not to Do
Avoid these patterns because they make runtime payloads difficult to serialize or observe.
- Do not place client instances, callbacks, streams, sockets, or open file handles inside JSON event payloads.
- Do not rely on Python or JavaScript object identity surviving the middleware boundary.
- Do not leak framework-internal classes into a plugin config document.
Practical Workarounds
Use these workarounds when framework data cannot be passed directly through NeMo Relay.
- Replace large objects with IDs and look them up later.
- Emit summarized metadata instead of full request bodies.
- Use request codecs to normalize provider payloads.
- Use manual lifecycle APIs when the framework does not expose a clean execution wrapper.
Common Failure Cases
These failure cases are common signs that non-serializable data crossed the runtime boundary.
- A request intercept tries to return a framework SDK object instead of JSON.
- A plugin config stores a callable, client instance, or file handle.
- A subscriber assumes Python or JavaScript object identity survives event serialization.
- A worker thread receives a scope UUID but not the corresponding framework-owned object lookup table.
Validation Checklist
Use this checklist to confirm the implementation preserves the expected runtime contract.
- Middleware receives only JSON-compatible values.
- Tool execution results and optional annotations are JSON-compatible.
- The framework callback can still resolve the original SDK client or stream by ID.
- Subscribers and exporters receive enough metadata to debug the call.
- Cleanup removes object-map entries after the request finishes.
- Redaction happens before payloads reach production subscribers.
Next Steps
Use these links to continue from this workflow into the next related task.
- Wrap the framework boundary with Wrap Tool Calls or Wrap LLM Calls.
- Use Provider Codecs when provider payloads need normalized request or response annotations.
- Use the typed value codec examples in Using Codecs for structured conversion helpers.
- Use Add Middleware before adding request transforms.