Support end_turn in response.completed (#19610)

Some providers of Responses API forward a model-defined `end_turn`
boolean indicating explicitly the model's indication of whether it would
like to end the turn or to be inferenced again. In this PR, we update
the sampling loop to use this field correctly if it's set. If the field
is not set by the provider, we fall back to the existing sampling logic.
This commit is contained in:
Andrey Mishchenko
2026-04-25 21:57:42 -07:00
committed by GitHub
Unverified
parent 5591912f0b
commit 355c40ad7e
7 changed files with 44 additions and 7 deletions
+20 -2
View File
@@ -78,8 +78,9 @@ fn response_event_to_json(event: codex_api::ResponseEvent) -> serde_json::Value
codex_api::ResponseEvent::Completed {
response_id,
token_usage,
end_turn,
} => {
let response = match token_usage {
let mut response = match token_usage {
Some(token_usage) => json!({
"id": response_id,
"usage": {
@@ -96,6 +97,9 @@ fn response_event_to_json(event: codex_api::ResponseEvent) -> serde_json::Value
}),
None => json!({ "id": response_id }),
};
if let Some(end_turn) = end_turn {
response["end_turn"] = json!(end_turn);
}
json!({ "type": "response.completed", "response": response })
}
codex_api::ResponseEvent::OutputTextDelta(delta) => {
@@ -165,6 +169,7 @@ mod tests {
reasoning_output_tokens: 3,
total_tokens: 17,
}),
end_turn: Some(true),
});
assert_eq!(
completed,
@@ -183,6 +188,7 @@ mod tests {
},
"total_tokens": 17,
},
"end_turn": true,
},
})
);
@@ -190,10 +196,22 @@ mod tests {
let completed_without_usage = response_event_to_json(codex_api::ResponseEvent::Completed {
response_id: "resp-2".to_string(),
token_usage: None,
end_turn: Some(false),
});
assert_eq!(
completed_without_usage,
json!({"type": "response.completed", "response": {"id": "resp-2"}})
json!({"type": "response.completed", "response": {"id": "resp-2", "end_turn": false}})
);
let completed_without_usage_or_end_turn =
response_event_to_json(codex_api::ResponseEvent::Completed {
response_id: "resp-3".to_string(),
token_usage: None,
end_turn: None,
});
assert_eq!(
completed_without_usage_or_end_turn,
json!({"type": "response.completed", "response": {"id": "resp-3"}})
);
}