Skip to content

fix: Ensure JSON instruction in prompts for json_object response format (#3559)#4271

Merged
whysosaket merged 3 commits intomem0ai:mainfrom
DhilipBinny:fix/json-instruction-custom-prompt
Mar 16, 2026
Merged

fix: Ensure JSON instruction in prompts for json_object response format (#3559)#4271
whysosaket merged 3 commits intomem0ai:mainfrom
DhilipBinny:fix/json-instruction-custom-prompt

Conversation

@DhilipBinny
Copy link
Copy Markdown
Contributor

@DhilipBinny DhilipBinny commented Mar 9, 2026

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)
  • 28 comprehensive tests

Description

When custom_fact_extraction_prompt is used, the prompt may not contain the word "json". Since response_format={"type": "json_object"} is always passed to llm.generate_response, OpenAI's API throws a 400 BadRequestError.

This fix adds an ensure_json_instruction() utility in utils.py that 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_store methods.

Default prompts already contain "json" so this has zero impact on existing behavior.

Fixes #3559

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

28 tests covering:

  • Core behavior: append when missing, skip when present
  • Case insensitivity: json, JSON, Json all detected
  • 10 parametrized custom prompt variations
  • Edge cases: empty prompt, whitespace-only, idempotency
  • Default prompt regression guards
  • Integration: verifies fix is wired into both sync and async paths

Run with: pytest tests/memory/test_json_prompt_fix.py -v

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

…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
@whysosaket
Copy link
Copy Markdown
Member

Hi @DhilipBinny could you please merge the latest changes from main into this branch and make sure that the tests pass locally?

@DhilipBinny
Copy link
Copy Markdown
Contributor Author

Hi @whysosaket, done! Merged the latest changes from main - no conflicts. All 28 tests pass locally (pytest tests/memory/test_json_prompt_fix.py -v). Let me know if there's anything else I can improve. Thanks for reviewing!

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error 400 'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'.

2 participants