fix: Ensure JSON instruction in prompts for json_object response format (#3559)#4271
Merged
whysosaket merged 3 commits intomem0ai:mainfrom Mar 16, 2026
Merged
Conversation
…at (mem0ai#3559) When using custom_fact_extraction_prompt without the word 'json', OpenAI API throws BadRequestError since response_format json_object requires 'json' to appear in messages. Added ensure_json_instruction() utility that appends a JSON format instruction to the system prompt only when 'json' is not already present. Applied in both sync and async _add_to_vector_store paths. - Zero impact on default prompts (already contain 'json') - Case-insensitive detection - Idempotent (safe to call multiple times) - 32 comprehensive tests
Member
|
Hi @DhilipBinny could you please merge the latest changes from |
Contributor
Author
|
Hi @whysosaket, done! Merged the latest changes from main - no conflicts. All 28 tests pass locally ( |
The test_custom_prompts test fixture used a custom prompt without the word 'json', causing the assertion to fail after ensure_json_instruction appends a JSON format instruction. Adding 'in json format' to the fixture prompt ensures ensure_json_instruction is a no-op, matching the expected behavior.
whysosaket
approved these changes
Mar 16, 2026
This was referenced Mar 16, 2026
jamebobob
pushed a commit
to jamebobob/mem0-vigil-recall
that referenced
this pull request
Mar 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using
custom_fact_extraction_promptwithout the word 'json', OpenAI API throwsBadRequestErrorsinceresponse_format: json_objectrequires 'json' to appear in messages.Added
ensure_json_instruction()utility that appends a JSON format instruction to the system prompt only when 'json' is not already present. Applied in both sync and async_add_to_vector_storepaths.Description
When
custom_fact_extraction_promptis used, the prompt may not contain the word "json". Sinceresponse_format={"type": "json_object"}is always passed tollm.generate_response, OpenAI's API throws a400 BadRequestError.This fix adds an
ensure_json_instruction()utility inutils.pythat checks if "json" appears (case-insensitive) in the combined system + user prompt. If missing, it appends a JSON format instruction to the system prompt. Applied in both sync (Memory) and async (AsyncMemory)_add_to_vector_storemethods.Default prompts already contain "json" so this has zero impact on existing behavior.
Fixes #3559
Type of change
How Has This Been Tested?
28 tests covering:
Run with:
pytest tests/memory/test_json_prompt_fix.py -vChecklist:
Maintainer Checklist