⚡ Bolt: Optimize JSON parsing in extract_tools.py with json.loads fast path#6
Conversation
This commit introduces a significant performance optimization to `helpers.extract_tools.json_parse_dirty`. Before this change, the function always used the custom `DirtyJson.parse_string` parser, which is robust but computationally expensive for well-formed JSON. Now, the function attempts to parse the extracted string using the highly-optimized `json.loads` from the standard library first. It falls back to `DirtyJson.parse_string` only if a `JSONDecodeError` occurs. This provides a "fast path" for the majority of LLM outputs, which are well-formed JSON, while preserving the fault-tolerant behavior for edge cases. A journal entry documenting this codebase-specific performance learning has been added to `.jules/bolt.md`. Co-authored-by: thirdeyenation <133812267+thirdeyenation@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What: Added a fast path to
json_parse_dirtyinhelpers/extract_tools.pythat tries the standard library'sjson.loadsbefore falling back to the custom, slowerDirtyJsonparser.🎯 Why: The custom
DirtyJsonparser is great for fixing broken LLM output, but it's significantly slower than Python's built-injsonmodule, which is implemented in C. Since most LLM output is valid JSON, we were paying a heavy performance penalty for no reason on the "happy path".📊 Impact: Significant reduction in CPU time when parsing extracted JSON objects, especially large ones. This is a hot path when extracting tool calls or structured outputs from the LLM.
🔬 Measurement: Can be verified by profiling JSON extraction during heavy tool usage or by running synthetic benchmarks comparing
json.loadstoDirtyJson.parse_stringon large, valid JSON strings.PR created automatically by Jules for task 12031075815286820875 started by @thirdeyenation