Skip to content

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

Rust

fix(llm): fall back to the tolerant parser on a 501 structured-output rejection

Igor Sant'Ana1f4a9b6b6755
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f1b570ab..0a24ec75 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -47,7 +47,10 @@ version in [`docs/changelog/`](docs/changelog/):
`agent_id`, which the router treats like an OpenCode child session.
`drop_subagent_captures` now covers OMP subagents too, and an advisor's tool
calls stay with the session they advise (#1779).
- a492ec23 (docs(config): map every toml key into template and properties doc)
+- The openai-compat strict path now treats an engine 501 structured-output
+ rejection as a capability gap and falls back to the tolerant parser. It used
+ to return the 501 as a transient failure, so the bootstrap chunk retry loop
+ repeated a call that failed the same way each time.
## [3.2.9] - 2026-10-27
diff --git a/crates/ai-memory-llm/src/openai_compat.rs b/crates/ai-memory-llm/src/openai_compat.rs
index cf5791ba..f121f1dc 100644
--- a/crates/ai-memory-llm/src/openai_compat.rs
+++ b/crates/ai-memory-llm/src/openai_compat.rs
@@ -380,13 +380,15 @@ fn is_parse_shape_error(err: &LlmError) -> bool {
}
/// True only when a compatibility endpoint rejected structured-output syntax
-/// before generation. Generic 4xx errors are deliberately excluded so auth,
-/// quota, model, and malformed-request failures retain their original cause.
+/// before generation. 400/422 are the usual rejection; 501 Not Implemented is
+/// an engine declaring the capability missing (`structured output is
+/// unavailable`). Generic errors are deliberately excluded so auth, quota,
+/// model, and malformed-request failures retain their original cause.
fn is_response_format_rejection(err: &LlmError) -> bool {
let LlmError::Provider { status, body, .. } = err else {
return false;
};
- if !matches!(status, 400 | 422) {
+ if !matches!(status, 400 | 422 | 501) {
return false;
}
let body = body.to_ascii_lowercase();
@@ -662,6 +664,17 @@ mod tests {
body: "json_schema is not supported".into(),
retry_after: None,
}));
+ // Production shape: an engine without structured output answers 501.
+ assert!(is_response_format_rejection(&LlmError::Provider {
+ status: 501,
+ body: r#"{"error":{"message":"structured output is unavailable","type":"api_error","param":null,"code":null}}"#.into(),
+ retry_after: None,
+ }));
+ assert!(!is_response_format_rejection(&LlmError::Provider {
+ status: 501,
+ body: "not implemented".into(),
+ retry_after: None,
+ }));
assert!(!is_response_format_rejection(&LlmError::Provider {
status: 400,
body: "unknown model".into(),
diff --git a/crates/ai-memory-llm/tests/suite/openai_compat_guardrails.rs b/crates/ai-memory-llm/tests/suite/openai_compat_guardrails.rs
index f1cccfc5..1862919d 100644
--- a/crates/ai-memory-llm/tests/suite/openai_compat_guardrails.rs
+++ b/crates/ai-memory-llm/tests/suite/openai_compat_guardrails.rs
@@ -20,7 +20,7 @@ use ai_memory_llm::{LlmError, LlmProvider, OpenAiCompatProvider, OpenAiProvider}
use secrecy::SecretString;
use serde_json::json;
use wiremock::matchers::{method, path};
-use wiremock::{Mock, MockServer, ResponseTemplate};
+use wiremock::{Mock, MockServer, Request, ResponseTemplate};
const QWEN_MODEL: &str = "applianceai01/qwen3.8-27b";
@@ -200,6 +200,46 @@ async fn strict_truncated_response_is_terminal_without_fallback() {
);
}
+/// Production shape: an engine serving a model without structured output
+/// answers HTTP 501 `structured output is unavailable`. That is a capability
+/// gap, so the strict path falls back to the tolerant parser once instead of
+/// surfacing a transient 5xx that callers would retry identically.
+#[tokio::test]
+async fn strict_501_structured_output_unavailable_falls_back_to_tolerant_parser() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/v1/chat/completions"))
+ .respond_with(|req: &Request| {
+ let request: serde_json::Value =
+ serde_json::from_slice(&req.body).expect("request body is JSON");
+ if request.get("response_format").is_some() {
+ ResponseTemplate::new(501).set_body_json(json!({
+ "error": {
+ "message": "structured output is unavailable",
+ "type": "api_error",
+ "param": null,
+ "code": null
+ }
+ }))
+ } else {
+ ResponseTemplate::new(200)
+ .set_body_json(qwen_body(Some("{\"findings\": []}"), "stop", 5))
+ }
+ })
+ .mount(&server)
+ .await;
+
+ let provider = tolerant_provider(server.uri()).with_strict(true);
+ let value = provider
+ .complete_structured_raw(tiny_request(), tiny_schema())
+ .await
+ .expect("a 501 structured-output rejection must use the tolerant fallback");
+
+ assert_eq!(value, json!({ "findings": [] }));
+ let requests = server.received_requests().await.expect("requests recorded");
+ assert_eq!(requests.len(), 2, "strict call, then exactly one tolerant call");
+}
+
/// The guard lives in the shared OpenAI-wire structured path, so the
/// `Official` dialect (api.openai.com shape) classifies the same way.
#[tokio::test]