Skip to content

Fix JSON parsing error in ExecutionAgent#24

Merged
loadchange merged 7 commits intomainfrom
claude/fix-execution-agent-json-01G27yv95627j6WRqyUKqbbG
Nov 21, 2025
Merged

Fix JSON parsing error in ExecutionAgent#24
loadchange merged 7 commits intomainfrom
claude/fix-execution-agent-json-01G27yv95627j6WRqyUKqbbG

Conversation

@loadchange
Copy link
Copy Markdown
Collaborator

问题:

  • 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 解析方案)
  • 更好的可调试性

问题:
- 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 解析方案)
- 更好的可调试性
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/agent/execution_agent.py Outdated
Comment thread src/agent/single_symbol_agent.py Outdated
Comment thread src/agent/execution_agent.py Outdated
Comment thread src/agent/execution_agent.py Outdated
Comment on lines +174 to +175
parsed_data = json.loads(json_str)
execution_plan = ExecutionPlan(**parsed_data)
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment on lines +167 to +168
import json
import re
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment thread src/agent/execution_agent.py Outdated
Comment thread src/agent/execution_agent.py Outdated
logger.print_info(f"[ExecutionAgent] 后备方案成功:手动解析 JSON")
else:
# 无法提取 JSON,使用文本分析
raise ValueError(f"无法从响应中提取有效的 JSON: {response_content[:100]}")
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ''}"

Suggested change
raise ValueError(f"无法从响应中提取有效的 JSON: {response_content[:100]}")
raise ValueError(f"无法从响应中提取有效的 JSON: {response_content[:100]}{'...' if len(response_content) > 100 else ''}")

Copilot uses AI. Check for mistakes.
if logger:
logger.print_error(f"[ExecutionAgent] 解析决策失败: {e}")
# 记录完整的异常堆栈
import traceback
Copy link

Copilot AI Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment thread src/agent/execution_agent.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI commented Nov 21, 2025

@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.

loadchange and others added 3 commits November 21, 2025 10:17
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>
Copy link
Copy Markdown

Copilot AI commented Nov 21, 2025

@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.

loadchange and others added 2 commits November 21, 2025 10:19
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI commented Nov 21, 2025

@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.

Copy link
Copy Markdown

Copilot AI commented Nov 21, 2025

@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.

@loadchange loadchange merged commit b3b1bdc into main Nov 21, 2025
@loadchange loadchange deleted the claude/fix-execution-agent-json-01G27yv95627j6WRqyUKqbbG branch December 5, 2025 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants