Provider Codecs
Use this guide when a framework integration needs NeMo Relay middleware, intercepts, or subscribers to reason about provider-specific LLM payloads through a stable annotated shape.
What You Build
You will attach request and response codecs to a managed LLM wrapper so that:
- Request intercepts can work with normalized messages, model names, tools, generation parameters, and provider-specific extras
- The provider callback still receives the provider payload that the framework expects
- Response subscribers can receive normalized response annotations without changing the caller-visible provider response
Before You Start
You need:
- A framework LLM boundary that can call
llm.execute,llm.stream_execute,llmCallExecute,typedLlmExecute, orllm_call_execute. - A provider payload that is JSON-compatible.
- A matching built-in provider codec, or a custom codec that can preserve unmodeled provider fields.
- Request intercepts or subscribers that benefit from normalized request or response data.
What Provider Codecs Are
A provider codec is a pure data translator at the NeMo Relay LLM boundary.
- An LLM request codec converts a raw provider request into a normalized annotated request, then encodes any annotated edits back into the original provider request.
- An LLM response codec converts a raw provider response into a normalized response annotation for lifecycle events.
Provider codecs let framework code keep using provider-native payloads while NeMo Relay middleware works against a shared annotated model. For application-facing type conversion, use Using Codecs.
How Provider Codecs Work
When a managed LLM call has a request codec:
- NeMo Relay calls
decodebefore LLM request intercepts run. - Request intercepts receive both the raw request and the annotated request.
- Intercepts may edit the raw request, the annotated request, or both.
- NeMo Relay calls
encodeto merge the annotated request back into the original raw request. - Execution intercepts and the provider callback receive the encoded provider request.
When a managed LLM call has a response codec, NeMo Relay decodes the raw provider response for observability and attaches the result to the emitted LLM end event. The response codec does not rewrite the value returned to the application. Use Provider Response Codecs for response-only behavior and custom response codec examples.
Codec implementations should preserve fields they do not understand. Treat encode as a merge operation over the original provider payload, not as a full replacement.
Built-in Provider Codecs
Use the built-in provider codecs when the framework payload already matches a supported provider API:
OpenAIChatCodec: OpenAI Chat Completions-compatible requests and responses.OpenAIResponsesCodec: OpenAI Responses-compatible requests and responses.AnthropicMessagesCodec: Anthropic Messages-compatible requests and responses.
Provider Codec Roles
Provider codecs have separate request and response roles:
LlmCodecdecodes provider-specific requests into an annotated request form and encodes edits back into the provider request.LlmResponseCodecdecodes raw provider responses into annotated response data for lifecycle events.
The built-in provider codecs expose the same core methods:
Choose the provider codec that matches the payload shape the framework already sends to the provider. Do not translate to a different provider shape only to make the codec fit.
Example: Add a System Message with a Provider Codec
This example uses a request intercept to edit the normalized request. The codec writes the edited messages back into the provider payload before the provider callback runs.
Python
Node.js
Rust
Example: Write a Custom Framework Codec
Use a custom codec when a framework uses a payload shape that does not directly match a built-in provider format. The codec decodes the framework shape into AnnotatedLLMRequest, and encodes edits back into the framework shape.
Python
Node.js
Validation Checklist
Use this checklist to confirm the implementation preserves the expected runtime contract.
- Intercepts receive
annotatedonly when the managed call supplies a request codec. encodepreserves provider fields that the annotated model does not represent.- Response codecs are used only for event annotations, not caller-visible response rewriting.
- Codec implementations are pure data transforms and do not perform provider I/O.
- Framework-owned clients, sockets, streams, callbacks, and file handles stay outside codec results.
Next Steps
Use these links to continue from this workflow into the next related task.
- Use Using Codecs for typed value codecs.
- Use Provider Response Codecs for response-only annotations and subscriber examples.
- Use Add Middleware before adding request transforms.
- Use Handle Non-Serializable Data when the framework boundary includes SDK objects or streams that cannot pass through JSON payloads.