Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix: ensure save_conversation_path ends with a slash and add tests for conversation logging

Takeshi Kishif710c89b40e8
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 45f264694..c1fa053ad 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -111,6 +111,8 @@ class Agent:
self.use_vision = use_vision
self.llm = llm
self.save_conversation_path = save_conversation_path
+ if self.save_conversation_path and '/' not in self.save_conversation_path:
+ self.save_conversation_path = f'{self.save_conversation_path}/'
self.save_conversation_path_encoding = save_conversation_path_encoding
self._last_result = None
self.include_attributes = include_attributes
diff --git a/docs/customize/agent-settings.mdx b/docs/customize/agent-settings.mdx
index efa434225..9a3e23cbf 100644
--- a/docs/customize/agent-settings.mdx
+++ b/docs/customize/agent-settings.mdx
@@ -35,7 +35,7 @@ agent = Agent(
llm=llm,
controller=custom_controller, # For custom tool calling
use_vision=True, # Enable vision capabilities
- save_conversation_path="logs/conversation.json" # Save chat logs
+ save_conversation_path="logs/conversation" # Save chat logs
)
```
diff --git a/tests/test_save_conversation.py b/tests/test_save_conversation.py
new file mode 100644
index 000000000..8b2b25856
--- /dev/null
+++ b/tests/test_save_conversation.py
@@ -0,0 +1,83 @@
+"""
+Simple try of the agent.
+
+@dev You need to add OPENAI_API_KEY to your environment variables.
+"""
+
+import os
+import shutil
+import sys
+
+from browser_use.browser.browser import Browser, BrowserConfig
+from browser_use.browser.context import BrowserContext
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from langchain_openai import ChatOpenAI
+
+from browser_use import Agent, AgentHistoryList
+
+llm = ChatOpenAI(model='gpt-4o')
+
+
+async def test_save_conversation_contains_slash():
+ if os.path.exists('./logs'):
+ shutil.rmtree('./logs')
+
+ agent = Agent(
+ task=('go to google.com and search for text "hi there"'),
+ llm=llm,
+ browser_context=BrowserContext(
+ browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),
+ ),
+ save_conversation_path='logs/conversation',
+ )
+ history: AgentHistoryList = await agent.run(20)
+
+ result = history.final_result()
+ assert result is not None
+
+ assert os.path.exists('./logs'), 'logs directory was not created'
+ assert os.path.exists('./logs/conversation_2.txt'), 'logs file was not created'
+
+
+async def test_save_conversation_not_contains_slash():
+ if os.path.exists('./logs'):
+ shutil.rmtree('./logs')
+
+ agent = Agent(
+ task=('go to google.com and search for text "hi there"'),
+ llm=llm,
+ browser_context=BrowserContext(
+ browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),
+ ),
+ save_conversation_path='logs',
+ )
+ history: AgentHistoryList = await agent.run(20)
+
+ result = history.final_result()
+ assert result is not None
+
+ assert os.path.exists('./logs'), 'logs directory was not created'
+ assert os.path.exists('./logs/_2.txt'), 'logs file was not created'
+
+
+async def test_save_conversation_deep_directory():
+ if os.path.exists('./logs'):
+ shutil.rmtree('./logs')
+
+ agent = Agent(
+ task=('go to google.com and search for text "hi there"'),
+ llm=llm,
+ browser_context=BrowserContext(
+ browser=Browser(config=BrowserConfig(headless=False, disable_security=True)),
+ ),
+ save_conversation_path='logs/deep/directory/conversation',
+ )
+ history: AgentHistoryList = await agent.run(20)
+
+ result = history.final_result()
+ assert result is not None
+
+ assert os.path.exists('./logs/deep/directory'), 'logs directory was not created'
+ assert os.path.exists('./logs/deep/directory/conversation_2.txt'), 'logs file was not created'