Brief IA

Python: Transforming Functions into Tools with OpenAI Agents SDK

🛠️ AI Tools·Tom Levy·

Python: Transforming Functions into Tools with OpenAI Agents SDK

Python: Transforming Functions into Tools with OpenAI Agents SDK
Key Takeaways
1A Python function decorated with @function_tool becomes a tool usable by an agent
2Runner orchestrates a loop where the model can call the tool until the final answer
3Example times: 1.59 s for python.org, 0.83 s for github.com, 0.49 s for openai.com, all with HTTP 200
💡Why it mattersThe SDK allows for orchestrating existing automations without rewriting, with models cited as more accessible for tool-equipped or even multi-agent systems.
Le brief IA que lisent les pros

Le brief IA que les pros lisent chaque soir

Les 7 actus IA du jour, décryptées en 5 min. Gratuit.

Inclus dès l'inscription : notre sélection des meilleurs guides & comparatifs IA.

Choisis ton rythme

Gratuit · Pas de spam · Désabonnement en 1 clic

📄
Full Analysis

A Python script does not need to be rewritten to be controlled by an agent. By decorating existing functions and entrusting them to the OpenAI Agents SDK, a model decides when and how to call them. An example of website monitoring illustrates this approach, with GPT-5.6 Luna cited as a model option.

Expanded Use Cases and More Affordable Models

Agentic AI is presented as a practical method for automating workflows. More and more companies are using agents to manage multi-step tasks rather than relying on fixed scripts. It also mentions that less expensive models like GPT-5.6 Luna make the execution of systems using tools, or even large-scale multi-agent systems, more accessible. Several application examples are provided: analyzing CSV files to filter rows, calculate metrics, and detect trends; monitoring servers by checking CPU, memory, disk, and processes; exploring logs to count errors and extract events; automating APIs to retrieve data, update records, or create reports.

Setting Up the Project and Exposing Functions as Tools

Preparation involves creating a new Python project and installing the openai-agents and requests packages, then setting the OPENAI_API_KEY environment variable. The OpenAI Agents SDK provides a runtime environment for agents, tools, transfers, sessions, and tracing. To transform a Python function into a tool, simply add the @function_tool decorator. The SDK automatically converts the function signature into a JSON schema and uses the name and docstring to describe the tool. In this schema, the Python code continues to perform the work, while the agent brings natural language understanding, tool selection, and orchestration.

From Fixed Script to Agent: What Changes in the Workflow

The initial script sends an HTTP request with a timeout of 10, reads the status code, measures latency, and returns the result. Without an agent, one must write all the logic in Python to check multiple sites, compare response times, or identify a failing site, typically through an explicit loop. By exposing the function as a tool, the AI model decides when to call it, which URLs to check, how many times to use it, and how to leverage the results, then produces the response.

Creating the Agent and Controlling Execution with Runner

The agent is created using Agent and Runner from the agents module. In the example, the agent named Website Monitor uses the gpt-5.6-luna model, receives instructions to monitor websites, compare results, and explain issues, and has the check_website function in its tool list. Runner.run_sync executes a request asking to test python.org, github.com, and openai.com to identify the slowest site, with the final result accessible via result.final_output.

The Tooling Loop: Iterative Decisions Until the Response

The Runner manages the interaction between the model and the tools within a loop. If the model feels it lacks information, it can recall the tool, and the loop continues until the final response is produced. The agentic nature comes from the model choosing actions to take based on the request and successive results, rather than following a fixed sequence. In the example, the function's docstring describes checking the HTTP status and response time, and the returned measurements indicate 1.59 seconds for python.org, 0.83 seconds for github.com, and 0.49 seconds for openai.com, all with an HTTP status of 200.

Brief IA — L'actualité IA en français

L'essentiel de l'actualité de l'intelligence artificielle, décrypté et expliqué chaque jour.