MemOS
MemOS copied to clipboard
fix(memos-local): include json hint in user messages
Summary
Fix OpenAI-compatible JSON mode requests by ensuring at least one user message contains the word json when jsonMode / completeJson is used.
Some OpenAI-compatible gateways and newer OpenAI-style APIs reject response_format: { type: "json_object" } unless the request input messages contain the word json. Previously, the memos LLM client injected the JSON instruction only into the system message. That can still fail on gateways that validate user/input content and return errors like:
status_code=400, Response input messages must contain the word 'json' in some form to use '***.format' of type 'json_object'.
This change appends a short user-level hint (Return valid json only.) when JSON mode is active and the existing user message does not already contain json.
Changes
- Add
ensureJsonWordInUserMessage()incore/llm/client.ts. - Apply it to both:
complete(..., { jsonMode: true })completeJson(...)
- Update unit tests to assert the user message receives the JSON hint.
Testing
From apps/memos-local-plugin:
npm run lint
npx vitest run tests/unit/llm/client.test.ts
Both pass locally.