Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(openai): force complete required + cap max_tokens per family

Pablo Winter0ffe56be39cc
diff --git a/crates/ai-memory-llm/src/openai.rs b/crates/ai-memory-llm/src/openai.rs
index d3bbb80a..db1c1e78 100644
--- a/crates/ai-memory-llm/src/openai.rs
+++ b/crates/ai-memory-llm/src/openai.rs
@@ -204,11 +204,12 @@ impl OpenAiProvider {
content: &m.content,
});
}
+ let capped = request.max_tokens.min(max_output_tokens_for(&self.model));
let (max_tokens, max_completion_tokens) =
if model_requires_max_completion_tokens(&self.model) {
- (None, Some(request.max_tokens))
+ (None, Some(capped))
} else {
- (Some(request.max_tokens), None)
+ (Some(capped), None)
};
OpenAiRequest {
model: &self.model,
@@ -260,14 +261,24 @@ impl OpenAiProvider {
}
}
-/// Recursively enforce `additionalProperties: false` on every object node of
-/// a JSON schema. Required by OpenAI Structured Outputs (`strict: true`):
-/// without this, the API rejects the request with
-/// `'additionalProperties' is required to be supplied and to be false`.
+/// Recursively normalise a JSON schema for OpenAI Structured Outputs
+/// (`strict: true`). The API rejects schemas missing either:
+///
+/// 1. `additionalProperties: false` on every object node — without it:
+/// `'additionalProperties' is required to be supplied and to be false`.
+///
+/// 2. `required` listing **every** key in `properties` (strict mode does
+/// not support optional fields; callers that need optionality express
+/// it via a nullable type instead, e.g. `["string", "null"]`). Without
+/// a complete `required` array: `'required' is required to be supplied
+/// and to be an array including every key in properties`.
///
/// Callers can hand us schemas authored elsewhere (or generated by
-/// macros) that don't bother — this normalisation hides the constraint
-/// from the rest of the codebase.
+/// `schemars`, which marks `#[serde(default)]` fields as non-required)
+/// that don't bother — this normalisation hides both constraints from
+/// the rest of the codebase. Caller-set values are preserved: if a
+/// caller deliberately set `additionalProperties: true` or supplied a
+/// trimmed `required`, we don't second-guess them.
fn enforce_strict_object_schemas(value: &mut serde_json::Value) {
match value {
serde_json::Value::Object(map) => {
@@ -276,8 +287,21 @@ fn enforce_strict_object_schemas(value: &mut serde_json::Value) {
.and_then(|t| t.as_str())
.is_some_and(|t| t == "object")
|| map.contains_key("properties");
- if is_object && !map.contains_key("additionalProperties") {
- map.insert("additionalProperties".to_string(), serde_json::json!(false));
+ if is_object {
+ if !map.contains_key("additionalProperties") {
+ map.insert("additionalProperties".to_string(), serde_json::json!(false));
+ }
+ // OpenAI strict mode rejects ANY incomplete `required` —
+ // even an explicit subset. The only way to express
+ // optionality is via a nullable type at the value site
+ // (e.g. `["string", "null"]`). Overwrite unconditionally
+ // when `properties` is present so a caller-supplied
+ // partial list doesn't sneak through.
+ if let Some(props) = map.get("properties").and_then(|p| p.as_object()) {
+ let keys: Vec<serde_json::Value> =
+ props.keys().map(|k| serde_json::json!(k)).collect();
+ map.insert("required".to_string(), serde_json::Value::Array(keys));
+ }
}
for (_, v) in map.iter_mut() {
enforce_strict_object_schemas(v);
@@ -302,6 +326,29 @@ fn model_requires_max_completion_tokens(model: &str) -> bool {
m.starts_with("gpt-5") || m.starts_with("o1") || m.starts_with("o3") || m.starts_with("o4")
}
+/// Per-model output-token ceiling.
+///
+/// OpenAI clamps requests above the model's published limit by returning
+/// `400 max_tokens is too large`, instead of silently truncating. Callers
+/// (e.g. bootstrap) deliberately ask for very large budgets (64K) so
+/// Anthropic / Haiku-class models don't truncate mid-JSON; the same
+/// request blows up on gpt-4o-mini (cap 16384) without this defensive
+/// cap. Reasoning models in the gpt-5 / o-series have much larger caps,
+/// so we leave their requests untouched.
+fn max_output_tokens_for(model: &str) -> u32 {
+ if model_requires_max_completion_tokens(model) {
+ // gpt-5 / o-series: documented at 128K output. Leave the
+ // caller's value alone — they know what they're asking for.
+ u32::MAX
+ } else {
+ // gpt-4o family, gpt-4-turbo, gpt-3.5: the conservative 16384
+ // is the published cap of gpt-4o / gpt-4o-mini. gpt-4-turbo
+ // is even smaller (4096) but rejects with the same 400, so
+ // the user gets a clear "lower max_tokens" signal there too.
+ 16_384
+ }
+}
+
#[cfg(test)]
mod tests {
use super::{
@@ -369,6 +416,46 @@ mod tests {
);
}
+ #[test]
+ fn enforce_strict_fills_required_with_all_property_keys() {
+ let mut schema = json!({
+ "type": "object",
+ "properties": {
+ "title": { "type": "string" },
+ "body": { "type": "string" },
+ "tags": { "type": "array", "items": { "type": "string" } }
+ }
+ });
+ enforce_strict_object_schemas(&mut schema);
+ let required = schema["required"].as_array().expect("required is array");
+ let names: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
+ assert!(names.contains(&"title"));
+ assert!(names.contains(&"body"));
+ assert!(names.contains(&"tags"));
+ assert_eq!(names.len(), 3);
+ }
+
+ #[test]
+ fn enforce_strict_overwrites_incomplete_required() {
+ // OpenAI strict mode rejects partial `required` arrays — even an
+ // explicit subset from the caller. Optionality at the value site
+ // (nullable union types) is the only supported escape hatch.
+ let mut schema = json!({
+ "type": "object",
+ "properties": {
+ "a": { "type": "string" },
+ "b": { "type": "string" }
+ },
+ "required": ["a"]
+ });
+ enforce_strict_object_schemas(&mut schema);
+ let required = schema["required"].as_array().expect("required is array");
+ let names: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
+ assert!(names.contains(&"a"));
+ assert!(names.contains(&"b"));
+ assert_eq!(names.len(), 2);
+ }
+
#[test]
fn enforce_strict_preserves_existing_additional_properties() {
let mut schema = json!({
@@ -430,6 +517,46 @@ mod tests {
assert!(json.get("max_tokens").is_none());
}
+ #[test]
+ fn build_request_caps_huge_max_tokens_on_gpt4o() {
+ // Bootstrap requests 64K output to avoid mid-JSON truncation on
+ // Anthropic Haiku-class models. OpenAI gpt-4o family caps at
+ // 16384 and rejects above; cap silently so the caller doesn't
+ // need to know per-model limits.
+ let p = provider_for("gpt-4o-mini");
+ let req_input = ChatRequest {
+ system: None,
+ messages: vec![ChatMessage {
+ role: Role::User,
+ content: "x".into(),
+ }],
+ max_tokens: 64_000,
+ temperature: None,
+ };
+ let req = p.build_request(&req_input, None);
+ let json = serde_json::to_value(&req).unwrap();
+ assert_eq!(json["max_tokens"], json!(16_384));
+ }
+
+ #[test]
+ fn build_request_does_not_cap_gpt5() {
+ // Reasoning models have a much larger output cap (128K+); leave
+ // the caller's value alone.
+ let p = provider_for("gpt-5.4-nano");
+ let req_input = ChatRequest {
+ system: None,
+ messages: vec![ChatMessage {
+ role: Role::User,
+ content: "x".into(),
+ }],
+ max_tokens: 64_000,
+ temperature: None,
+ };
+ let req = p.build_request(&req_input, None);
+ let json = serde_json::to_value(&req).unwrap();
+ assert_eq!(json["max_completion_tokens"], json!(64_000));
+ }
+
#[test]
fn normalize_openai_base_chat_completions() {
let ep = "chat/completions";