Decode bash tool output as UTF-8 instead of locale encoding, closes #1326 (#1327)

This commit is contained in:
Manobhi Sriram
2026-10-04 09:32:30 -04:00
committed by GitHub
parent ff5aec468a
commit d9b4476187
2 changed files with 17 additions and 1 deletions
+3 -1
View File
@@ -58,7 +58,9 @@ class BashTool(Tool):
output = None
if command and command[0] in self.allowed:
try:
output = subprocess.run(command, capture_output=True, text=True, check=False, stdin=subprocess.DEVNULL, timeout=self.timeout).stdout
output = subprocess.run(
command, capture_output=True, text=True, encoding="utf-8", check=False, stdin=subprocess.DEVNULL, timeout=self.timeout
).stdout
except subprocess.TimeoutExpired:
output = f"Command timed out after {self.timeout} seconds"
+14
View File
@@ -232,6 +232,20 @@ class TestAgent(unittest.TestCase):
# Timeout is disabled when set to None
self.assertIsNone(BashTool(timeout=None).timeout)
@patch("subprocess.run")
def testToolsBashEncoding(self, run):
"""
Test bash tool decodes subprocess output as UTF-8 instead of the locale encoding
"""
run.return_value.stdout = "output"
tool = BashTool(allowed=["cat"])
tool(["cat", "file.txt"])
# Subprocess output must be decoded as UTF-8, not left to the locale's default encoding
self.assertEqual(run.call_args.kwargs.get("encoding"), "utf-8")
def testToolsEmbeddings(self):
"""
Test adding Embeddings as a tool