Filter out reasoning items from previous turns (#5857)

Reduces request size and prevents 400 errors when switching between API
orgs.

Based on Responses API behavior described in
https://cookbook.openai.com/examples/responses_api/reasoning_items#caching
This commit is contained in:
pakrym-oai
2025-10-28 11:39:34 -07:00
committed by GitHub
parent 65107d24a2
commit 1b8f2543ac
6 changed files with 176 additions and 29 deletions

View File

@@ -67,6 +67,15 @@ impl ConversationHistory {
self.contents()
}
// Returns the history prepared for sending to the model.
// With extra response items filtered out and GhostCommits removed.
pub(crate) fn get_history_for_prompt(&mut self) -> Vec<ResponseItem> {
let mut history = self.get_history();
Self::remove_ghost_snapshots(&mut history);
Self::remove_reasoning_before_last_turn(&mut history);
history
}
pub(crate) fn remove_first_item(&mut self) {
if !self.items.is_empty() {
// Remove the oldest item (front of the list). Items are ordered from
@@ -111,6 +120,29 @@ impl ConversationHistory {
self.items.clone()
}
fn remove_ghost_snapshots(items: &mut Vec<ResponseItem>) {
items.retain(|item| !matches!(item, ResponseItem::GhostSnapshot { .. }));
}
fn remove_reasoning_before_last_turn(items: &mut Vec<ResponseItem>) {
// Responses API drops reasoning items before the last user message.
// Sending them is harmless but can lead to validation errors when switching between API organizations.
// https://cookbook.openai.com/examples/responses_api/reasoning_items#caching
let Some(last_user_index) = items
.iter()
// Use last user message as the turn boundary.
.rposition(|item| matches!(item, ResponseItem::Message { role, .. } if role == "user"))
else {
return;
};
let mut index = 0usize;
items.retain(|item| {
let keep = index >= last_user_index || !matches!(item, ResponseItem::Reasoning { .. });
index += 1;
keep
});
}
fn ensure_call_outputs_present(&mut self) {
// Collect synthetic outputs to insert immediately after their calls.
// Store the insertion position (index of call) alongside the item so
@@ -498,6 +530,7 @@ fn is_api_message(message: &ResponseItem) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use codex_git_tooling::GhostCommit;
use codex_protocol::models::ContentItem;
use codex_protocol::models::FunctionCallOutputPayload;
use codex_protocol::models::LocalShellAction;
@@ -515,6 +548,15 @@ mod tests {
}
}
fn reasoning(id: &str) -> ResponseItem {
ResponseItem::Reasoning {
id: id.to_string(),
summary: Vec::new(),
content: None,
encrypted_content: None,
}
}
fn create_history_with_items(items: Vec<ResponseItem>) -> ConversationHistory {
let mut h = ConversationHistory::new();
h.record_items(items.iter());
@@ -571,6 +613,50 @@ mod tests {
);
}
#[test]
fn get_history_drops_reasoning_before_last_user_message() {
let mut history = ConversationHistory::new();
let items = vec![
user_msg("initial"),
reasoning("first"),
assistant_msg("ack"),
user_msg("latest"),
reasoning("second"),
assistant_msg("ack"),
reasoning("third"),
];
history.record_items(items.iter());
let filtered = history.get_history_for_prompt();
assert_eq!(
filtered,
vec![
user_msg("initial"),
assistant_msg("ack"),
user_msg("latest"),
reasoning("second"),
assistant_msg("ack"),
reasoning("third"),
]
);
let reasoning_count = history
.contents()
.iter()
.filter(|item| matches!(item, ResponseItem::Reasoning { .. }))
.count();
assert_eq!(reasoning_count, 3);
}
#[test]
fn get_history_for_prompt_drops_ghost_commits() {
let items = vec![ResponseItem::GhostSnapshot {
ghost_commit: GhostCommit::new("ghost-1".to_string(), None, Vec::new(), Vec::new()),
}];
let mut history = create_history_with_items(items);
let filtered = history.get_history_for_prompt();
assert_eq!(filtered, vec![]);
}
#[test]
fn remove_first_item_removes_matching_output_for_function_call() {
let items = vec![