Fix JSON parsing error in ExecutionAgent#24
Conversation
问题: - ExecutionAgent 在解析 LLM 返回的决策时,如果收到空响应或无效 JSON, 会抛出 "Invalid JSON: expected value at line 1 column 1" 错误 - 缺少详细的错误日志,难以定位问题根源 修复: 1. 增加决策文本预检查,避免传入空文本 2. 为 structured output 调用添加 try-catch 包装 3. 实现后备方案:当 structured output 失败时,尝试手动解析 JSON 4. 增强错误日志:记录决策文本长度、预览和完整异常堆栈 5. 在 SingleSymbolAgent 中添加决策文本的详细日志输出 改进效果: - 更清晰的错误诊断信息 - 更强的容错能力(后备 JSON 解析方案) - 更好的可调试性
There was a problem hiding this comment.
Pull Request Overview
This PR fixes JSON parsing errors in the ExecutionAgent by adding comprehensive error handling and fallback mechanisms. The changes improve error diagnostics, fault tolerance, and debuggability when LLM responses are empty, malformed, or fail structured output parsing.
Key Changes:
- Added pre-validation to check for empty decision text before parsing
- Implemented fallback mechanism using plain LLM calls and manual JSON extraction when structured output fails
- Enhanced error logging with text length, previews, and full exception stack traces
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| src/agent/execution_agent.py | Enhanced error handling with empty text validation, structured output fallback mechanism, and detailed error logging including stack traces |
| src/agent/single_symbol_agent.py | Added diagnostic logging for decision text length and preview before ExecutionAgent parsing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| parsed_data = json.loads(json_str) | ||
| execution_plan = ExecutionPlan(**parsed_data) |
There was a problem hiding this comment.
Using ExecutionPlan(**parsed_data) with data from an unvalidated JSON parse is potentially unsafe. If the parsed JSON contains unexpected keys or malformed data, this could raise unexpected exceptions. While Pydantic provides some validation, consider adding explicit validation or at minimum handling validation errors separately to provide better error messages.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| import json | ||
| import re |
There was a problem hiding this comment.
The imports json, re, and traceback are being used inline within the function but should be imported at the module level. This is against Python best practices as imports should be placed at the top of the file to improve readability and avoid potential import overhead within frequently called functions.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| logger.print_info(f"[ExecutionAgent] 后备方案成功:手动解析 JSON") | ||
| else: | ||
| # 无法提取 JSON,使用文本分析 | ||
| raise ValueError(f"无法从响应中提取有效的 JSON: {response_content[:100]}") |
There was a problem hiding this comment.
The error message truncates the response content to 100 characters but doesn't indicate this truncation with ellipsis. For consistency with other error messages and better clarity, consider: f"无法从响应中提取有效的 JSON: {response_content[:100]}{'...' if len(response_content) > 100 else ''}"
| raise ValueError(f"无法从响应中提取有效的 JSON: {response_content[:100]}") | |
| raise ValueError(f"无法从响应中提取有效的 JSON: {response_content[:100]}{'...' if len(response_content) > 100 else ''}") |
| if logger: | ||
| logger.print_error(f"[ExecutionAgent] 解析决策失败: {e}") | ||
| # 记录完整的异常堆栈 | ||
| import traceback |
There was a problem hiding this comment.
The import traceback should be moved to the module level instead of being imported inline within exception handling. This is a Python best practice to keep all imports at the top of the file.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@loadchange I've opened a new pull request, #25, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@loadchange I've opened a new pull request, #26, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@loadchange I've opened a new pull request, #27, to work on those changes. Once the pull request is ready, I'll request review from you. |
|
@loadchange I've opened a new pull request, #28, to work on those changes. Once the pull request is ready, I'll request review from you. |
问题:
修复:
改进效果: