change response parsing,support v1.1 format#854
change response parsing,support v1.1 format#854shiyiyue1102 wants to merge 1 commit intoagentscope-ai:mainfrom
Conversation
Change-Id: I584de6958cacd4c9a29b4cdc04cfcd79bcee6750
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
There was a problem hiding this comment.
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 intoMem0SearchResponsefor consistent return type.
| // Support both response formats: direct array or object with results | ||
| String trimmed = responseBody != null ? responseBody.trim() : ""; |
There was a problem hiding this comment.
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).
| 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); |
There was a problem hiding this comment.
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.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This pull request updates the
searchmethod inMem0Client.javato 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:
searchmethod inMem0Client.javato 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: I584de6958cacd4c9a29b4cdc04cfcd79bcee6750AgentScope-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.
mvn spotless:applymvn test)