Skip to content

change response parsing,support v1.1 format#854

Open
shiyiyue1102 wants to merge 1 commit intoagentscope-ai:mainfrom
shiyiyue1102:main-change-mem0-response-parsing
Open

change response parsing,support v1.1 format#854
shiyiyue1102 wants to merge 1 commit intoagentscope-ai:mainfrom
shiyiyue1102:main-change-mem0-response-parsing

Conversation

@shiyiyue1102
Copy link
Contributor

This pull request updates the search method in Mem0Client.java to improve compatibility with different Mem0 API response formats. The method now detects whether the response is a JSON array or a wrapped object and parses it accordingly, ensuring consistent handling of both platform and self-hosted Mem0 endpoints.

Improved response format handling:

  • Updated the search method in Mem0Client.java to support both direct JSON array responses and wrapped object responses by checking the response format at runtime and parsing appropriately. This ensures compatibility with both platform and self-hosted Mem0 endpoints.Change-Id: I584de6958cacd4c9a29b4cdc04cfcd79bcee6750

AgentScope-Java Version

[The version of AgentScope-Java you are working on, e.g. 1.0.9, check your pom.xml dependency version or run mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]

Description

[Please describe the background, purpose, changes made, and how to test this PR]

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Change-Id: I584de6958cacd4c9a29b4cdc04cfcd79bcee6750
@shiyiyue1102 shiyiyue1102 requested review from a team and Copilot March 3, 2026 03:59
@gemini-code-assist
Copy link

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Mem0Client#search(...) response parsing to be compatible with multiple Mem0 API response shapes by detecting whether the HTTP response body is a JSON array or a wrapped JSON object and deserializing accordingly.

Changes:

  • Detect response shape at runtime (array vs object) instead of inferring format from the endpoint path.
  • When the response is a JSON array, deserialize into List<Mem0SearchResult> and wrap it into Mem0SearchResponse for consistent return type.

Comment on lines +246 to +247
// Support both response formats: direct array or object with results
String trimmed = responseBody != null ? responseBody.trim() : "";
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

The Javadoc for search(...) (above this block) still states that the v2 API returns a direct array that is always wrapped for consistency. Since the implementation now supports both array and wrapped-object responses at runtime, the Javadoc should be updated to describe the new behavior (and avoid implying that only v2/Platform responses are supported).

Copilot uses AI. Check for mistakes.
Comment on lines +248 to +259
if (trimmed.startsWith("[")) {
// Response is a JSON array: parse as list and wrap in results
List<Mem0SearchResult> results =
jsonCodec.fromJson(
responseBody,
new TypeReference<List<Mem0SearchResult>>() {});

// Wrap in Mem0SearchResponse for consistency
Mem0SearchResponse searchResponse = new Mem0SearchResponse();
searchResponse.setResults(results);
return searchResponse;
} else {
// Self-hosted Mem0 returns response wrapped in {"results":
// [...]}
return jsonCodec.fromJson(responseBody, Mem0SearchResponse.class);
}
// Response is an object (e.g. {"results": [...]})
return jsonCodec.fromJson(responseBody, Mem0SearchResponse.class);
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

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

Test coverage doesn’t currently exercise the newly supported cross-format cases (e.g., Platform mode returning a wrapped object, or self-hosted mode returning a direct JSON array). Adding unit tests for these permutations would prevent regressions in the response-shape detection logic.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ava/io/agentscope/core/memory/mem0/Mem0Client.java 66.66% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

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.

2 participants