fix: adapt LLM client parameter response_format
for Anthropic
#2050
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.
Description
Fixes(Occasional add_to_vector_store failure on Claude models)
Fix JSON formatting issue with Anthropic LLM responses in memory operations. The Anthropic API doesn't support the response_format parameter directly like OpenAI, (nor was this parameter injected into the request) causing JSON parsing errors occasionally when adding memories to the vector store. This change implements a workaround by injecting explicit JSON formatting requirements through the system message.
Motivation: Memory operations were failing with JSON parsing errors when using Anthropic's Claude model, preventing proper storage of memories in the vector store. While the FACT_RETRIEVAL_PROMPT does contain these formatting instructions, the prompt is not as explicit in its instruction as it could be (
The response should be in json with a key as "facts" and corresponding value will be a list of strings
,you can return an empty list corresponding to the "facts" key
, etc. VSRESPONSE FORMAT: You MUST ALWAYS respond with a JSON object containing a 'facts' array
.Without wanting to make changes to the core prompt that powers vector storage operations, I opted to inject a more explicit system prompt when the
response_format
parameter of typejson_format
is passed from the memory client (system message is the . The more appropriate longer term solution may be a reevaluation of the core prompt or prefilling the Assistant responses to enforce the desired structure.Type of change
Please delete options that are not relevant.
How Has This Been Tested?
previous:
2024-11-24 18:07:09,488 - mem0.vector_stores.qdrant - INFO - Inserting 1 vectors into collection mem0
2024-11-24 18:07:33,504 - httpx - INFO - HTTP Request: POST https://api.anthropic.com/v1/messages?beta=true "HTTP/1.1 200 OK"
2024-11-24 18:07:53,148 - httpx - INFO - HTTP Request: POST https://api.anthropic.com/v1/messages?beta=true "HTTP/1.1 200 OK"
2024-11-24 18:07:55,012 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
2024-11-24 18:07:55,218 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2024-11-24 18:07:55,972 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2024-11-24 18:07:56,476 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2024-11-24 18:07:56,824 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
2024-11-24 18:07:57,763 - httpx - INFO - HTTP Request: POST https://api.anthropic.com/v1/messages "HTTP/1.1 200 OK"
2024-11-24 18:07:57,766 - root - ERROR - Error in new_retrieved_facts: Expecting value: line 1 column 1 (char 0)
2024-11-24 18:07:57,766 - root - INFO - Total existing memories: 0
with change:
2024-11-24 19:36:59,282 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
2024-11-24 19:37:01,046 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
2024-11-24 19:37:04,873 - httpx - INFO - HTTP Request: POST https://api.anthropic.com/v1/messages "HTTP/1.1 200 OK"
2024-11-24 19:37:04,874 - root - INFO - {'id': '0', 'text': 'The capital of France is Paris', 'event': 'ADD'}
2024-11-24 19:37:04,874 - root - INFO - Creating memory with data='The capital of France is Paris'
2024-11-24 19:37:04,893 - mem0.vector_stores.qdrant - INFO - Inserting 1 vectors into collection mem0
Unit Test
test suite in
tests/llms/test_anthropic.py
covering:Test results show all scenarios passing, confirming proper JSON formatting across different use cases.
Checklist:
Maintainer Checklist