-
Notifications
You must be signed in to change notification settings - Fork 185
fix: sanitize Responses reasoning across backend handoffs #483
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
Open
srchandrupatla
wants to merge
5
commits into
NVIDIA-NeMo:main
Choose a base branch
from
srchandrupatla:fix/responses-reasoning-handoff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30c648c
fix: sanitize unsigned Responses reasoning handoffs
srchandrupatla 1fd39ec
fix: preserve reasoning content arrays across handoffs
srchandrupatla ab1def4
fix: drop encrypted reasoning for local backends
srchandrupatla ab9b9e8
refactor: configure Responses reasoning replay explicitly
srchandrupatla 0971862
refactor: preserve backend configuration compatibility
srchandrupatla 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,116 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Target-specific replay policy for OpenAI Responses reasoning items. | ||
|
|
||
| use serde::Deserialize; | ||
| use serde_json::Value; | ||
|
|
||
| /// Controls which Responses reasoning items are replayed to an upstream. | ||
| #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq)] | ||
| #[serde(rename_all = "snake_case")] | ||
| pub enum ResponsesReasoningPolicy { | ||
| /// Preserve provider-encrypted reasoning but remove plaintext reasoning. | ||
| /// | ||
| /// This is the safe default for strict hosted Responses providers. | ||
| #[default] | ||
| PreserveEncrypted, | ||
| /// Drop all reasoning items while preserving messages and tool-call history. | ||
| /// | ||
| /// Use this for local Responses-compatible servers that cannot consume | ||
| /// another provider's encrypted reasoning representation. | ||
| Drop, | ||
| } | ||
|
|
||
| impl ResponsesReasoningPolicy { | ||
| /// Normalizes a Responses request body for this replay policy. | ||
| pub(crate) fn normalize(self, body: &mut Value) { | ||
| let Some(Value::Array(input)) = body.get_mut("input") else { | ||
| return; | ||
| }; | ||
| input.retain_mut(|item| self.normalize_item(item)); | ||
| } | ||
|
|
||
| fn normalize_item(self, item: &mut Value) -> bool { | ||
| let Some(object) = item.as_object_mut() else { | ||
| return true; | ||
| }; | ||
| if object.get("type").and_then(Value::as_str) != Some("reasoning") { | ||
| return true; | ||
| } | ||
|
|
||
| let signed = matches!( | ||
| object.get("encrypted_content").and_then(Value::as_str), | ||
| Some(encrypted_content) if !encrypted_content.is_empty() | ||
| ); | ||
| if self == Self::PreserveEncrypted && signed { | ||
| object.insert("content".to_string(), Value::Array(Vec::new())); | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use serde_json::json; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn mixed_history() -> Value { | ||
| json!({ | ||
| "input": [ | ||
| {"type": "message", "role": "user", "content": []}, | ||
| { | ||
| "type": "reasoning", | ||
| "content": [{"type": "reasoning_text", "text": "plaintext"}], | ||
| "encrypted_content": "" | ||
| }, | ||
| {"type": "function_call", "call_id": "call_1"}, | ||
| {"type": "function_call_output", "call_id": "call_1", "output": "ok"}, | ||
| { | ||
| "type": "reasoning", | ||
| "content": [{"type": "reasoning_text", "text": "must be removed"}], | ||
| "encrypted_content": "encrypted" | ||
| } | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| #[test] | ||
| fn preserve_encrypted_drops_unsigned_and_clears_plaintext() { | ||
| let mut body = mixed_history(); | ||
| ResponsesReasoningPolicy::PreserveEncrypted.normalize(&mut body); | ||
|
|
||
| let input = body["input"].as_array().expect("input array"); | ||
| let reasoning: Vec<&Value> = input | ||
| .iter() | ||
| .filter(|item| item["type"] == "reasoning") | ||
| .collect(); | ||
| assert_eq!(reasoning.len(), 1); | ||
| assert_eq!(reasoning[0]["encrypted_content"], "encrypted"); | ||
| assert_eq!(reasoning[0]["content"], json!([])); | ||
| assert!(input.iter().any(|item| item["type"] == "function_call")); | ||
| assert!( | ||
| input | ||
| .iter() | ||
| .any(|item| item["type"] == "function_call_output") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn drop_removes_all_reasoning_and_keeps_tool_history() { | ||
| let mut body = mixed_history(); | ||
| ResponsesReasoningPolicy::Drop.normalize(&mut body); | ||
|
|
||
| let input = body["input"].as_array().expect("input array"); | ||
| assert!(input.iter().all(|item| item["type"] != "reasoning")); | ||
| assert!(input.iter().any(|item| item["type"] == "function_call")); | ||
| assert!( | ||
| input | ||
| .iter() | ||
| .any(|item| item["type"] == "function_call_output") | ||
| ); | ||
| } | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Document the reasoning-item classification.
Add a concise comment before
normalize_item. State that non-reasoning items remain unchanged, and that only non-emptyencrypted_contentis retained byPreserveEncrypted.As per coding guidelines, add concise comments for private helpers with non-obvious behavior.
🤖 Prompt for AI Agents
Source: Coding guidelines