How to use computer use tool
Quick answer
Use the OpenAI Python SDK with the
tools parameter specifying a computer_20241022 tool and include betas=["computer-use-2024-10-22"] in your chat completion request. This enables the AI model to perform computer interactions like screenshots or file manipulations.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the latest openai Python package and set your OpenAI API key as an environment variable.
pip install --upgrade openai Step by step
This example shows how to call the OpenAI chat completion API with the computer use tool enabled. The tools parameter defines the computer tool with display dimensions, and the betas parameter activates the computer use beta feature.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
tools = [{
"type": "computer_20241022",
"name": "computer",
"display_width_px": 1024,
"display_height_px": 768
}]
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Take a screenshot of the desktop."}],
tools=tools,
betas=["computer-use-2024-10-22"]
)
print(response.choices[0].message.content) output
Screenshot saved as screenshot.png
Common variations
- Use different models like
gpt-4o-miniorgpt-4odepending on your needs. - Adjust
display_width_pxanddisplay_height_pxto match your virtual screen size. - Use async calls with
asyncioandawaitfor non-blocking execution.
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
tools = [{
"type": "computer_20241022",
"name": "computer",
"display_width_px": 1280,
"display_height_px": 720
}]
response = await client.chat.completions.acreate(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Open the calculator app."}],
tools=tools,
betas=["computer-use-2024-10-22"]
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Calculator app opened successfully.
Troubleshooting
- If you get an error about missing
betas, ensure you includebetas=["computer-use-2024-10-22"]in your request. - If the tool calls are not recognized, verify your API key has access to beta features.
- Check that
toolsparameter uses"type": "computer_20241022"exactly; other values will not work.
Key Takeaways
- Always include the computer use tool in the
toolsparameter with correct type and display size. - Enable the computer use beta by adding
betas=["computer-use-2024-10-22"]in your chat completion call. - Use the OpenAI Python SDK v1+ with environment variable API keys for secure and up-to-date integration.