Skip to main content
Python tools are one of the most flexible and powerful ways to extend agent functionality in watsonx Orchestrate, especially when combined with agentic workflows. A Python tool consists of one or more Python files. Within these files, you define one or more functions and annotate them with the @tool decorator to expose them to Orchestrate. Each Python tool runs inside an isolated container with its own virtual environment. This ensures that the tool and its dependencies remain sandboxed from the host operating system, providing a secure and consistent runtime. Python tool example
Python
Note:
Python tools must use Google-style docstrings to apply descriptions and document the tool.

Importing Python-based tools

To import a Python tool use the orchestrate tools import command using the -f flag to specify which python file contains your tool definitions. Each @tool annotated in the given file will be exposed as a tool within your active watsonx Orchestrate environment. It is recommended to include only a single @tool annotated function per file so that tools can be imported independently.
BASH

Additional features of Python tools

If your Python function relies on external libraries or packages, you can ensure your tool works correctly by specifying these dependencies in the requirements.txt file.Python tool example
Python
requirements.txt example
TEXT
For more information on the format of a requests file, see the official pip documentation.
When importing your tool, specify the requirements.txt file by using the -r flag.
BASH
Python tools support input and output arguments based on any native Python type from the typings package and classes which extend BaseModel from the popular library Pydantic.Example
PYTHON
Python tools are compatible with the watsonx Orchestrate Connections framework. A connection represents a dependency on an external service and are a way to associate credentials to a tool such as those for Basic, Bearer, API Key, OAuth, or IDP SSO based authentication flows. Python tools also support key_value connections which as the name implies are arbitrary dictionaries of keys and values which can be used to pass in credentials for any authorization scheme which does not match one of the existing native schemes.Connections are referenced by something known as an application id (or app_id). This app_id is the unique identifier of a connection. OpenAPI connections can have at most one Connection’s app_id associated to them.For more information, see Connections, and the expected_credentials input to the @tool annotation.
In addition to importing a single tool file, it is possible to import an entire folder (package) along with your tool file. This is useful when you wish to centralize logic into shared utility libraries, for example, shared authentication, data processing, or including static files such as CSVs or sqlite databases.Assuming a folder structure as follows:
my_tool.py:
PYTHON
Importing your multifile Python tool:
Assuming your CLI was currently in the my_agentic_project folder, this tool could be imported using the following:
BASH
Notes:
  • Only tools defined in my_tool.py will be imported. Other Python files in the package will not be scanned for tools.
  • The system will only accept strings composed of alphanumeric characters and underscores (_) in the name attribute of the @tool decorator in my_tool.py.
  • The system will only accept tool file names composed of alphanumeric characters and underscores (_).
  • The package root folder and the tool file path CLI arguments MUST share a common base path.
  • The path of the tool file folder relative to the package root folder, must be composed of folder names which are only composed of alphanumeric characters and underscores (_).
  • Any whitespace like characters which prefix or suffix provided package root path will be stripped by the system.
  • A package root folder path that resolves to an empty string will make the system assume that no package root was specified. Then, the system falls back to single Python file tool import.
Limitations:
  • The max compressed tool size: 50Mb
  • File resolution for non-python packages must be done relative to the current file, as seen above
  • Imports to packages within your code must be resolved relatively
You can create Python tools that accept files or return files to download.To do that, you must comply with the following requirements:
  • To accept files as input, the tool must accept a sequence of bytes as arguments.
  • To return a file for download, the tool must return a sequence of bytes as output.
The following example accepts a file as input and returns the processed file for download:
Use the WXOFile parameter type when a tool must reference a file without processing its full content.
The WXOFile class provides methods to retrieve file properties or content as needed.
You can call the following methods:Use this approach when a tool only needs file metadata or to read the content on demand.
It helps reduce processing overhead and improves performance.
If your agent has access to context variables, provided either by the default context or by passing context with the request, you can read these variables within your python tool.To do this you will need to pass an AgentRun object paramater to the python tool:Python tool example
Python

Notes about context in python tools

  • Writing information to the context is not currently supported in python tools, as such the request_context is immutable. Attempting to write to the context will cause the tool to error.
  • A tool can only have one AgentRun paramater, a tools with more than one will fail to import
For more information about providing context to agents see:

Runtime and migration strategy

Python tools execute within a UV virtual environment inside a component called the tools runtime. This runtime includes a predefined set of supported Python versions. Currently, the only supported version is Python 3.12.

Version Management

  • When a tool is imported into the runtime, it uses the Python version it was originally imported with.
  • If that version becomes deprecated by watsonx Orchestrate, the system will:
    • Display a deprecation warning in the UI.
    • Show the same warning when running the command:
    • Inform the user that the tool must be reimported to remain compatible.

Deprecation Timeline

  • After 24 months of deprecation:
    • The runtime will remove support for the deprecated Python version.
    • If the tool has not been reimported, the runtime will attempt to reimport it using the closest supported Python version.

Limitations of Python tools

Host networking

  • watsonx Orchestrate Developer Edition: Python tools run inside a container. In this environment, localhost refers to the container itself, not the host machine. To call an endpoint on the host machine, use docker.host.internal or the host machine’s IP address.
  • SaaS: The SaaS version of watsonx Orchestrate cannot access your company’s internal network. If internal access is required, prototype with the Developer Edition and then deploy watsonx Orchestrate on-premises or consult IBM for alternative solutions.
  • On-premises: To allow a tool to access external internet resources, ensure that firewall rules do not block outbound traffic from the node pool responsible for tool execution.

Read only filesystem

For security and stability, tools execute in a read-only filesystem. Each tool can access only its own filesystem and cannot modify files during execution.