-
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) |
95077f0 to
b2afa00
Compare
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
Fixes
json.JSONDecodeErrorcaused by LLMs returning JSON with trailing commas.Changes
RestaurantAgentto strip trailing commas before callingjson.loads().Fixes
Resolves #480