Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
fix(coach): answer a strict-schema 400 with one strict-schema copy of the request (#701)
choksi22129f534a033160
diff --git a/api/coach/core/adapters/http.js b/api/coach/core/adapters/http.jsindex b63041987..523cc9a88 100644--- a/api/coach/core/adapters/http.js+++ b/api/coach/core/adapters/http.js@@ -175,6 +175,7 @@ export function httpAdapter(spec) {if (!chosen) return { code: 1, text: '', stderr: `no model chosen for ${id} (pick one from the list the endpoint serves)` };let body = spec.body({ model: chosen, prompt, system: system || null, schema: schema || null, maxTokens });+ let retriedWithStrictSchema = false;let retriedWithoutJsonMode = false;let transientRetries = 0;for (;;) {@@ -197,6 +198,18 @@ export function httpAdapter(spec) {transientRetries++;continue;}+ // A hosted validator that enforces strict JSON Schema refuses the flat schemas on+ // additionalProperties (OpenRouter, #701). The strict copy is a wire-only change —+ // local grammar-sampling servers keep getting the flat shape they can convert — and+ // only after the endpoint itself said no to it, so Ollama, LM Studio and vLLM never+ // see a request they did not already ask for.+ if (res.status === 400 && spec.withStrictSchema && !retriedWithStrictSchema+ && body.response_format && body.response_format.type === 'json_schema'+ && /additionalProperties|must be explicitly set/i.test(msg)) {+ body = spec.withStrictSchema(body);+ retriedWithStrictSchema = true;+ continue;+ }// Some OpenAI-compatible servers reject the JSON-mode flag outright, and some providers// reject a schema they cannot serve (Gemini: "the specified schema produces a constraint// that has too many states"). Once, without it: the validator still judges the answer.diff --git a/api/coach/core/adapters/openai.js b/api/coach/core/adapters/openai.jsindex 5c4630879..cfb62c76f 100644--- a/api/coach/core/adapters/openai.js+++ b/api/coach/core/adapters/openai.js@@ -30,6 +30,12 @@ export function chatCompletionsSpec(id, { maxTokensField = 'max_completion_token: { type: 'json_object' },[maxTokensField]: maxTokens}),+ // The 400 that says the schema is not strict enough gets a strict-schema copy of the same+ // request once (see strictSchema below); a server that rejects even that, or rejects+ // schema/JSON mode outright, still gets the plain-JSON fallbacks below.+ withStrictSchema: body => (body.response_format && body.response_format.type === 'json_schema'+ ? { ...body, response_format: { ...body.response_format, json_schema: { ...body.response_format.json_schema, schema: strictSchema(body.response_format.json_schema.schema) } } }+ : body),// A server that rejects schema/JSON mode gets the same request once more with plain JSON// mode, then without any; the parser copes with a fenced answer and the validator is the// gate either way.@@ -76,6 +82,31 @@ export function chatCompletionsSpec(id, { maxTokensField = 'max_completion_token}const NOT_CHAT = /realtime|audio|tts|transcri|whisper|embedding|image|dall-e|moderation|search|instruct|codex|computer-use|deep-research|-pro(?:-|$)|davinci|babbage|curie|ada/i;+/* A strict-schema copy of the flat schemas: every node that can be an object gains+ * `additionalProperties: false`. That is exactly what a hosted validator that enforces strict+ * JSON Schema asks for — OpenRouter maps chat-completions response_format to its internal+ * output_config.format.schema and answers the flat shape with+ * 400 "output_config.format.schema: For 'object' type, 'additionalProperties' must be+ * explicitly set to false"+ * — and exactly what schemas.js leaves out on purpose, because llama.cpp's grammar converter+ * has a history of choking on additionalProperties (ollama#8444, #8063). So the strict copy is+ * made only for the wire, only when an endpoint has already proven it wants one; nothing else+ * moves. `required` stays per-schema and the shape stays flat: a validator that demands more+ * (every property required, single types) still says no, and the request falls through to+ * plain JSON mode, where validate.js judges the answer either way.+ */+export function strictSchema(node) {+ if (Array.isArray(node)) return node.map(strictSchema);+ if (node == null || typeof node !== 'object') return node;+ const out = {};+ for (const key of Object.keys(node)) out[key] = strictSchema(node[key]);+ const type = out.type;+ if ((type === 'object' || (Array.isArray(type) && type.includes('object'))) && !('additionalProperties' in out)) {+ out.additionalProperties = false;+ }+ return out;+}+export const isChatModel = id => /^(gpt-|o\d|chatgpt-)/.test(id) && !NOT_CHAT.test(id);export const openaiSpec = chatCompletionsSpec('openai');diff --git a/api/test/adapters-http.test.js b/api/test/adapters-http.test.jsindex bf9903a9b..8e49690d9 100644--- a/api/test/adapters-http.test.js+++ b/api/test/adapters-http.test.js@@ -17,6 +17,7 @@ const compatible = (await import('../coach/core/adapters/compatible.js')).defaulconst { attemptOnce } = await import('../coach/core/pipeline.js');const { HTTP_PROVIDERS, validateBaseUrl, validateHeaders } = await import('../coach/core/providers.js');const { SYSTEM_PROMPT } = await import('../coach/core/system-prompt.js');+const { REVIEW_SCHEMA } = await import('../coach/core/schemas.js');/** A fetch that records what it was asked and answers from a script. Like the real one, it* refuses an already-aborted signal without putting anything on the wire. */@@ -256,6 +257,61 @@ test('a wrapped provider error whose raw text is not JSON is passed through as iassert.ok(r.stderr.includes('upstream said no'), r.stderr);});+test('a validator that rejects the flat schema on additionalProperties gets one strict copy of the same request (#701)', async () => {+ const f = fakeFetch(n => n === 1+ ? { status: 400, body: { error: { type: 'invalid_request_error', message: "output_config.format.schema: For 'object' type, 'additionalProperties' must be explicitly set to false" } } }+ : ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] }));+ const r = await compatible.invoke({ cfg: cfgCompat, prompt: 'P', env: {}, model: 'anthropic/claude-haiku', schema: REVIEW_SCHEMA, fetch: f });+ assert.equal(r.code, 0);+ assert.equal(f.calls.length, 2);+ const first = f.calls[0].body.response_format.json_schema.schema;+ const second = f.calls[1].body.response_format.json_schema.schema;+ // the strict copy says what the validator asked for, at every level it walks+ assert.equal(first.additionalProperties, undefined);+ assert.equal(second.additionalProperties, false);+ assert.equal(second.properties.evidence.additionalProperties, false);+ assert.equal(second.properties.changes.items.additionalProperties, false);+ assert.equal(second.properties.changes.items.properties.target.additionalProperties, false);+ // union types that can be an object say it too, so the walker has no gap to trip on+ assert.equal(second.properties.changes.items.properties.before.additionalProperties, false);+ // nothing else about the request moved — the added marker is the only difference at+ // the top level — and the flat schema the grammar converters need is still the one+ // stored in schemas.js+ assert.deepEqual(Object.keys(second), [...Object.keys(first), 'additionalProperties']);+ assert.equal(second.type, first.type);+ assert.deepEqual(second.required, first.required);+ assert.deepEqual(Object.keys(second.properties), Object.keys(first.properties));+ assert.equal(f.calls[1].body.model, f.calls[0].body.model);+ assert.deepEqual(f.calls[1].body.messages, f.calls[0].body.messages);+ assert.equal(REVIEW_SCHEMA.additionalProperties, undefined);+ assert.equal(REVIEW_SCHEMA.properties.evidence.additionalProperties, undefined);+});++test('a validator that rejects the strict copy as well still falls back to plain JSON mode', async () => {+ const f = fakeFetch(n => n === 1+ ? { status: 400, body: { error: { message: "output_config.format.schema: For 'object' type, 'additionalProperties' must be explicitly set to false" } } }+ : n === 2+ ? { status: 400, body: { error: { message: 'response_format is not supported' } } }+ : ok({ choices: [{ message: { content: ANSWER }, finish_reason: 'stop' }] }));+ const r = await compatible.invoke({ cfg: cfgCompat, prompt: 'P', env: {}, model: 'anthropic/claude-haiku', schema: REVIEW_SCHEMA, fetch: f });+ assert.equal(r.code, 0);+ assert.equal(f.calls.length, 3);+ assert.equal(f.calls[0].body.response_format.type, 'json_schema');+ assert.equal(f.calls[1].body.response_format.type, 'json_schema');+ assert.equal(f.calls[1].body.response_format.json_schema.schema.additionalProperties, false);+ assert.deepEqual(f.calls[2].body.response_format, { type: 'json_object' });+});++test('an additionalProperties complaint about a request with no schema never invents a strict schema', async () => {+ const f = fakeFetch([{ status: 400, body: { error: { message: "output_config.format.schema: For 'object' type, 'additionalProperties' must be explicitly set to false" } } }]);+ const r = await compatible.invoke({ cfg: cfgCompat, prompt: 'P', env: {}, model: 'anthropic/claude-haiku', fetch: f });+ assert.equal(r.code, 1);+ assert.match(r.stderr, /^400 /);+ // the plain-JSON fallback may still drop JSON mode once; a json_schema never goes out+ assert.ok(f.calls.every(c => !c.body.response_format || c.body.response_format.type !== 'json_schema'));+ assert.ok(f.calls.length <= 2);+});+test('a missing key is "missing", like an absent CLI — no request is made', async () => {const f = fakeFetch([]);const r = await anthropic.invoke({ cfg: {}, prompt: 'P', env: {}, fetch: f });