Protocol for request codecs used by annotated LLM intercepts. ``decode()`` converts a provider-specific ``LLMRequest`` into an ``AnnotatedLLMRequest`` so request intercepts can work with a normalized structure. ``encode()`` merges any annotated edits back into the original raw paylo
| 58 | |
| 59 | @runtime_checkable |
| 60 | class LlmCodec(Protocol): |
| 61 | """Protocol for request codecs used by annotated LLM intercepts. |
| 62 | |
| 63 | ``decode()`` converts a provider-specific ``LLMRequest`` into an |
| 64 | ``AnnotatedLLMRequest`` so request intercepts can work with a normalized |
| 65 | structure. ``encode()`` merges any annotated edits back into the original |
| 66 | raw payload before the provider callback is invoked. |
| 67 | |
| 68 | Notes: |
| 69 | ``encode()`` should preserve unknown provider-specific fields whenever |
| 70 | possible instead of rebuilding the payload from scratch. That keeps |
| 71 | transport-specific settings intact even when an intercept edits the |
| 72 | normalized representation. |
| 73 | |
| 74 | Example:: |
| 75 | |
| 76 | from nemo_relay import AnnotatedLLMRequest, LLMRequest |
| 77 | from nemo_relay.codecs import LlmCodec |
| 78 | |
| 79 | class DemoCodec(LlmCodec): |
| 80 | def decode(self, request: LLMRequest) -> AnnotatedLLMRequest: |
| 81 | return AnnotatedLLMRequest( |
| 82 | request.content.get("messages", []), |
| 83 | model=request.content.get("model"), |
| 84 | ) |
| 85 | |
| 86 | def encode( |
| 87 | self, |
| 88 | annotated: AnnotatedLLMRequest, |
| 89 | original: LLMRequest, |
| 90 | ) -> LLMRequest: |
| 91 | content = {**original.content, "messages": annotated.messages} |
| 92 | if annotated.model is not None: |
| 93 | content["model"] = annotated.model |
| 94 | return LLMRequest(original.headers, content) |
| 95 | """ |
| 96 | |
| 97 | def decode(self, request: LLMRequest) -> AnnotatedLLMRequest: |
| 98 | """Decode a raw provider request into ``AnnotatedLLMRequest``. |
| 99 | |
| 100 | Args: |
| 101 | request: The provider-specific request payload received by |
| 102 | ``nemo_relay.llm.execute()`` or ``nemo_relay.llm.stream_execute()``. |
| 103 | |
| 104 | Returns: |
| 105 | AnnotatedLLMRequest: The normalized request consumed by annotated |
| 106 | intercepts. |
| 107 | """ |
| 108 | ... |
| 109 | |
| 110 | def encode(self, annotated: AnnotatedLLMRequest, original: LLMRequest) -> LLMRequest: |
| 111 | """Merge annotated edits back into the original raw request. |
| 112 | |
| 113 | Args: |
| 114 | annotated: The normalized request after intercepts have applied any |
| 115 | edits. |
| 116 | original: The original provider-specific request passed into the |
| 117 | runtime before normalization. |
nothing calls this directly
no outgoing calls
no test coverage detected