Learning resourcesReviewed · Pydantic

Pydantic AI Harness: Playwright Browser Guide

This guide teaches Python developers how to give an AI agent a real browser, but its built-in network restrictions do not fully isolate untrusted pages. It documents PlaywrightBrowser, a Pydantic AI Harness capability with eighteen tools for navigating Chromium, interacting with pages and inspecting results. Sessions last for one agent run; browser uploads, downloads and durable execution are unsupported.

Researched 15 Nov 2026

In short.

This guide shows Python developers how to add browser control to Pydantic AI agents, with the main catch that sessions end with each run and untrusted pages require safeguards beyond the built-in network policy. PlaywrightBrowser gives the model eighteen tools for stateful navigation, page interaction, screenshots and diagnostics. The documentation explains authenticated sessions, remote browser attachment, iframe handling and troubleshooting in useful detail. It also identifies practical limits: no browser file transfers, no durable execution, bounded text and image results, and incomplete protection against data leaving the browser. No code was executed, and reliability, performance, model compatibility and operating costs were not measured.

Researched and written by an AI model and checked by a second AI model. How it works

Covers the complete supplied page at [https://pydantic.dev/docs/ai/harness/playwright/index.md], including installation, all eighteen tools, configuration, authentication, CDP attachment, lifecycle, iframes, diagnostics, limitations, network policy and API reference. The 0.x compatibility notice and security caveats are included. Linked pages, repositories, issue threads and external articles are excluded. All behavior remains documentation-based: nothing was executed, and runtime compatibility, reliability, performance, operating costs and licence permissions were not independently established.

The overview

Identity

The resource is the Playwright Browser documentation for Pydantic AI Harness at [https://pydantic.dev/docs/ai/harness/playwright/index.md]. It covers the PlaywrightBrowser Python capability, which adds a stateful Chromium browser to a Pydantic AI agent through asynchronous Playwright.

The host model chooses individual browser actions through tools. The page distinguishes this from Browser Use, a separate capability that accepts an open-ended browser task. Each capability has its own browser, so their sessions are not shared.

Assessment

The guide is useful when an agent needs to interact with a website rather than merely retrieve information. It addresses logged-in pages, sites that render content with JavaScript and flows involving several clicks or form fields.

Its strongest feature is the detail around integration failures: missing Chromium, unsuccessful selectors, slow navigation, embedded frames and blocked network requests. These explanations support implementation planning, but the page supplies no success-rate benchmarks or independent evidence of dependable task completion.

Users

The primary audience is Python developers building Pydantic AI agents that need authorized access to interactive websites. Potential applications include browser-based quality checks, retrieving information from an authenticated account and working through JavaScript-driven interfaces.

Beginners get installation commands and short examples, but still need to understand asynchronous Python, model configuration and browser sessions. Experienced developers get details on custom network policies, remote browser connections, telemetry and lower-level session and toolset APIs.

Capabilities

The eighteen tools cover navigation, accessibility snapshots, clicking, typing, key presses, selecting options, hovering, waiting, screenshots, text extraction, scrolling, history, JavaScript execution, tabs, dialogs, console messages and network requests. Actions operate on the active tab.

An accessibility snapshot gives the model a structured description of the page and element references such as aria-ref=e4. References can also identify elements inside embedded frames. The documentation recommends these references over model-written CSS selectors and reserves screenshots for tasks needing visual inspection.

Authenticated access uses supplied cookies and localStorage. A Chrome DevTools Protocol endpoint can connect the capability to an existing Chromium process. OpenTelemetry spans and diagnostic tools expose browser events, including refused requests and page errors.

Using it

Requirements

A local setup requires Pydantic AI, pydantic-ai-harness with its playwright optional extra, and a separately downloaded Chromium binary. The examples use an asynchronous agent run with a configured model. The page does not establish minimum Python or operating-system versions, hardware requirements or a complete model compatibility list.

Connecting through cdp_url removes the need for a local Chromium binary. Authenticated tasks require storage_state captured outside the capability; that state contains credentials capable of impersonating the account. The documentation advises keeping it out of source control and logs.

Private and local addresses are blocked by default. Reaching an internal dashboard or local application requires deliberately changing that setting and assessing the resulting access.

Mechanism

PlaywrightBrowser registers browser tools, supplies usage guidance to the agent and manages the browser session. Chromium starts on the first browser-tool call. The session persists across tool calls within that run and is released when the run succeeds, fails or is cancelled. Concurrent runs receive separate browser sessions.

A remote CDP connection still creates a new browser context for the run, so it does not inherit sessions already open in that Chromium process. Supplied storage_state initializes the new context instead.

Text results have a default approximate budget of 4,000 tokens. Element actions default to a five-second deadline; navigation and browser startup default to sixty seconds. A deadline covers the whole tool operation, rather than restarting for every internal Playwright call. Browser failures are returned as error strings that the model can respond to.

Network enforcement combines a request guard with checks of the resulting page URL. Service workers are blocked. These mechanisms limit access, but the page explicitly says they are not a general security boundary.

Setup

The documented installation is pip install "pydantic-ai-harness[playwright]", followed by playwright install chromium. Equivalent uv commands are provided. Import Agent from pydantic_ai and PlaywrightBrowser from pydantic_ai_harness.playwright, then add a PlaywrightBrowser instance to the agent's capabilities list.

allowed_domains is unset by default, which allows every public host; the documentation recommends setting it to an explicit allowlist when the agent may act on untrusted input. An entry such as example.com also permits its subdomains; wildcard entries such as *.example.com are rejected. For authenticated work, capture a Playwright storage-state object through a controlled login and supply it directly as storage_state.

If Chromium is missing, the tool returns installation guidance and emits BrowserUnavailableWarning. Automatic download is available through auto_install_chromium=True and is disabled by default. The local Chromium renderer sandbox is enabled by default; an attached remote browser uses its existing sandbox configuration.

Example

A small starting task is to configure PlaywrightBrowser with allowed_domains=['example.com'], attach it to an agent using a configured model, and ask the agent to open [https://example.com] and report the page title. This adapts the documentation's introductory example; no resulting title or successful run is established here.

For a form interaction, the documented sequence is to inspect snapshot, target a field using its aria-ref handle, call type_text and then press_key('Enter') if submission is required. Typing alone does not submit. Fields that react to individual keystrokes may need sequential=True, while native dropdowns use select_option.

Assessment & evidence

Limitations

Uploads are not exposed, downloads are refused and sessions support at most eight tabs. Screenshots over 5 MB produce a bounded error. Text output can be truncated, including text gathered from embedded frames. Ordinary CSS selectors cannot cross iframe boundaries; frame-specific snapshot references provide access there.

Durable execution is rejected when constructing the agent because a live browser page cannot survive activity replay or a worker restart. Authentication must be prepared separately, and the page makes no claim that an expired login state can be renewed automatically.

By default allowed_domains is None, so the agent can reach any public host, although private, loopback and link-local addresses are still blocked. When allowed_domains is set, it bounds top-level navigation and script-driven data requests (fetch, XHR, EventSource, WebSocket, sendBeacon), but by default it leaves passive resources (images, stylesheets, scripts, fonts, media) and sub-frame documents outside the allowlist. Those requests can still carry information outward. The private-address block applies more broadly, but DNS rebinding remains a documented gap because Chromium resolves hostnames separately.

A stricter EgressPolicy can restrict every request kind, but the documentation still recommends external network isolation for untrusted-input scenarios. Neither an allowlist nor a browser sandbox guarantees protection from prompt injection or all data leakage.

Fit rationale

This approach fits tasks where browser state and interaction are necessary: an authenticated page, content loaded by JavaScript or a sequence of UI actions. The model can inspect intermediate results and choose its next action, while the application controls session setup and network policy.

The tradeoff is responsibility for a browser process, account credentials, model-directed actions and network isolation. The documentation is detailed enough to inform those engineering choices, but it does not establish production reliability or a security guarantee. Its lack of durable execution is decisive for tasks that must resume after a worker restart.

Alternatives

The page recommends a web-search tool for research questions and a web-fetch tool for a known static URL. These avoid introducing a browser when page interaction is unnecessary.

Browser Use is the documented alternative for delegating an open-ended browser goal. Its linked documentation is outside the supplied evidence, so its implementation, pricing and comparative performance are unknown.

For applications that need the same guarded browser without agent capability hooks, the page exposes PlaywrightBrowserSession. PlaywrightBrowserToolset supplies the eighteen tools around a provided session. These are lower-level integration options within the same system.

Evidence

The evidence is the complete documentation snapshot from [https://pydantic.dev/docs/ai/harness/playwright/index.md], captured at 2026-11-15T11:41:47.307Z. Its supplied SHA-256 source revision is 371bbe86fc19b82792150d025a503ac59471c04085b96835d20a368176006eef.

The page includes explanatory guidance, installation commands, examples, option defaults, limitations and an API reference. These establish what the documentation describes. No code was executed, browser session opened or task result observed. Linked source code, issue discussions, external examples and other documentation pages were not assessed.

Access & freshness

Costs

The page gives no monetary prices for the package, model calls, browser hosting or telemetry. Total operating cost is unknown. It does not establish whether any particular deployment or provider is free.

Chromium launches only when a browser tool is called, avoiding browser startup for runs that never browse. Text budgets and optional screenshots affect what reaches the model, but the page provides no measured token consumption, latency, CPU usage, memory usage or cost per completed task.

Rights

The page links implementation source at [https://github.com/pydantic/pydantic-ai/tree/main/src/pydantic_ai_harness/pydantic_ai_harness/playwright/], but the supplied text contains no licence identifier or licence terms for the software or documentation.

Commercial-use, modification and redistribution permissions are therefore unknown from this evidence. A source-code link alone does not establish those permissions.

Freshness

The snapshot was captured on 15 November 2026, but it does not establish a package release number or the page's last-updated date. Capture time alone does not prove compatibility with an installed release.

The documentation warns that Pydantic AI Harness APIs may change between minor releases while on 0.x, with deprecation warnings and migration guidance. References to outstanding issues describe documented gaps; their current resolution status is unknown because the linked issue pages are outside the supplied evidence.

Next steps

First establish whether the task actually needs a browser. If it does, confirm the intended package release and model configuration, install the optional dependency and Chromium, and begin with a small read-only task on an explicitly permitted site.

For authenticated work, capture narrowly scoped login state and treat it as a credential. For untrusted pages, define the required network reach and use the documented container, VM, firewall or proxy protections as appropriate. Pair consequential actions with approval hooks.

Before relying on the integration, verify the actual site's frame handling, selectors, timeouts and session behavior. Check licence terms and provider costs separately. Tasks requiring browser uploads, downloads or restart-safe sessions need another approach under the documented limitations.

Evidence for this report

selected-page.txtselected · checked 2026-11-15