Skip to content

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

Rust

fix(llm): request structured OpenCode chat output (#1441)

AkitaOnRailsc5bd9aa239a1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b92f3a6..5c60054c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Sent `response_format=json_schema` for structured OpenCode Chat Completions
+ calls, with the existing tolerant fallback for models that reject it, so
+ `memory_query answer=true` no longer receives unstructured prose by default.
+ ([#1441](https://github.com/akitaonrails/ai-memory/issues/1441))
- Revalidated session-purge tombstones inside the reindex write transaction,
preventing a queued watcher update from recreating a deleted summary after
file cleanup failed. Kept surviving manual replacements at the same path
diff --git a/crates/ai-memory-llm/src/factory.rs b/crates/ai-memory-llm/src/factory.rs
index 91c59fc8..8d08a705 100644
--- a/crates/ai-memory-llm/src/factory.rs
+++ b/crates/ai-memory-llm/src/factory.rs
@@ -504,6 +504,10 @@ pub fn build_provider(config: ProviderConfig) -> LlmResult<Arc<dyn LlmProvider>>
}
Ok(Arc::new(
provider
+ // Go's Chat Completions catalogue supports JSON Schema;
+ // the wrapper falls back only when a model explicitly
+ // rejects the parameter or returns an unparsable shape.
+ .with_strict(true)
.with_timeout_secs(timeout)
.with_reasoning_effort(config.reasoning_effort)
.with_extra_headers(extra_headers),
diff --git a/crates/ai-memory-llm/src/opencode.rs b/crates/ai-memory-llm/src/opencode.rs
index 8fd1680e..413f73be 100644
--- a/crates/ai-memory-llm/src/opencode.rs
+++ b/crates/ai-memory-llm/src/opencode.rs
@@ -83,8 +83,7 @@ impl OpenCodeProvider {
})
}
- #[cfg(test)]
- fn with_strict(mut self, strict: bool) -> Self {
+ pub(crate) fn with_strict(mut self, strict: bool) -> Self {
self.chat_completions = self.chat_completions.with_strict(strict);
self
}
diff --git a/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs b/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs
index 70bc57cb..3aef90b1 100644
--- a/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs
+++ b/crates/ai-memory-llm/tests/suite/extra_headers_on_the_wire.rs
@@ -231,6 +231,61 @@ async fn an_operator_base_url_redirects_the_opencode_provider() {
assert_eq!(values(request, "user-agent"), vec![DEFAULT_USER_AGENT]);
}
+#[tokio::test]
+async fn factory_opencode_chat_completions_sends_structured_response_format() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/v1/chat/completions"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "id": "id",
+ "object": "chat.completion",
+ "created": 0,
+ "model": "mimo-v2.6-flash",
+ "choices": [{
+ "index": 0,
+ "message": { "role": "assistant", "content": "{\"ok\":true}" },
+ "finish_reason": "stop"
+ }]
+ })))
+ .mount(&server)
+ .await;
+
+ let provider = build_provider(ProviderConfig {
+ provider: ProviderChoice::OpenCode,
+ model: "mimo-v2.6-flash".into(),
+ auth: ProviderAuth::required_api_key_from_env(
+ "OPENCODE_API_KEY",
+ Some(SecretString::from("sk-test")),
+ ),
+ base_url: Some(server.uri()),
+ compat_strict: false,
+ compat_disable_thinking: false,
+ request_timeout_secs: 30,
+ reasoning_effort: None,
+ extra_headers: ExtraHeaders::default(),
+ })
+ .expect("provider builds");
+
+ let value = provider
+ .complete_structured_raw(
+ ChatRequest::user_prompt("emit JSON"),
+ json!({
+ "type": "object",
+ "properties": { "ok": { "type": "boolean" } },
+ "required": ["ok"]
+ }),
+ )
+ .await
+ .expect("structured response parses");
+ assert_eq!(value, json!({"ok": true}));
+
+ let requests = server.received_requests().await.unwrap();
+ assert_eq!(requests.len(), 1);
+ let body: serde_json::Value = serde_json::from_slice(&requests[0].body).unwrap();
+ assert_eq!(body["response_format"]["type"], "json_schema");
+ assert_eq!(body["response_format"]["json_schema"]["strict"], true);
+}
+
/// An explicit `llm_base_url` configures the vendor-fixed providers too: it is
/// how an operator routes `anthropic` or `openai` through a gateway on
/// purpose. Dropping it sends the request, and the key, straight to the vendor