> This page is for NeMo Relay, version v0.11.0.
> For other versions, use one of these documentation indexes:
> - Latest (v0.10.4) (default): https://docs.nvidia.com/nemo/relay/latest/llms.txt
> - dev: https://docs.nvidia.com/nemo/relay/dev/llms.txt
> - v0.11.0: https://docs.nvidia.com/nemo/relay/v0.11.0/llms.txt
> - v0.10.4: https://docs.nvidia.com/nemo/relay/v0.10.4/llms.txt
> - v0.9.4: https://docs.nvidia.com/nemo/relay/v0.9.4/llms.txt
> - v0.8.4: https://docs.nvidia.com/nemo/relay/v0.8.4/llms.txt
> - v0.7.3: https://docs.nvidia.com/nemo/relay/v0.7.3/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/relay/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/relay/_mcp/server.

# Trait LlmCodec

> A bidirectional translator between opaque `LlmRequest` content and structured `AnnotatedLlmRequest`.

Generated from `cargo doc --no-deps -p nemo-relay -p nemo-relay-adaptive -p nemo-relay-pii-redaction -p nemo-relay-ffi -p nemo-relay-types -p nemo-relay-plugin -p nemo-relay-worker-proto -p nemo-relay-worker`.

<pre />

A bidirectional translator between opaque [`LlmRequest`](/reference/api/rust-library-reference/nemo-relay/api/llm/struct-llmrequest) content and structured [`AnnotatedLlmRequest`](/reference/api/rust-library-reference/nemo-relay/codec/request/struct-annotatedllmrequest).

Codecs are implemented by framework integrations and provider adapters because each SDK has its own request format. A codec is supplied per call by the caller; the built-in provider codecs can also be selected from a raw payload via [`crate::codec::resolve`](/reference/api/rust-library-reference/nemo-relay/codec/resolve).

## Design

* **Synchronous**: `decode`/`encode` are pure data transforms (JSON restructuring), not I/O operations. This matches existing guardrails and request intercepts.
* **`Send + Sync`**: Required because [`NemoRelayContextState`](/reference/api/rust-library-reference/nemo-relay/api/runtime/state/struct-nemorelaycontextstate) is behind `Arc<RwLock<>>` and accessed from async contexts.
* **Trait object**: Codecs are registered at runtime by callers or bindings, so the Rust core cannot know concrete types at compile time. Store as `Arc<dyn LlmCodec>`.

## Required Methods

#### `decode`

<pre />

Parse opaque request content into structured form.

#### `encode`

<pre />

Merge structured changes back into the opaque request.

The `original` parameter is the pre-intercept [`LlmRequest`](/reference/api/rust-library-reference/nemo-relay/api/llm/struct-llmrequest), used to preserve fields that the Codec does not structurally model. Implementations MUST use merge-not-replace semantics: overlay structured changes onto the original content, do not construct a fresh content object.

## Provided Methods

#### `codec_identity`

<pre />

Return this codec's identity for LLM sanitizer context.

Custom codecs should keep the default [`LlmCodecIdentity::Opaque`](/reference/api/rust-library-reference/nemo-relay/api/runtime/enum-llmcodecidentity) unless they have a stable runtime registration ID. Callers must not infer a provider surface from an opaque implementation or request shape.

#### `encode_with_baseline`

<pre />

Merge changes using the annotation decoded from `original.content`.

Use a baseline from this codec and the same raw request body. Do not change the baseline or that body after decoding. Request headers can change while middleware runs. The runtime supplies this baseline to avoid decoding twice. The default calls [`Self::encode`](/reference/api/rust-library-reference/nemo-relay/codec/traits/trait-llmcodec), so existing custom codecs still work.

#### `encode_owned_with_baseline`

<pre />

Encode a request that the caller gives to this codec.

The codec can move the request body or keep it for later use. The baseline must match the original request body. If both header maps have the same key, keep the value from the original request. The default calls [`Self::encode_with_baseline`](/reference/api/rust-library-reference/nemo-relay/codec/traits/trait-llmcodec) and then moves the original headers into the result.

## Implementors

### `impl LlmCodec for AnthropicMessagesCodec`

<pre />

### `impl LlmCodec for GeminiGenerateContentCodec`

<pre />

### `impl LlmCodec for OCIGenAIChatCodec`

<pre />

### `impl LlmCodec for OpenAIChatCodec`

<pre />

### `impl LlmCodec for OpenAIResponsesCodec`

<pre />