今話題のbrowser-useというPythonのライブラリの検証を依頼されたので試してみましたが、
タイトルにあるエラーでAIが全然検索を開始してくれなかったので、その時の解決方法と調べた内容をまとめます。
結論
結論、langchain-google-genai
を2.0.8にする。
動かなかった時は2.0.9でlangchain-google-genai
の最新版(2025/01/25現在)を使ってましたが、ダウングレードすることで動きました。
概要
一通り環境を整えてpythonのコードを実行したところ、browser-useはちゃんと起動したけど、
立ち上がったブラウザは真っ白で一向に検索が始まらない。
ターミナルに出力されるStepには何やら name
属性は空じゃダメだと書かれている。
📍 Step 1
ERROR [agent] ❌ Result failed 1/3 times:
Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.
環境
MacOS Sonoma 14.4.1
Python 3.12.4
browser-use 0.1.24
langchain-google-genai 2.0.9 (エラー発生時)
browser-useをGeminiで動かす方法
まずはbrowser-useで使用するAIをGeminiで環境構築する方法を解説します。
以下の記事を参考にさせていただきました。
browser-use やばいです
browser-useの基礎理解
browser-useをGeminiで動かすまで(langchain-google-genai 2.0.8がPyPIにリリースされるまでは暫定的にリポジトリからインストール)
Pythonの仮想環境をつくる
ちなみにわたくし、Pythonについては初心者です。理解が及んでいない部分が多々ありますがご了承ください。
※Pythonは3.11以上をご利用ください。筆者の環境は3.12.4です。
mkdir brower-use-sample
cd browser-use-sample
以降は作成したディレクトリ下で作業します。
python -m venv .venv # 仮想環境の作成
source .venv/bin/activate # 仮想環境立ち上げ
プロンプトが(.venv) user@username browser-use-sample
のようになっていれば仮想環境が立ち上がっています。
browser-useをインストールする
仮想環境が立ち上がったら、この中でbrowser-useと操作するブラウザをインストールします。
pip install browser-use
playwright install # 操作するブラウザをインストールしています
※playwrightは様々なブラウザをコマンドライン上で実行できるライブラリです。
※もしご自身の環境に貼っていない場合は、まず playwright
からインストールしてください。
langchain-google-genaiをインストールする
Geminiを使用するため langchain-google-genai
をインストールします。
pip install "langchain-google-genai @ git+ssh://git@github.com/langchain-ai/langchain-google.git#subdirectory=libs/genai"
上記のコマンドを実行したところ、2025年1月25日現在最新の2.0.9がインストールされました。
APIキーの取得と設定
Gemini API キーを取得する こちらからGeminiのAPIキーを取得してコピーしておいてください。
touch .env
vim .env
# .env
GOOGLE_API_KEY=<コピーしたAPIキー>
※ここで変数名を GEMINI_API_KEY
にしたら動かなかったので、必ず GOOGLE_API_KEY
にしてください。
うまくキーが反映されないときは以下をを実行してみてください。
source .env
実行するファイルを作成
touch main.py
vim main.py # エディターを使う場合はそちらで作成したファイルを開いてください
# main.py
from langchain_google_genai import ChatGoogleGenerativeAI
from browser_use import Agent
import asyncio
async def main():
agent = Agent(
task="Googleで東京の最新の天気予報を調べて教えてください",
llm=ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp"),
)
result = await agent.run()
print(result)
asyncio.run(main())
task
に書かれている文章がAIに依頼する内容です。
上記のファイルを実行するとbrowser-useがGeminiを使ってtaskの内容を実行します。
python main.py
成功すると以下の様なブラウザが立ち上がり、天気予報を調べてくれます。

📍 Step 3
INFO [agent] 👍 Eval: Success - Extracted the weather forecast information from the search result.
INFO [agent] 🧠 Memory:
INFO [agent] 🎯 Next goal: Complete the task by providing the weather information.
INFO [agent] 🛠️ Action 1/1: {"done":{"text":"東京の天気予報は、今日(2025年1月25日)は雲が広がり、午前中はにわか雨が心配ですが、段々と日差しが届くでしょう。朝晩は冷え込み、昼間はこの時期らしい寒さが戻るでしょう。"}}
INFO [agent] 📄 Result: 東京の天気予報は、今日(2025年1月25日)は雲が広がり、午前中はにわか雨が心配ですが、段々と日差しが届くでしょう。朝晩は冷え込み、昼間はこの時期らしい寒さが戻るでしょう。
INFO [agent] ✅ Task completed successfully
INFO [agent] Created GIF at agent_history.gif
Name cannot be empty.
2025年1月25日現在、前述までの設定で main.py
を実行すると以下の様に失敗しました。
% python main.py
INFO [browser_use] BrowserUse logging setup complete with level info
INFO [root] Anonymized telemetry enabled. See https://github.com/browser-use/browser-use for more information.
INFO [agent] 🚀 Starting task: Googleで東京の最新の天気予報を調べて教えてください。
INFO [agent]
📍 Step 1
ERROR [agent] ❌ Result failed 1/3 times:
Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.
INFO [agent]
📍 Step 1
ERROR [agent] ❌ Result failed 2/3 times:
Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.
INFO [agent]
📍 Step 1
ERROR [agent] ❌ Result failed 3/3 times:
Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.
ERROR [agent] ❌ Stopping due to 3 consecutive failures
INFO [agent] Created GIF at agent_history.gif
AgentHistoryList(all_results=[ActionResult(is_done=False, extracted_content=None, error='Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.\n', include_in_memory=True), ActionResult(is_done=False, extracted_content=None, error='Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.\n', include_in_memory=True), ActionResult(is_done=False, extracted_content=None, error='Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.\n', include_in_memory=True)], all_model_outputs=[])
どうやら GenerateContentRequest
に基づいてモデルのインスタンスを作成する様なのですが、その時にname
は省略不可なのに値が存在しないというエラーの様です。
ただこのエラーで調べてみてもなかなか解決策が見つからず、参考にしていた記事では2.0.8で動くっぽかったのでひとまずそちらにダウングレードすることにしました。
pip install --upgrade langchain-google-genai==2.0.8
もっとよく探せば2.0.9の正式な実行方法があるのかもしれませんが、今回はbrowser-useが今のプロジェクトで使用できそうか調べるのが私のタスクだったので取り急ぎこの対策で手を打ちました。
もし正しい方法をご存知の方がいらっしゃいましたらご教授いただけますと幸いです。
私ももう少し公式リファレンスなどを調査してみようと思います。
https://ai.google.dev/api/generate-content?hl=ja
https://python.langchain.com/docs/integrations/chat/google_generative_ai/#integration-details
https://github.com/langchain-ai/langchain-google/releases/tag/libs%2Fgenai%2Fv2.0.9
まとめ
AI技術が進んでいるとはいえ、まだまだ最新の情報を調べるには英語が必要な様です…
コメント