Add the `computer` tool contract to core: `pydantic_ai.computer_use` and `ComputerTool`

Merged
AD
11d ago
+1113-34

Part of #9347

This PR adds the types model classes need to send and read computer-use calls: the mouse and keyboard actions, the typed call and result parts, and the ComputerTool native tool. A native tool is one the provider defines on its side, such as OpenAI's computer use tool.

On its own, it changes nothing for users. ComputerUse() in Pydantic AI Harness (#11037) adds the tool to an agent. The OpenAI PR (#11038) and the Anthropic PR (#11039) send it to each provider as that provider's own computer tool. The screen itself is not in core: the display protocols live in the harness (#11037).

How to review:

  1. computer_use.py: the actions, the parts and ComputerResult.
  2. ComputerTool in native_tools/__init__.py.
  3. _drop_unrunnable_computer_tool and ModelRequestParameters.available_tool_names_for_kind in models/__init__.py. They decide whether a request sends ComputerTool.
  4. The typed metadata change in messages.py.
  5. models/_computer_calls.py: the X11 key names Claude and Gemini send.

Changes in this round

  • MouseButton drops back and forward, and MouseMove drops modifiers. Models used them only when the prompt named them.
  • Scroll.amount has no default. Each adapter sets it from what its provider sends.
  • ComputerResult.changed is now actions_changed, so it can't be read as "the screen changed".
  • A typed return part must name its kind's metadata type again. The relaxation that let it leave the type out is reverted, with its test. This also fixes the cognitive complexity check, which flagged _check_declared_shape at 21.
  • models/_computer_calls.py keeps only the X11 key map. control, prior and next are cut: no model sent them. The call-blocking helper moved to the OpenAI adapter (#11038), its only user.
  • Renames for clarity: _drop_unrunnable_computer_tool, key_from_x11, _mark_type_required, _lowercase_key, _check_x_and_y_given_together.
  • tests/test_tool_search.py: a test that landed on main after this branch lists every native tool TestModel supports. ComputerTool is now one of them, so the snapshot gains it. This was the run tests failure on the last push.

What the PR adds

  • pydantic_ai.computer_use:
    • Actions: Click, MouseMove, Drag, MouseDown, MouseUp, Scroll, TypeText, PressKeys, HoldKeys, Wait, Screenshot, Zoom, CursorPosition. Each one's type is its class name in snake case.
    • Supporting types: Point, Region, MouseButton, and the union ComputerToolAction.
    • Key names come from one fixed list, with common aliases. Return and KP_Enter are accepted, and ctrl+s becomes two keys.
  • Call and result parts:
    • COMPUTER_KIND is the tool kind computer_use.computer. Model classes look for this label, not for a tool name.
    • ComputerCallPart and ComputerReturnPart are the typed parts for a call and its result.
    • ComputerResult is the result's metadata: how many actions ran, the error if one failed, and actions_changed when a hook or approval changed the actions before they ran.
  • Typed tool metadata: ToolKind(metadata=...) declares the shape of a kind's metadata, and typed_metadata returns it after validation, or None if it doesn't fit.
  • ComputerTool(display_size=...): a native tool sent in place of the request's local computer tool, which still runs the calls. It is sent only when the request has exactly one callable tool of the computer kind. With none, an optional one is left out and a required one is refused. With more than one, the request is refused.
  • ModelRequestParameters.available_tool_names_for_kind: the tools of a kind the model can call on this request.

Design decisions

Core holds only what model classes need. Display and the optional display abilities are harness API, so they can change with ComputerUse without a core release.

No computer-use rules in the model layer. Existing paths already cover each case: a text-only model gets the "was not sent" note in place of a screenshot, and an oversized Anthropic screenshot gets Anthropic's 400, which FallbackModel and ModelFallback move on from.

Actions have no limits on amounts. Validation checks only that an action makes sense: positions aren't negative, durations are positive and finite, text isn't empty, a drag has two or more points. Live runs past the old limits worked; they only took longer. A developer who wants limits can refuse calls in a before_tool_execute hook.

A failed action still returns a normal tool result. The failure goes in ComputerResult.error, and the result keeps its screenshot. ToolFailed carries only text, so failing the tool call would lose the screenshot of the screen after the failure.

Evidence

  • Back/forward buttons and modifier keys on a mouse move appeared in live runs only when the prompt asked for them.
  • An oversized screenshot on Claude Sonnet 5.5 alone: ModelRequestRejectedError, status 400. In FallbackModel(Sonnet 5.5, Haiku 4.5), Haiku answered and read the screen.
  • Typing 10,000 to 20,000 characters, 1,000 scroll clicks, a 500-point drag and 9 keys at once all ran on X11 and Daytona desktops.

Testing

Unit tests for the actions, key names, typed parts, typed metadata and ComputerTool, with function models, because they check what the model layer adds to a request. The provider PRs record real requests (#11038, #11039).

Local, one file at a time: tests/test_computer_use.py + tests/test_computer_use_actions.py + tests/models/test_computer_calls.py 48 passed, tests/test_typed_tool_parts.py 72 passed, the tests/test_tool_search.py snapshot test passed. ruff and pyright on the changed files: clean. scripts/check_complexity.py --base origin/main: clean.

Merging

Merge this first, then #11037, then #11038 and #11039.