-
Notifications
You must be signed in to change notification settings - Fork 280
Fix 20260210 #772
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
Open
hangweizhang
wants to merge
3
commits into
agentscope-ai:main
Choose a base branch
from
hangweizhang:fix-20260210
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+349
−26
Open
Fix 20260210 #772
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
agentscope-core/src/main/java/io/agentscope/core/util/JsonParseHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright 2024-2026 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.agentscope.core.util; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Helper for parsing JSON that may contain invalid characters (e.g. literal newlines inside | ||
| * string values). LLMs sometimes output pretty-printed or multi-line strings which are not | ||
| * valid JSON; this helper allows best-effort recovery. | ||
| */ | ||
| public final class JsonParseHelper { | ||
|
|
||
| private JsonParseHelper() {} | ||
|
|
||
| /** | ||
| * Parse a JSON string into a map, with fallback to sanitizing literal newlines inside | ||
| * strings when the first attempt fails. Use for tool input JSON that may contain | ||
| * multi-line content (e.g. HTML). | ||
| * | ||
| * @param json JSON string (e.g. tool call arguments) | ||
| * @return parsed map, or null if parsing failed even after sanitization | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static Map<String, Object> parseMapWithNewlineFallback(String json) { | ||
| if (json == null || json.isEmpty()) { | ||
| return null; | ||
| } | ||
| try { | ||
| Object parsed = JsonUtils.getJsonCodec().fromJson(json, Map.class); | ||
| return parsed != null ? (Map<String, Object>) parsed : null; | ||
| } catch (Exception e) { | ||
| String sanitized = escapeNewlinesInJsonStrings(json); | ||
| try { | ||
| Object parsed = JsonUtils.getJsonCodec().fromJson(sanitized, Map.class); | ||
| return parsed != null ? (Map<String, Object>) parsed : null; | ||
| } catch (Exception e2) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sanitize a JSON string by escaping literal newlines and carriage returns inside | ||
| * double-quoted string values. Standard JSON does not allow unescaped newlines in strings; | ||
| * when an LLM returns tool arguments with multi-line content (e.g. HTML with newlines), | ||
| * parsing fails. This method makes such content parseable. | ||
| * | ||
| * @param raw non-null JSON-like string | ||
| * @return new string with \n and \r inside quoted strings replaced by \\n and \\r | ||
| */ | ||
| public static String escapeNewlinesInJsonStrings(String raw) { | ||
| if (raw == null || raw.isEmpty()) { | ||
| return raw; | ||
| } | ||
| StringBuilder sb = new StringBuilder(raw.length() * 2); | ||
| boolean inString = false; | ||
| boolean escape = false; | ||
| for (int i = 0; i < raw.length(); i++) { | ||
| char c = raw.charAt(i); | ||
| if (escape) { | ||
| sb.append(c); | ||
| escape = false; | ||
| continue; | ||
| } | ||
| if (c == '\\') { | ||
| sb.append(c); | ||
| escape = true; | ||
| continue; | ||
| } | ||
| if (c == '"') { | ||
| inString = !inString; | ||
| sb.append(c); | ||
| continue; | ||
| } | ||
| if (inString) { | ||
| if (c == '\n') { | ||
| sb.append("\\n"); | ||
| } else if (c == '\r') { | ||
| sb.append("\\r"); | ||
| } else { | ||
| sb.append(c); | ||
| } | ||
| } else { | ||
| sb.append(c); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the fallback logic is great for robustness, swallowing exceptions without logging can make it very difficult to debug issues where JSON parsing fails for unexpected reasons. It's a good practice to log these exceptions, at least at a debug or warn level, to provide visibility into why parsing is failing. This helps in identifying if the sanitization logic needs to be improved or if the input is fundamentally malformed.