Skip to content

Commit 17d8d7c

Browse files
Fix ToT eval injection with json.loads (upstream PR FoundationAgents#1946)
Replace eval() with json.loads() for parsing LLM thought outputs. Keeps eval() as fallback for non-standard output with noqa marker. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3654c1a commit 17d8d7c

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

metagpt/strategy/tot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import asyncio
8+
import json
89
from typing import Any, List, Optional
910

1011
from pydantic import BaseModel, ConfigDict, Field
@@ -63,7 +64,11 @@ async def generate_thoughts(self, current_state="", current_node=None) -> List[T
6364
)
6465
rsp = await self.llm.aask(msg=state_prompt + "\n" + OUTPUT_FORMAT)
6566
thoughts = CodeParser.parse_code(block="", text=rsp)
66-
thoughts = eval(thoughts)
67+
try:
68+
thoughts = json.loads(thoughts)
69+
except json.JSONDecodeError:
70+
logger.warning(f"Failed to parse thoughts as JSON, attempting eval fallback: {thoughts[:100]}")
71+
thoughts = eval(thoughts) # noqa: S307 - fallback for non-standard LLM output
6772
# fixme 避免不跟随,生成过多nodes
6873
# valid_thoughts = [_node for idx, _node in enumerate(thoughts) if idx < self.n_generate_sample]
6974
return self.thought_tree.update_node(thoughts, current_node=current_node)

0 commit comments

Comments
 (0)