Add the `computer` tool contract to core: `pydantic_ai.computer_use` and `ComputerTool`
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:
computer_use.py: the actions, the parts andComputerResult.ComputerToolinnative_tools/__init__.py._drop_unrunnable_computer_toolandModelRequestParameters.available_tool_names_for_kindinmodels/__init__.py. They decide whether a request sendsComputerTool.- The typed metadata change in
messages.py. models/_computer_calls.py: the X11 key names Claude and Gemini send.
Changes in this round
MouseButtondropsbackandforward, andMouseMovedropsmodifiers. Models used them only when the prompt named them.Scroll.amounthas no default. Each adapter sets it from what its provider sends.ComputerResult.changedis nowactions_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 complexitycheck, which flagged_check_declared_shapeat 21. models/_computer_calls.pykeeps only the X11 key map.control,priorandnextare 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 toolTestModelsupports.ComputerToolis now one of them, so the snapshot gains it. This was therun testsfailure 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'stypeis its class name in snake case. - Supporting types:
Point,Region,MouseButton, and the unionComputerToolAction. - Key names come from one fixed list, with common aliases.
ReturnandKP_Enterare accepted, andctrl+sbecomes two keys.
- Actions:
- Call and result parts:
COMPUTER_KINDis the tool kindcomputer_use.computer. Model classes look for this label, not for a tool name.ComputerCallPartandComputerReturnPartare the typed parts for a call and its result.ComputerResultis the result's metadata: how many actions ran, the error if one failed, andactions_changedwhen a hook or approval changed the actions before they ran.
- Typed tool metadata:
ToolKind(metadata=...)declares the shape of a kind's metadata, andtyped_metadatareturns it after validation, orNoneif it doesn't fit. ComputerTool(display_size=...): a native tool sent in place of the request's localcomputertool, which still runs the calls. It is sent only when the request has exactly one callable tool of the computer kind. With none, anoptionalone 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. InFallbackModel(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.