Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed json.loads() decode error when LLM produces code block #1860

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

FoliageOwO
Copy link
Contributor

Description

Using some models may make mem0 internal responses generating JSONDecodeError:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The response from Mem0 LLM is below (using diff as code block format because the triple backticks "```" does not show at the code block below, so ignore these + that at the first of a line).

+ ```json
+ {
+     "memory": [
+         {
+             "id": "(UUID)",
+             "text": "I'm visiting Paris",
+             "event": "ADD"
+         }
+     ]
+ }
+ ```

Fixes #1854

Type of change

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

How Has This Been Tested?

Please refer to the issue #1854.

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

@@ -170,6 +172,7 @@ def _add_to_vector_store(self, messages, metadata, filters):
messages=[{"role": "user", "content": function_calling_prompt}],
response_format={"type": "json_object"},
)
new_memories_with_actions = parse_json_response(new_memories_with_actions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we should still do "json.loads"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prateekchhikara Hi. Thanks for reviewing.

I think it may be the name of the function that confused you.

The function parse_json_response only extracts the JSON content from the potential code block from the given origin response string, it doesn't call json.loads(), which means it doesn't return a dictionary but a string.

So we HAVE to call json.loads() after the parsing progress.

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.

JSONDecodeError: AI sometimes produces JSON with a code block format
2 participants