-
Notifications
You must be signed in to change notification settings - Fork 30
refactor: hoist provider schema detection and normalization #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rapids-bot
merged 10 commits into
NVIDIA:main
from
zhongxuanwang-nv:refactor/hoist-provider-schema-handling
Jun 24, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3610a91
refactor: hoist provider schema handling for Responses, Messages, and…
zhongxuanwang-nv e64c06b
Merge branch 'main' into refactor/hoist-provider-schema-handling
zhongxuanwang-nv 3d93e03
fix: accept messages-only Anthropic requests in adaptive resolution
zhongxuanwang-nv 49717f2
perf: decode normalized response once per OpenInference end event
zhongxuanwang-nv c4e3511
refactor: drop unused provider-aware surface tie-break
zhongxuanwang-nv 1ffd131
Merge remote-tracking branch 'upstream/main' into refactor/hoist-prov…
zhongxuanwang-nv 66350cf
docs: remove redundant no-annotation section from response codecs
zhongxuanwang-nv a369cd4
Merge remote-tracking branch 'upstream/main' into refactor/hoist-prov…
zhongxuanwang-nv b200506
fix(codec): rename ProviderSurface OpenAI variants for consistency
zhongxuanwang-nv e307e8d
Merge branch 'main' into refactor/hoist-provider-schema-handling
zhongxuanwang-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Provider-surface detection and best-effort normalization: the preferred path | ||
| //! for turning raw provider JSON into normalized types when no codec annotation | ||
| //! is present. | ||
|
|
||
| use crate::api::llm::LlmRequest; | ||
| use crate::json::Json; | ||
|
|
||
| use super::anthropic::AnthropicMessagesCodec; | ||
| use super::openai_chat::OpenAIChatCodec; | ||
| use super::openai_responses::OpenAIResponsesCodec; | ||
| use super::request::AnnotatedLlmRequest; | ||
| use super::response::AnnotatedLlmResponse; | ||
| use super::traits::{LlmCodec, LlmResponseCodec}; | ||
|
|
||
| /// A built-in provider request/response surface. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum ProviderSurface { | ||
|
zhongxuanwang-nv marked this conversation as resolved.
|
||
| /// OpenAI Chat Completions. | ||
| OpenAIChat, | ||
| /// OpenAI Responses. | ||
| OpenAIResponses, | ||
| /// Anthropic Messages. | ||
| AnthropicMessages, | ||
| } | ||
|
|
||
| /// Detect the request surface from a raw request body by top-level key. | ||
| /// | ||
| /// Priority: OpenAI Responses (`input`/`instructions`) > Anthropic Messages | ||
| /// (`system`) > OpenAI Chat (`messages`). `None` when no key matches or `body` | ||
| /// is not an object. This is a best-effort heuristic: an Anthropic request that | ||
| /// omits the optional top-level `system` is indistinguishable from OpenAI Chat | ||
| /// and classifies as `OpenAIChat`. | ||
| #[must_use] | ||
| pub fn detect_request_surface(body: &Json) -> Option<ProviderSurface> { | ||
| let obj = body.as_object()?; | ||
| if obj.contains_key("input") || obj.contains_key("instructions") { | ||
|
zhongxuanwang-nv marked this conversation as resolved.
|
||
| Some(ProviderSurface::OpenAIResponses) | ||
| } else if obj.contains_key("system") { | ||
| Some(ProviderSurface::AnthropicMessages) | ||
| } else if obj.contains_key("messages") { | ||
| Some(ProviderSurface::OpenAIChat) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// Detect the response surface from a raw provider response, classifying only | ||
| /// when exactly one built-in shape matches (the built-in codecs accept minimal | ||
| /// objects, so decode success alone is not a reliable classifier). | ||
| #[must_use] | ||
| pub fn detect_response_surface(raw: &Json) -> Option<ProviderSurface> { | ||
| let obj = raw.as_object()?; | ||
| let is_chat = obj.get("choices").is_some_and(Json::is_array); | ||
| let is_responses = obj.get("output").is_some_and(Json::is_array) | ||
| || obj.get("output_text").is_some_and(Json::is_string); | ||
| let is_anthropic = obj.get("type").and_then(Json::as_str) == Some("message") | ||
| && obj.get("content").is_some_and(Json::is_array); | ||
|
|
||
| match (is_chat, is_responses, is_anthropic) { | ||
| (true, false, false) => Some(ProviderSurface::OpenAIChat), | ||
| (false, true, false) => Some(ProviderSurface::OpenAIResponses), | ||
| (false, false, true) => Some(ProviderSurface::AnthropicMessages), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| /// Best-effort decode of a raw request into [`AnnotatedLlmRequest`] (fail-open). | ||
| #[must_use] | ||
| pub fn normalize_request(request: &LlmRequest) -> Option<AnnotatedLlmRequest> { | ||
| match detect_request_surface(&request.content)? { | ||
| ProviderSurface::OpenAIChat => OpenAIChatCodec.decode(request), | ||
| ProviderSurface::OpenAIResponses => OpenAIResponsesCodec.decode(request), | ||
| ProviderSurface::AnthropicMessages => AnthropicMessagesCodec.decode(request), | ||
| } | ||
| .ok() | ||
| } | ||
|
|
||
| /// Best-effort decode of a raw response into [`AnnotatedLlmResponse`] (fail-open). | ||
| #[must_use] | ||
| pub fn normalize_response(raw: &Json) -> Option<AnnotatedLlmResponse> { | ||
| match detect_response_surface(raw)? { | ||
| ProviderSurface::OpenAIChat => OpenAIChatCodec.decode_response(raw), | ||
| ProviderSurface::OpenAIResponses => OpenAIResponsesCodec.decode_response(raw), | ||
| ProviderSurface::AnthropicMessages => AnthropicMessagesCodec.decode_response(raw), | ||
| } | ||
| .ok() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "../../tests/unit/codec/resolve_tests.rs"] | ||
| mod tests; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.