Skip to content

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

Rust

fix(llm): strip unsupported numeric schema formats

AkitaOnRailscb0b4627fe8e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01c8a23c..b27e967f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,10 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- Removed numeric `format` annotations from strict OpenAI-shaped JSON Schemas,
+ including nullable number and integer fields, while preserving string
+ formats. OpenCode Go Muse rejected schemars' `float` hint before running the
+ model, so every auto-improvement review failed with HTTP 400 (#1901).
- Appended the target JSON Schema to the system prompt during `openai-compat`
tolerant structured completion fallback and direct tolerant mode, preventing
models from omitting top-level object wrappers when `response_format` is
diff --git a/crates/ai-memory-llm/src/openai.rs b/crates/ai-memory-llm/src/openai.rs
index 2b867795..5100d242 100644
--- a/crates/ai-memory-llm/src/openai.rs
+++ b/crates/ai-memory-llm/src/openai.rs
@@ -662,9 +662,13 @@ pub(crate) fn is_length_truncated(finish_reason: Option<&str>) -> bool {
/// a complete `required` array: `'required' is required to be supplied
/// and to be an array including every key in properties`.
///
-/// Both rules are unconditional here: this normalisation only runs on
-/// the `Official` request dialect, which targets `api.openai.com`
-/// where strict mode is mandatory. Any caller-supplied
+/// Numeric `format` annotations are also removed. OpenAI-compatible strict
+/// endpoints commonly reject schemars' `float`, `double`, `int32`, and
+/// `int64` hints even though they accept the underlying `number` or `integer`.
+/// String formats such as `date-time` remain intact.
+///
+/// The object rules are unconditional here: every caller of this helper emits
+/// an OpenAI-shaped strict schema. Any caller-supplied
/// `additionalProperties: true` or trimmed `required` array is
/// overwritten — preserving them would let invalid schemas through
/// and re-introduce the 400 this function exists to prevent. Callers
@@ -694,6 +698,18 @@ pub(crate) fn enforce_strict_object_schemas(value: &mut serde_json::Value) {
if let Some(one_of) = map.remove("oneOf") {
map.insert("anyOf".to_string(), one_of);
}
+ let numeric = map.get("type").is_some_and(|schema_type| {
+ let is_numeric = |value: &serde_json::Value| {
+ matches!(value.as_str(), Some("number" | "integer"))
+ };
+ is_numeric(schema_type)
+ || schema_type
+ .as_array()
+ .is_some_and(|types| types.iter().any(is_numeric))
+ });
+ if numeric {
+ map.remove("format");
+ }
let is_object = map
.get("type")
.and_then(|t| t.as_str())
@@ -911,6 +927,26 @@ mod tests {
);
}
+ #[test]
+ fn enforce_strict_removes_numeric_formats_but_keeps_string_formats() {
+ let mut schema = json!({
+ "type": "object",
+ "properties": {
+ "confidence": { "type": "number", "format": "float" },
+ "count": { "type": ["integer", "null"], "format": "int64" },
+ "created_at": { "type": "string", "format": "date-time" }
+ }
+ });
+ enforce_strict_object_schemas(&mut schema);
+
+ assert!(schema["properties"]["confidence"].get("format").is_none());
+ assert!(schema["properties"]["count"].get("format").is_none());
+ assert_eq!(
+ schema["properties"]["created_at"]["format"],
+ json!("date-time")
+ );
+ }
+
#[test]
fn enforce_strict_fills_required_with_all_property_keys() {
let mut schema = json!({
diff --git a/crates/ai-memory-llm/src/opencode.rs b/crates/ai-memory-llm/src/opencode.rs
index c2be4d14..5d748844 100644
--- a/crates/ai-memory-llm/src/opencode.rs
+++ b/crates/ai-memory-llm/src/opencode.rs
@@ -1288,8 +1288,12 @@ mod tests {
ChatRequest::user_prompt("emit JSON"),
json!({
"type": "object",
- "properties": { "ok": { "type": "boolean" } },
- "required": ["ok"],
+ "properties": {
+ "ok": { "type": "boolean" },
+ "confidence": { "type": "number", "format": "float" },
+ "created_at": { "type": "string", "format": "date-time" }
+ },
+ "required": ["ok", "confidence", "created_at"],
}),
)
.await
@@ -1300,6 +1304,15 @@ mod tests {
let body: serde_json::Value = serde_json::from_slice(&requests[0].body).unwrap();
assert_eq!(body["text"]["format"]["type"], "json_schema");
assert_eq!(body["text"]["format"]["schema"]["type"], "object");
+ assert!(
+ body["text"]["format"]["schema"]["properties"]["confidence"]
+ .get("format")
+ .is_none()
+ );
+ assert_eq!(
+ body["text"]["format"]["schema"]["properties"]["created_at"]["format"],
+ "date-time"
+ );
}
#[tokio::test]