Authoring Python-Based Tools
Python tools are one of the most flexible and powerful ways to extend agent functionality in watsonx Orchestrate, especially when combined with Flows. 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.
Python tools must use Google-style docstrings to apply descriptions and document the tool.
Importing Python-Based Tools
To import a Python tool use theorchestrate 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
Adding Dependencies
Adding Dependencies
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 exampleWhen importing your tool, specify the requirements.txt file by using the -r flag.
requirements.txt
file.Python tool examplePython
TEXT
For more information on the format of a requests file, please see the official pip documentation.
BASH
Using complex input and output argument types with Pydantic
Using complex input and output argument types with Pydantic
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
Securely providing credentials
Securely providing credentials
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 please see, Connections, and the expected_credentials input to the @tool
annotation.Creating multi file Python tool packages
Creating multi file Python tool packages
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:Importing your multifile Python tool:
Assuming your cli was currently in the my_agentic_project folder, this tool could be imported using the following:Limitations:
PYTHON
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 thename
attribute of the@tool
decorator inmy_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.
- 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
Creating tools that accept files
Creating tools that accept files
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.
Creating tools that return inline tables, images or links
Creating tools that return inline tables, images or links
The native watsonx Orchestrate webchat supports rendering markdown. To render output correctly, it is possible
to instruct the LLM to render the output in this format, though in some cases it’s desirable to simply have the
tool return the expected output and reduce the amount of transformation that the LLM has to do.Inline tables
Links
Images
PYTHON
PYTHON
PYTHON
Common pitfalls of Python tools
Host networking
- On watsonx Orchestrate Developer Edition: Because Python tools run within a container, localhost within a Python tool refers to the
container executing the tool, not the host machine. To call an endpoint on your host machine, use a URL of
docker.host.internal
or explicitly use your host machine’s IP address. - On SaaS: The SaaS version of watsonx Orchestrate does not have access to the internal network of your company. If this access is necessary it is possible to prototype with the Developer Edition and then deploy watsonx Orchestrate via the Cloud Pak for Data, on-premises, or by engaging with IBM for alternatives.
- On CPD (On-premises): for a tool to be able to access the external internet resources, ensure that no firewall rules prevent access to that resource from the node pool responsible for tool execution.