Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing & Executing a BrowserBased Agent in Langgraph Studio #233

Open
KateZhang98 opened this issue Jan 25, 2025 · 0 comments
Open

Importing & Executing a BrowserBased Agent in Langgraph Studio #233

KateZhang98 opened this issue Jan 25, 2025 · 0 comments

Comments

@KateZhang98
Copy link

Async Browser reports handler is closed

Problem

To illustrate the problem, I copied the NavigateTool from langchain-community into local. When I tried to run the simplified agent as illustrated in the code, I got the error

RuntimeError('unable to perform operation on <WriteUnixTransport closed=True reading=False 0xffff8ff9ec20>; the handler is closed')

The error is not reported when I do not have anything after the page.goto(url). However, if I add

response = await page.goto(url)
print("here is the response", response)

The above error appeared.

Screen.Recording.2025-01-25.at.12.50.32.PM.mov
import asyncio

#from langchain_community.tools.playwright.navigate import NavigateTool
from langgraph.graph import START, MessagesState, StateGraph
from playwright.async_api import Page, async_playwright

from .navigate_tool import NavigateTool


async def start_browser():
    playwright = await async_playwright().start()
    browser = await playwright.chromium.launch(headless=True)
    context = await browser.new_context()
    await context.new_page()
    return browser


def build_graph(browser):
    builder = StateGraph(MessagesState)
    # Pass browser to test node
    navigate_tool = NavigateTool.from_browser(async_browser=browser)
    builder.add_node("navigate", navigate_tool)
    builder.add_edge(START, "navigate")
    return builder.compile()


async def create_graph():
    browser = await start_browser()
    return build_graph(browser)


graph = asyncio.run(create_graph())

Sync Browser not allowed for BrowserTool

Problem

Take NavigateTool as an example. For the sync code below, I got the below error if input a url and click submit. Why is async browser required for it?

ValueError('Asynchronous browser not provided to navigate_browser')
Screen.Recording.2025-01-25.at.12.42.35.PM.mov
def start_browser():
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(headless=True)
    context = browser.new_context()
    context.new_page()
    return browser


def build_graph(browser):
    builder = StateGraph(UrlState)
    # Pass browser to test node
    navigate_tool = NavigateTool.from_browser(sync_browser=browser)
    builder.add_node("navigate", navigate_tool)
    builder.add_edge(START, "navigate")
    return builder.compile()


def create_graph():
    browser = start_browser()
    return build_graph(browser)


graph = create_graph()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant