-
Notifications
You must be signed in to change notification settings - Fork 843
fix(agent): Autofix trailing commas in LLM-generated JSON #619
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
base: main
Are you sure you want to change the base?
Conversation
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.
Code Review
This pull request aims to fix JSON decoding errors from LLM-generated content by removing trailing commas. The approach of using a regular expression is sound. However, the implemented regex is not fully robust and can fail in cases of multiple trailing commas. I've suggested a more robust regex that handles these cases correctly.
| raise ValueError("Cleaned JSON string is empty.") | ||
|
|
||
| # Autofix: Remove trailing commas from arrays and objects | ||
| json_string_cleaned = re.sub(r",\s*([\]}])", r"\1", json_string_cleaned) |
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.
The current regex only handles a single trailing comma. It would fail to fix a string with multiple trailing commas, like [1, 2,,], which would be converted to the still-invalid [1, 2,]. Using a positive lookahead is more robust and handles multiple trailing commas correctly in a single pass.
| json_string_cleaned = re.sub(r",\s*([\]}])", r"\1", json_string_cleaned) | |
| json_string_cleaned = re.sub(r",(?=\s*[\]}])", "", json_string_cleaned) |
b2afa00 to
b08a1da
Compare
| raise ValueError("Cleaned JSON string is empty.") | ||
|
|
||
| # Autofix: Remove trailing commas from arrays and objects | ||
| json_string_cleaned = re.sub(r",(?=\s*[\]}])", "", json_string_cleaned) |
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.
thanks for this
can you put it in the SDK here:
https://github.com/google/A2UI/blob/main/a2a_agents/python/a2ui_agent/src/a2ui/extension/send_a2ui_to_client_toolset.py#L265
and add some tests
I think flow would be
if not jsonschema.validate():
run fixers
if trailing comma fixer does something
emit warning
jsonschema.validate
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.
Thanks, I've adjusted the code into the SDK and followed the suggested flow. I also added unit tests to verify the fix and the warning message.
91a7032 to
e259644
Compare
e259644 to
144883a
Compare
revert sample-agentand add fix to adk-agent for malformed JSON Add robust JSON autofix and tests Add robust JSON autofix logic
0253f9a to
a87a01b
Compare
Fixes
json.JSONDecodeErrorcaused by LLMs returning JSON with trailing commas.Changes
RestaurantAgentto strip trailing commas before callingjson.loads().Fixes
Resolves #480