Skip to content

Commit debbd9c

Browse files
fix: Replace shallow copy with deep copy in _build_messages()
- Added import copy to both agent.py and test file - Changed prompt.copy() to copy.deepcopy(prompt) to prevent mutation of original objects - Ensures nested dictionaries in multimodal prompts are not modified This fixes the issue where modifying item["text"] would mutate the original prompt objects. Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 77a3030 commit debbd9c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/praisonai-agents/praisonaiagents/agent/agent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import time
33
import json
4+
import copy
45
import logging
56
import asyncio
67
from typing import List, Optional, Any, Dict, Union, Literal, TYPE_CHECKING, Callable, Tuple
@@ -868,8 +869,8 @@ def _build_messages(self, prompt, temperature=0.2, output_json=None, output_pyda
868869
if isinstance(prompt, str):
869870
prompt = prompt + "\nReturn ONLY a valid JSON object. No other text or explanation."
870871
elif isinstance(prompt, list):
871-
# Create a copy to avoid modifying the original
872-
prompt = prompt.copy()
872+
# Create a deep copy to avoid modifying the original
873+
prompt = copy.deepcopy(prompt)
873874
for item in prompt:
874875
if item.get("type") == "text":
875876
item["text"] = item["text"] + "\nReturn ONLY a valid JSON object. No other text or explanation."

src/praisonai/tests/unit/test_agent_refactor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import asyncio
8+
import copy
89
import logging
910
from typing import Dict, Any
1011

@@ -66,8 +67,8 @@ def _build_messages(self, prompt, temperature=0.2, output_json=None, output_pyda
6667
if isinstance(prompt, str):
6768
prompt = prompt + "\nReturn ONLY a valid JSON object. No other text or explanation."
6869
elif isinstance(prompt, list):
69-
# Create a copy to avoid modifying the original
70-
prompt = prompt.copy()
70+
# Create a deep copy to avoid modifying the original
71+
prompt = copy.deepcopy(prompt)
7172
for item in prompt:
7273
if item.get("type") == "text":
7374
item["text"] = item["text"] + "\nReturn ONLY a valid JSON object. No other text or explanation."

0 commit comments

Comments
 (0)